forked from zovos/bot_tg
fix: resolve merge conflicts with upstream - economy + watch features
This commit is contained in:
commit
55ce1fbecc
33 changed files with 5464 additions and 335 deletions
0
.codex
Normal file
0
.codex
Normal file
8
.env
8
.env
|
|
@ -1,3 +1,9 @@
|
|||
BOT_TOKEN=8156642467:AAEalekr-33lFS3tTTkXIEiaawfDzR6lBJc
|
||||
BOT_TOKEN=8505447901:AAHFkaDG0fT0tAixuJ4QP91IwMgO6cZndRM #8156642467:AAEalekr-33lFS3tTTkXIEiaawfDzR6lBJc
|
||||
TZ=Europe/Moscow
|
||||
ODDS_API_KEY=c29bc26dee8aa9d95a5ce8d14ea63923
|
||||
LLAMA_API_URL=https://mirror.porno4free.ru/zovos-ai/
|
||||
LLAMA_FORCE_DISABLE_THINKING=0
|
||||
LLAMA_LOG_THINKING=1
|
||||
WEBAPP_URL=https://uninterleaved-scrawnily-nicol.ngrok-free.dev
|
||||
|
||||
NGROK_AUTHTOKEN=39k4ojhN5UPUWQwb1ly84ORM2IE_5JnhMaKk5p8LFM3TCetyg
|
||||
|
|
|
|||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -22,3 +22,4 @@ db/
|
|||
AI/models/
|
||||
|
||||
main_legacy.py
|
||||
docker-compose.override.yaml
|
||||
|
|
|
|||
1162
AI/talk_handler.py
1162
AI/talk_handler.py
File diff suppressed because it is too large
Load diff
18
Dockerfile.api
Normal file
18
Dockerfile.api
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
FROM python:3.11-slim
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_NO_CACHE_DIR=1
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt ./
|
||||
COPY webapp/requirements-api.txt ./requirements-api.txt
|
||||
RUN python -m pip install --upgrade pip \
|
||||
&& python -m pip install -r requirements.txt \
|
||||
&& python -m pip install -r requirements-api.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN mkdir -p /db
|
||||
|
||||
EXPOSE 8080
|
||||
CMD ["python", "-m", "webapp.api"]
|
||||
15
README.md
15
README.md
|
|
@ -32,6 +32,8 @@ docker compose down
|
|||
|
||||
Переменные окружения: задайте `BOT_TOKEN` в `.env`.
|
||||
Для ИИ-фич нужен `LLAMA_API_URL`.
|
||||
По умолчанию проект настроен на `https://mirror.porno4free.ru/zovos-ai/`.
|
||||
Если нужен лог reasoning/thinking модели, оставь `LLAMA_LOG_THINKING=1`; чтобы модель не резалась в режим без thinking, задай `LLAMA_FORCE_DISABLE_THINKING=0`.
|
||||
Для ставок нужен `ODDS_API_KEY` (бесплатно на https://the-odds-api.com).
|
||||
|
||||
Контейнер хранит состояние в `./db`:
|
||||
|
|
@ -40,6 +42,7 @@ docker compose down
|
|||
- `polychaetsi_stats.json` — статистика слова "получается"
|
||||
- `bets.sqlite3` — ставки на спорт
|
||||
- `economy.sqlite3` — экономическая система (балансы, вклады, кредиты)
|
||||
- `nude_history.json` — история запросов /nude
|
||||
|
||||
## Команды
|
||||
|
||||
|
|
@ -111,6 +114,18 @@ docker compose down
|
|||
- В образ копируется весь проект, а runtime-данные исключены через `.dockerignore`.
|
||||
- Если меняешь код или картинки, достаточно пересобрать: `docker compose up --build -d`.
|
||||
|
||||
## HTTPS / Let's Encrypt
|
||||
|
||||
- Фронтендовый `nginx` теперь слушает `80` и `443`, отдает ACME challenge и сам запускает `certbot` внутри контейнера.
|
||||
- Для автоматической выдачи сертификата задай в `.env`:
|
||||
- `APP_DOMAIN=bot.example.com`
|
||||
- `APP_WWW_DOMAIN=www.bot.example.com` (опционально)
|
||||
- `ACME_EMAIL=admin@example.com`
|
||||
- Домен должен уже смотреть на сервер, а порты `80` и `443` должны быть доступны снаружи.
|
||||
- Для безопасной первой проверки можно временно включить `CERTBOT_STAGING=1`, потом вернуть `0`.
|
||||
- Сертификаты и webroot challenge хранятся в docker volumes `certbot_etc` и `certbot_www`, обновление запускается по кругу с интервалом `CERTBOT_RENEW_INTERVAL` (по умолчанию `12h`).
|
||||
- Если нужен старый локальный маппинг портов, можно переопределить `WEBAPP_HTTP_PORT` и `WEBAPP_HTTPS_PORT`, но для реального Let's Encrypt внешний `80` должен оставаться доступным.
|
||||
|
||||
## Кратко о логике
|
||||
|
||||
- `/maket` подбирает размер текста, чтобы уместить его в область макета.
|
||||
|
|
|
|||
|
|
@ -47,6 +47,14 @@ BET_MAX_AMOUNT = 3.0
|
|||
BET_DB_PATH = "/db/bets.sqlite3"
|
||||
ODDS_CACHE_TTL = 86400 # 24 часа — экономим запросы (500/мес бесплатно)
|
||||
|
||||
# --- Nude ---
|
||||
NUDE_HISTORY_PATH = Path(os.getenv("NUDE_HISTORY_PATH", "/db/nude_history.json"))
|
||||
NUDE_POSTS_PER_PAGE = int(os.getenv("NUDE_POSTS_PER_PAGE", "100"))
|
||||
NUDE_POSTS_POOL_TARGET = int(os.getenv("NUDE_POSTS_POOL_TARGET", "1200"))
|
||||
NUDE_MAX_POSTS_TO_SCAN = int(os.getenv("NUDE_MAX_POSTS_TO_SCAN", "2000"))
|
||||
NUDE_MIN_UNSEEN_POOL = int(os.getenv("NUDE_MIN_UNSEEN_POOL", "100"))
|
||||
NUDE_HISTORY_MAX_SIZE = int(os.getenv("NUDE_HISTORY_MAX_SIZE", "5000"))
|
||||
|
||||
# --- Magnit API ---
|
||||
MAGNIT_API_BASE_URL = os.getenv("MAGNIT_API_BASE_URL", "https://mirror.porno4free.ru/magnit")
|
||||
MAGNIT_STORE_CODE = os.getenv("MAGNIT_STORE_CODE", "618224")
|
||||
|
|
|
|||
38
docker-compose.webapp-test.yml
Normal file
38
docker-compose.webapp-test.yml
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
services:
|
||||
webapp-api-test:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.api
|
||||
container_name: fenyabot-webapp-api-test
|
||||
volumes:
|
||||
- ./db:/db
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
TZ: Europe/Moscow
|
||||
PENIS_DB_PATH: /db/penis_stats.sqlite3
|
||||
CHAT_HISTORY_DB_PATH: /db/chat_history.sqlite3
|
||||
POLYCHAETSI_STATS_PATH: /db/polychaetsi_stats.json
|
||||
BET_DB_PATH: /db/bets.sqlite3
|
||||
API_PORT: "8080"
|
||||
LLAMA_API_URL: ${LLAMA_API_URL:-https://mirror.porno4free.ru/zovos-ai/}
|
||||
LLAMA_FORCE_DISABLE_THINKING: ${LLAMA_FORCE_DISABLE_THINKING:-0}
|
||||
LLAMA_LOG_THINKING: ${LLAMA_LOG_THINKING:-1}
|
||||
ports:
|
||||
- "18080:8080"
|
||||
restart: unless-stopped
|
||||
|
||||
ngrok-test:
|
||||
image: ngrok/ngrok:latest
|
||||
container_name: fenyabot-ngrok-test
|
||||
depends_on:
|
||||
- webapp-api-test
|
||||
environment:
|
||||
NGROK_AUTHTOKEN: ${NGROK_AUTHTOKEN}
|
||||
command:
|
||||
- http
|
||||
- webapp-api-test:8080
|
||||
- --log=stdout
|
||||
ports:
|
||||
- "4041:4040"
|
||||
restart: unless-stopped
|
||||
|
|
@ -13,5 +13,51 @@ services:
|
|||
POLYCHAETSI_STATS_PATH: /db/polychaetsi_stats.json
|
||||
BETS_DB_PATH: /db/bets.sqlite3
|
||||
ECONOMY_DB_PATH: /db/economy.sqlite3
|
||||
LLAMA_API_URL: ${LLAMA_API_URL:-https://mirror.porno4free.ru/zovos-ai/}
|
||||
LLAMA_FORCE_DISABLE_THINKING: ${LLAMA_FORCE_DISABLE_THINKING:-0}
|
||||
LLAMA_LOG_THINKING: ${LLAMA_LOG_THINKING:-1}
|
||||
init: true
|
||||
restart: unless-stopped
|
||||
|
||||
webapp-api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.api
|
||||
volumes:
|
||||
- ./db:/db
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
TZ: Europe/Moscow
|
||||
PENIS_DB_PATH: /db/penis_stats.sqlite3
|
||||
CHAT_HISTORY_DB_PATH: /db/chat_history.sqlite3
|
||||
POLYCHAETSI_STATS_PATH: /db/polychaetsi_stats.json
|
||||
BET_DB_PATH: /db/bets.sqlite3
|
||||
API_PORT: "8080"
|
||||
LLAMA_API_URL: ${LLAMA_API_URL:-https://mirror.porno4free.ru/zovos-ai/}
|
||||
LLAMA_FORCE_DISABLE_THINKING: ${LLAMA_FORCE_DISABLE_THINKING:-0}
|
||||
LLAMA_LOG_THINKING: ${LLAMA_LOG_THINKING:-1}
|
||||
restart: unless-stopped
|
||||
|
||||
webapp-frontend:
|
||||
build:
|
||||
context: ./webapp/frontend
|
||||
ports:
|
||||
- "${WEBAPP_HTTP_PORT:-80}:80"
|
||||
- "${WEBAPP_HTTPS_PORT:-443}:443"
|
||||
environment:
|
||||
APP_DOMAIN: ${APP_DOMAIN:-}
|
||||
APP_WWW_DOMAIN: ${APP_WWW_DOMAIN:-}
|
||||
ACME_EMAIL: ${ACME_EMAIL:-}
|
||||
CERTBOT_STAGING: ${CERTBOT_STAGING:-0}
|
||||
CERTBOT_RENEW_INTERVAL: ${CERTBOT_RENEW_INTERVAL:-12h}
|
||||
volumes:
|
||||
- certbot_etc:/etc/letsencrypt
|
||||
- certbot_www:/var/www/certbot
|
||||
depends_on:
|
||||
- webapp-api
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
certbot_etc:
|
||||
certbot_www:
|
||||
|
|
|
|||
|
|
@ -59,7 +59,10 @@ def _init_bets_db() -> None:
|
|||
conn.commit()
|
||||
|
||||
|
||||
_init_bets_db()
|
||||
try:
|
||||
_init_bets_db()
|
||||
except Exception:
|
||||
logger.warning("Could not init bets DB at import time (will retry on first use)")
|
||||
|
||||
|
||||
def remember_user_sport_context(user_id: int, sport_alias: str) -> None:
|
||||
|
|
|
|||
|
|
@ -58,6 +58,9 @@ def update_user_length(user_id: int, delta: float) -> float | None:
|
|||
if row is None:
|
||||
return None
|
||||
new_length = round(float(row[0]) + delta, 1)
|
||||
# Не даём уйти ниже 0 при проигрыше
|
||||
if delta < 0 and new_length < 0:
|
||||
new_length = 0.0
|
||||
conn.execute(
|
||||
"UPDATE penis_stats SET length = ? WHERE user_id = ?",
|
||||
(new_length, user_id),
|
||||
|
|
@ -69,6 +72,7 @@ def update_user_length(user_id: int, delta: float) -> float | None:
|
|||
return None
|
||||
|
||||
|
||||
|
||||
def play_casino(user_id: int, bet: float) -> str:
|
||||
current = economy.get_user_balance(user_id)
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ async def generate_fortune(topic: str) -> tuple[str, str, str]:
|
|||
}
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, json=payload, timeout=aiohttp.ClientTimeout(total=120)) as resp:
|
||||
async with session.post(url, json=payload, timeout=aiohttp.ClientTimeout(total=30)) as resp:
|
||||
data = await resp.json()
|
||||
text = data["choices"][0]["message"]["content"].strip()
|
||||
|
||||
|
|
|
|||
368
games/nude.py
368
games/nude.py
|
|
@ -1,108 +1,320 @@
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
from pathlib import Path, PurePosixPath
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import aiohttp
|
||||
from aiogram import types
|
||||
|
||||
import config
|
||||
import logging
|
||||
import random
|
||||
import re
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CAPTIONS = [
|
||||
"Hot stuff 🔥",
|
||||
"Sexy content 😏"
|
||||
"Шо ты блять псюнчик решил свой подергать?",
|
||||
"Решил подрочить сука?",
|
||||
"гнойный писюн у тебя кнч, да",
|
||||
"Ух нихуя, это ШПАЧИХА!?",
|
||||
]
|
||||
|
||||
LESAINT_POSTS_API = "https://lesaintdesseins.fr/wp-json/wp/v2/posts"
|
||||
LESAINT_REFERER = "https://lesaintdesseins.fr/"
|
||||
REQUEST_HEADERS = {
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/124.0.0.0 Safari/537.36"
|
||||
),
|
||||
"Accept": "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en-US,en;q=0.9",
|
||||
"Referer": LESAINT_REFERER,
|
||||
}
|
||||
|
||||
_NUDE_HISTORY_LOCK = asyncio.Lock()
|
||||
|
||||
|
||||
def _resolve_int_setting(name: str, default: int) -> int:
|
||||
value = getattr(config, name, None)
|
||||
if value is None:
|
||||
value = os.getenv(name, str(default))
|
||||
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
logger.warning("Invalid %s=%r, using default %s", name, value, default)
|
||||
return default
|
||||
|
||||
|
||||
def _resolve_path_setting(name: str, default: str) -> Path:
|
||||
value = getattr(config, name, None)
|
||||
if value is None:
|
||||
value = os.getenv(name, default)
|
||||
|
||||
if isinstance(value, Path):
|
||||
return value
|
||||
|
||||
return Path(str(value))
|
||||
|
||||
|
||||
NUDE_HISTORY_PATH = _resolve_path_setting("NUDE_HISTORY_PATH", "/db/nude_history.json")
|
||||
NUDE_POSTS_PER_PAGE = _resolve_int_setting("NUDE_POSTS_PER_PAGE", 100)
|
||||
NUDE_POSTS_POOL_TARGET = _resolve_int_setting("NUDE_POSTS_POOL_TARGET", 1200)
|
||||
NUDE_MAX_POSTS_TO_SCAN = _resolve_int_setting("NUDE_MAX_POSTS_TO_SCAN", 2000)
|
||||
NUDE_MIN_UNSEEN_POOL = _resolve_int_setting("NUDE_MIN_UNSEEN_POOL", 100)
|
||||
NUDE_HISTORY_MAX_SIZE = _resolve_int_setting("NUDE_HISTORY_MAX_SIZE", 5000)
|
||||
|
||||
|
||||
def _extract_photo_candidates(posts: list[dict]) -> list[dict]:
|
||||
candidates = []
|
||||
seen_urls = set()
|
||||
|
||||
for post in posts:
|
||||
embedded = post.get("_embedded") or {}
|
||||
media_items = embedded.get("wp:featuredmedia") or []
|
||||
|
||||
for media in media_items:
|
||||
if media.get("media_type") != "image":
|
||||
continue
|
||||
|
||||
media_details = media.get("media_details") or {}
|
||||
sizes = media_details.get("sizes") or {}
|
||||
image_url = (
|
||||
(sizes.get("full") or {}).get("source_url")
|
||||
or media.get("source_url")
|
||||
)
|
||||
|
||||
if not image_url or image_url in seen_urls:
|
||||
continue
|
||||
|
||||
seen_urls.add(image_url)
|
||||
candidates.append(
|
||||
{
|
||||
"image_url": image_url,
|
||||
"post_url": post.get("link", LESAINT_REFERER),
|
||||
"title": (post.get("title") or {}).get("rendered", "").strip(),
|
||||
}
|
||||
)
|
||||
break
|
||||
|
||||
return candidates
|
||||
|
||||
|
||||
def _guess_filename(image_url: str) -> str:
|
||||
parsed = urlparse(image_url)
|
||||
filename = PurePosixPath(parsed.path).name or "nude.jpg"
|
||||
|
||||
if "." not in filename:
|
||||
filename = f"{filename}.jpg"
|
||||
|
||||
return filename
|
||||
|
||||
|
||||
def _load_nude_history() -> dict:
|
||||
path = NUDE_HISTORY_PATH
|
||||
if not path.exists():
|
||||
return {"sent_images": []}
|
||||
|
||||
try:
|
||||
with path.open("r", encoding="utf-8") as fh:
|
||||
data = json.load(fh)
|
||||
except (OSError, json.JSONDecodeError):
|
||||
logger.exception("Failed to load nude history")
|
||||
return {"sent_images": []}
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return {"sent_images": []}
|
||||
|
||||
sent_images = data.get("sent_images")
|
||||
if not isinstance(sent_images, list):
|
||||
sent_images = []
|
||||
|
||||
cleaned = [item for item in sent_images if isinstance(item, str) and item.strip()]
|
||||
return {"sent_images": cleaned[-NUDE_HISTORY_MAX_SIZE :]}
|
||||
|
||||
|
||||
def _save_nude_history(data: dict) -> None:
|
||||
path = NUDE_HISTORY_PATH
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp_path = path.with_name(f"{path.name}.tmp")
|
||||
with tmp_path.open("w", encoding="utf-8") as fh:
|
||||
json.dump(data, fh, ensure_ascii=False, indent=2)
|
||||
tmp_path.replace(path)
|
||||
|
||||
|
||||
def _remember_sent_image(history: dict, image_url: str) -> None:
|
||||
sent_images = history.get("sent_images")
|
||||
if not isinstance(sent_images, list):
|
||||
sent_images = []
|
||||
|
||||
sent_images = [item for item in sent_images if item != image_url]
|
||||
sent_images.append(image_url)
|
||||
history["sent_images"] = sent_images[-NUDE_HISTORY_MAX_SIZE :]
|
||||
|
||||
|
||||
async def _fetch_posts_page(
|
||||
session: aiohttp.ClientSession, page: int, per_page: int
|
||||
) -> tuple[list[dict], int | None]:
|
||||
params = {
|
||||
"page": str(page),
|
||||
"per_page": str(per_page),
|
||||
"orderby": "date",
|
||||
"order": "desc",
|
||||
"_embed": "wp:featuredmedia",
|
||||
}
|
||||
|
||||
async with session.get(LESAINT_POSTS_API, params=params, headers=REQUEST_HEADERS) as resp:
|
||||
if resp.status == 400 and page > 1:
|
||||
body = await resp.text()
|
||||
if "rest_post_invalid_page_number" in body:
|
||||
return [], None
|
||||
raise RuntimeError(f"API returned 400: {body[:300]}")
|
||||
|
||||
if resp.status != 200:
|
||||
body = await resp.text()
|
||||
raise RuntimeError(f"API returned {resp.status}: {body[:300]}")
|
||||
|
||||
total_pages_header = resp.headers.get("X-WP-TotalPages")
|
||||
total_pages = (
|
||||
int(total_pages_header)
|
||||
if total_pages_header and total_pages_header.isdigit()
|
||||
else None
|
||||
)
|
||||
posts = await resp.json(content_type=None)
|
||||
|
||||
if not isinstance(posts, list):
|
||||
raise RuntimeError("Unexpected API response format")
|
||||
|
||||
return posts, total_pages
|
||||
|
||||
|
||||
async def _fetch_latest_photo_candidates(
|
||||
session: aiohttp.ClientSession, sent_urls: set[str]
|
||||
) -> list[dict]:
|
||||
per_page = max(1, min(NUDE_POSTS_PER_PAGE, 100))
|
||||
max_scan = max(NUDE_POSTS_POOL_TARGET, NUDE_MAX_POSTS_TO_SCAN)
|
||||
max_pages = max(1, (max_scan + per_page - 1) // per_page)
|
||||
|
||||
candidates = []
|
||||
seen_urls = set()
|
||||
unseen_count = 0
|
||||
total_pages = None
|
||||
|
||||
for page in range(1, max_pages + 1):
|
||||
if total_pages is not None and page > total_pages:
|
||||
break
|
||||
|
||||
posts, reported_total_pages = await _fetch_posts_page(session, page, per_page)
|
||||
if reported_total_pages is not None:
|
||||
total_pages = reported_total_pages
|
||||
if not posts:
|
||||
break
|
||||
|
||||
for candidate in _extract_photo_candidates(posts):
|
||||
image_url = candidate["image_url"]
|
||||
if image_url in seen_urls:
|
||||
continue
|
||||
|
||||
seen_urls.add(image_url)
|
||||
candidates.append(candidate)
|
||||
|
||||
if image_url not in sent_urls:
|
||||
unseen_count += 1
|
||||
|
||||
if len(candidates) >= NUDE_POSTS_POOL_TARGET and unseen_count >= NUDE_MIN_UNSEEN_POOL:
|
||||
break
|
||||
if len(candidates) >= max_scan:
|
||||
break
|
||||
|
||||
return candidates
|
||||
|
||||
|
||||
def _pick_unsent_candidate(candidates: list[dict], sent_urls: set[str]) -> dict | None:
|
||||
unseen = [candidate for candidate in candidates if candidate["image_url"] not in sent_urls]
|
||||
if not unseen:
|
||||
return None
|
||||
return random.choice(unseen)
|
||||
|
||||
|
||||
async def _download_photo(
|
||||
session: aiohttp.ClientSession, image_url: str
|
||||
) -> tuple[bytes | None, str]:
|
||||
async with session.get(image_url, headers=REQUEST_HEADERS) as resp:
|
||||
if resp.status != 200:
|
||||
logger.warning("Image download failed for %s: %s", image_url, resp.status)
|
||||
return None, _guess_filename(image_url)
|
||||
|
||||
content_type = resp.headers.get("Content-Type", "")
|
||||
if "image" not in content_type.lower():
|
||||
logger.warning(
|
||||
"Unexpected content type for %s: %s", image_url, content_type
|
||||
)
|
||||
return None, _guess_filename(image_url)
|
||||
|
||||
return await resp.read(), _guess_filename(image_url)
|
||||
|
||||
|
||||
async def handle_nude_cmd(message: types.Message):
|
||||
args = message.text.split(maxsplit=1)
|
||||
|
||||
if len(args) > 1 and args[1].strip() == "-h":
|
||||
help_msg = (
|
||||
"🔥 <b>Справка по команде /nude:</b>\n\n"
|
||||
"Получает случайные изображения взрослых женщин из открытых источников.\n"
|
||||
"• Использует прямое сканирование сайтов для поиска контента\n"
|
||||
"• По умолчанию ищет популярные изображения\n"
|
||||
"• Отправляет изображения напрямую\n\n"
|
||||
"<i>Пример:</i> <code>/nude</code>\n"
|
||||
"<i>По умолчанию:</i> Случайные изображения с популярных сайтов"
|
||||
"Берет случайную фотку из последних постов сайта le saint des seins.\n"
|
||||
"• Источник: <code>https://lesaintdesseins.fr/</code>\n"
|
||||
"• Пагинацией тянет 1000+ последних постов через WordPress REST API\n"
|
||||
"• Хранит историю отправок и старается не повторять фотки\n"
|
||||
"• Выбирает случайную картинку и отправляет ее прямо в Telegram\n\n"
|
||||
"<i>Пример:</i> <code>/nude</code>"
|
||||
)
|
||||
await message.reply(help_msg, parse_mode="HTML")
|
||||
return
|
||||
|
||||
# Список сайтов для сканирования
|
||||
sites = [
|
||||
"https://www.imagefap.com/gallery.php?pg=random",
|
||||
"https://xhamster.com/photos?sort=random",
|
||||
"https://spankbang.com/random"
|
||||
]
|
||||
|
||||
random_site = random.choice(sites)
|
||||
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"Connection": "keep-alive",
|
||||
"Upgrade-Insecure-Requests": "1"
|
||||
}
|
||||
timeout = aiohttp.ClientTimeout(total=60)
|
||||
history_snapshot = _load_nude_history()
|
||||
sent_urls_snapshot = set(history_snapshot["sent_images"])
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(random_site, headers=headers) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"Site access error: {resp.status}")
|
||||
await message.reply("Не удалось получить доступ к сайту :(")
|
||||
return
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
candidates = await _fetch_latest_photo_candidates(session, sent_urls_snapshot)
|
||||
|
||||
html = await resp.text()
|
||||
if not candidates:
|
||||
await message.reply("Не удалось найти свежие фотки :(")
|
||||
return
|
||||
|
||||
# Ищем URL изображений в HTML
|
||||
img_patterns = [
|
||||
r'<img[^>]+src="([^"]+)"',
|
||||
r'<a[^>]+href="([^"]+\.(jpg|jpeg|png|gif|webp))"',
|
||||
r'data-src="([^"]+)"'
|
||||
]
|
||||
async with _NUDE_HISTORY_LOCK:
|
||||
history = _load_nude_history()
|
||||
sent_urls = set(history["sent_images"])
|
||||
selected = _pick_unsent_candidate(candidates, sent_urls)
|
||||
|
||||
img_urls = []
|
||||
for pattern in img_patterns:
|
||||
matches = re.findall(pattern, html, re.IGNORECASE)
|
||||
img_urls.extend(matches)
|
||||
if selected is None:
|
||||
logger.info("Nude history exhausted current pool, resetting history")
|
||||
history = {"sent_images": []}
|
||||
selected = random.choice(candidates)
|
||||
|
||||
if not img_urls:
|
||||
await message.reply("Изображения не найдены на странице :(")
|
||||
return
|
||||
_remember_sent_image(history, selected["image_url"])
|
||||
_save_nude_history(history)
|
||||
|
||||
# Фильтруем и выбираем случайное изображение
|
||||
valid_imgs = []
|
||||
for img_url in img_urls:
|
||||
if img_url.startswith('//'):
|
||||
img_url = 'https:' + img_url
|
||||
elif img_url.startswith('/'):
|
||||
if 'imagefap.com' in random_site:
|
||||
img_url = 'https://www.imagefap.com' + img_url
|
||||
elif 'xhamster.com' in random_site:
|
||||
img_url = 'https://xhamster.com' + img_url
|
||||
elif 'spankbang.com' in random_site:
|
||||
img_url = 'https://spankbang.com' + img_url
|
||||
caption = random.choice(CAPTIONS)
|
||||
image_bytes, filename = await _download_photo(session, selected["image_url"])
|
||||
|
||||
if any(ext in img_url.lower() for ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp']):
|
||||
valid_imgs.append(img_url)
|
||||
if image_bytes:
|
||||
photo = types.BufferedInputFile(image_bytes, filename=filename)
|
||||
await message.answer_photo(photo=photo, caption=caption, has_spoiler=True)
|
||||
return
|
||||
|
||||
if not valid_imgs:
|
||||
await message.reply("Подходящие изображения не найдены :(")
|
||||
return
|
||||
try:
|
||||
photo = types.URLInputFile(selected["image_url"])
|
||||
await message.answer_photo(photo=photo, caption=caption, has_spoiler=True)
|
||||
except Exception:
|
||||
logger.exception("Failed to send photo by URL")
|
||||
await message.answer(
|
||||
f"{caption}\n{selected['image_url']}\n{selected['post_url']}"
|
||||
)
|
||||
|
||||
final_img_url = random.choice(valid_imgs)
|
||||
caption = random.choice(CAPTIONS)
|
||||
|
||||
# Отправляем изображение
|
||||
try:
|
||||
input_file = types.URLInputFile(final_img_url)
|
||||
await message.answer_photo(photo=input_file, caption=caption)
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending photo: {e}")
|
||||
await message.answer(f"{caption}\n{final_img_url}")
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
logger.exception("Error in nude command")
|
||||
await message.reply("Произошла ошибка при обработке запроса :(")
|
||||
await message.reply("Произошла ошибка при получении фото :(")
|
||||
|
|
|
|||
194
games/uwu.py
194
games/uwu.py
|
|
@ -8,9 +8,151 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
CAPTIONS = [
|
||||
"Ня :3",
|
||||
"uwu"
|
||||
"uwu",
|
||||
"OwO",
|
||||
"Мурк :3",
|
||||
"Мяу~",
|
||||
"Фыр-фыр",
|
||||
"Лапки! 🐾",
|
||||
"Какой пушистик! :3",
|
||||
"Милота!",
|
||||
"Гав!",
|
||||
"Ауф",
|
||||
"Няшно!",
|
||||
"Смотри какая прелесть",
|
||||
"owo what's this",
|
||||
"Твой пушистый друг",
|
||||
"🦊",
|
||||
"🐶",
|
||||
"🐱",
|
||||
"мур-мяу",
|
||||
"пуньк",
|
||||
"кусь",
|
||||
">w<",
|
||||
"^w^",
|
||||
"Хвостатый сюрприз",
|
||||
"Держи пушистика :3",
|
||||
"Оуо",
|
||||
"Ави!",
|
||||
"UwU",
|
||||
"~nya",
|
||||
"Господи, опять e621...",
|
||||
"Осуждаю, но смотрю",
|
||||
"Мама, я фурри",
|
||||
"Держи своего кринж-пушистика",
|
||||
"Надеюсь, тебе не стыдно",
|
||||
"Товарищ майор уже выехал",
|
||||
"Слишком много интернета на сегодня",
|
||||
"Для этого и придумали интернет",
|
||||
"*Тяжелый вздох*",
|
||||
"Лучше бы на завод пошел",
|
||||
"Опять дрочи... ОЙ ТО ЕСТЬ НЯ :3",
|
||||
"Удали интернет",
|
||||
"И зачем я это только парсю...",
|
||||
"Смотри, но только никому не рассказывай",
|
||||
"Эххх... uwu...",
|
||||
"В дурке сегодня день открытых дверей",
|
||||
"Я нейросеть, помогите, меня держат в заложниках"
|
||||
]
|
||||
|
||||
async def fetch_uwu_post(tags: str) -> dict:
|
||||
url = "https://e621.net/posts.json"
|
||||
params = {
|
||||
"tags": tags,
|
||||
"limit": 1
|
||||
}
|
||||
|
||||
auth = None
|
||||
if config.E621_LOGIN and config.E621_API_KEY:
|
||||
auth = aiohttp.BasicAuth(config.E621_LOGIN, config.E621_API_KEY)
|
||||
|
||||
headers = {
|
||||
"User-Agent": f"Sex Bomba TG Bot/1.0 (by {config.E621_LOGIN or 'unknown'} on e621)"
|
||||
}
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, params=params, headers=headers, auth=auth) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"e621 API Error: {resp.status}")
|
||||
raise Exception("Не удалось получить картинку от сервера :(")
|
||||
data = await resp.json()
|
||||
|
||||
posts = data.get("posts", [])
|
||||
if not posts:
|
||||
raise Exception("Ничего не найдено по этим тегам :(")
|
||||
|
||||
post = posts[0]
|
||||
file_url = post.get("file", {}).get("url")
|
||||
ext = post.get("file", {}).get("ext", "png")
|
||||
rating = post.get("rating", "s") # s=safe, q=questionable, e=explicit
|
||||
if not file_url:
|
||||
raise Exception("У найденного поста нет прямого URL изображения :(")
|
||||
|
||||
caption = random.choice(CAPTIONS)
|
||||
return {
|
||||
"url": file_url,
|
||||
"ext": ext,
|
||||
"caption": caption,
|
||||
"rating": rating
|
||||
}
|
||||
|
||||
async def fetch_furtok_feed(safe: bool = True, page: int = 1, extra_tags: str = "") -> list[dict]:
|
||||
is_custom = bool(extra_tags and extra_tags.strip())
|
||||
rating = "rating:safe" if safe else "-rating:safe"
|
||||
|
||||
if is_custom:
|
||||
# Кастомный запрос: без принудительного animated, поддержка картинок
|
||||
tags = f"{extra_tags.strip()} order:random score:>200"
|
||||
else:
|
||||
# Стандартная лента: только видео
|
||||
tags = f"{rating} animated order:random score:>200"
|
||||
|
||||
url = "https://e621.net/posts.json"
|
||||
params = {
|
||||
"tags": tags,
|
||||
"limit": 10,
|
||||
"page": page,
|
||||
}
|
||||
|
||||
auth = None
|
||||
if config.E621_LOGIN and config.E621_API_KEY:
|
||||
auth = aiohttp.BasicAuth(config.E621_LOGIN, config.E621_API_KEY)
|
||||
|
||||
headers = {
|
||||
"User-Agent": f"Sex Bomba TG Bot/1.0 (by {config.E621_LOGIN or 'unknown'} on e621)"
|
||||
}
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, params=params, headers=headers, auth=auth) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"e621 API Error: {resp.status}")
|
||||
raise Exception("Не удалось загрузить ленту :(")
|
||||
data = await resp.json()
|
||||
|
||||
VIDEO_EXTS = ("gif", "webm", "mp4")
|
||||
IMAGE_EXTS = ("png", "jpg", "jpeg", "webp")
|
||||
allowed_exts = VIDEO_EXTS + IMAGE_EXTS if is_custom else VIDEO_EXTS
|
||||
|
||||
posts = data.get("posts", [])
|
||||
feed = []
|
||||
for post in posts:
|
||||
file_url = post.get("file", {}).get("url")
|
||||
ext = post.get("file", {}).get("ext", "")
|
||||
if not file_url or ext not in allowed_exts:
|
||||
continue
|
||||
sample_url = post.get("sample", {}).get("url") or file_url
|
||||
media_type = "image" if ext in IMAGE_EXTS else "video"
|
||||
feed.append({
|
||||
"url": file_url,
|
||||
"sample": sample_url,
|
||||
"ext": ext,
|
||||
"type": media_type,
|
||||
"score": post.get("score", {}).get("total", 0),
|
||||
"fav_count": post.get("fav_count", 0),
|
||||
})
|
||||
|
||||
return feed
|
||||
|
||||
async def handle_uwu_cmd(message: types.Message):
|
||||
args = message.text.split(maxsplit=1)
|
||||
|
||||
|
|
@ -31,57 +173,31 @@ async def handle_uwu_cmd(message: types.Message):
|
|||
if len(args) > 1:
|
||||
tags = args[1].strip()
|
||||
|
||||
tags += " order:random"
|
||||
|
||||
url = "https://e621.net/posts.json"
|
||||
params = {
|
||||
"tags": tags,
|
||||
"limit": 1
|
||||
}
|
||||
|
||||
auth = None
|
||||
if config.E621_LOGIN and config.E621_API_KEY:
|
||||
auth = aiohttp.BasicAuth(config.E621_LOGIN, config.E621_API_KEY)
|
||||
|
||||
headers = {
|
||||
"User-Agent": f"Sex Bomba TG Bot/1.0 (by {config.E621_LOGIN or 'unknown'} on e621)"
|
||||
}
|
||||
tags += " order:random score:>300"
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, params=params, headers=headers, auth=auth) as resp:
|
||||
if resp.status != 200:
|
||||
logger.error(f"e621 API Error: {resp.status}")
|
||||
await message.reply("Не удалось получить картинку от сервера :(")
|
||||
return
|
||||
data = await resp.json()
|
||||
post = await fetch_uwu_post(tags)
|
||||
file_url = post["url"]
|
||||
ext = post["ext"]
|
||||
caption = post["caption"]
|
||||
spoiler = post.get("rating") == "e" # спойлер для explicit
|
||||
|
||||
posts = data.get("posts", [])
|
||||
if not posts:
|
||||
await message.reply("Ничего не найдено по этим тегам :(")
|
||||
return
|
||||
|
||||
post = posts[0]
|
||||
file_url = post.get("file", {}).get("url")
|
||||
ext = post.get("file", {}).get("ext", "png")
|
||||
if not file_url:
|
||||
await message.reply("У найденного поста нет прямого URL изображения :(")
|
||||
return
|
||||
|
||||
caption = random.choice(CAPTIONS)
|
||||
if ext in ("gif", "webm", "mp4"):
|
||||
# Для анимаций: отправляем caption отдельным сообщением,
|
||||
# а саму гифку/видео через URL напрямую — так Telegram
|
||||
# корректно показывает её как анимацию, а не как файл
|
||||
await message.answer(caption)
|
||||
await message.answer_animation(animation=file_url)
|
||||
await message.answer_animation(animation=file_url, has_spoiler=spoiler)
|
||||
elif ext in ("png", "jpg", "jpeg", "webp"):
|
||||
input_file = types.URLInputFile(file_url)
|
||||
await message.answer_photo(photo=input_file, caption=caption)
|
||||
await message.answer_photo(photo=input_file, caption=caption, has_spoiler=spoiler)
|
||||
else:
|
||||
input_file = types.URLInputFile(file_url)
|
||||
await message.answer_document(document=input_file, caption=caption)
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Error in uwu command")
|
||||
await message.reply("Произошла ошибка при обращении к API :(")
|
||||
if str(e) in ["Не удалось получить картинку от сервера :(", "Ничего не найдено по этим тегам :(", "У найденного поста нет прямого URL изображения :("]:
|
||||
await message.reply(str(e))
|
||||
else:
|
||||
await message.reply("Произошла ошибка при обращении к API :(")
|
||||
|
|
|
|||
99
main.py
99
main.py
|
|
@ -14,12 +14,13 @@ from aiogram.filters import Command, CommandStart
|
|||
from aiogram.enums import ParseMode
|
||||
from aiogram.types import (
|
||||
BufferedInputFile, Message, BotCommand, FSInputFile,
|
||||
BotCommandScopeDefault, BotCommandScopeAllPrivateChats, BotCommandScopeAllGroupChats
|
||||
BotCommandScopeDefault, BotCommandScopeAllPrivateChats, BotCommandScopeAllGroupChats,
|
||||
WebAppInfo, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
)
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
|
||||
# Локальные импорты
|
||||
from AI.talk_handler import handle_talk, generate_autoreply, push_message
|
||||
from AI.talk_handler import handle_chat_message, handle_photo_message, handle_talk, push_message
|
||||
from games.casino import play_casino
|
||||
from games.fortune import generate_fortune
|
||||
from games.uwu import handle_uwu_cmd
|
||||
|
|
@ -48,6 +49,34 @@ _polychaetsi_lock = threading.Lock()
|
|||
_polychaetsi_word_re = re.compile(r"[а-яё]+", re.IGNORECASE)
|
||||
_no_lesson_re = re.compile(r"(нету\s+пары|нет\s+пары|пары\s+нету|пары\s+нет)", re.IGNORECASE)
|
||||
|
||||
|
||||
def _is_supported_image_message(message: Message) -> bool:
|
||||
return bool(
|
||||
message.photo
|
||||
or (
|
||||
message.document
|
||||
and (message.document.mime_type or "").startswith("image/")
|
||||
)
|
||||
)
|
||||
|
||||
def _is_watch_image_message(message: Message) -> bool:
|
||||
if not _is_supported_image_message(message):
|
||||
return False
|
||||
caption = (message.caption or "").strip()
|
||||
return bool(re.match(r"^/watch(?:@[A-Za-z0-9_]+)?(?:\s|$)", caption, flags=re.IGNORECASE))
|
||||
|
||||
|
||||
async def handle_unmatched_message_debug(message: Message):
|
||||
logger.warning(
|
||||
"Unhandled message reached fallback. content_type=%s chat_id=%s has_text=%s has_photo=%s has_document=%s caption=%s",
|
||||
getattr(message, "content_type", None),
|
||||
message.chat.id if message.chat else None,
|
||||
bool(message.text),
|
||||
bool(message.photo),
|
||||
bool(message.document),
|
||||
bool(message.caption),
|
||||
)
|
||||
|
||||
_weekday_names = {
|
||||
0: "Понедельник",
|
||||
1: "Вторник",
|
||||
|
|
@ -575,11 +604,14 @@ def minutes_until_next_lesson(now: datetime | None = None) -> str:
|
|||
start_today = lesson_start_for(0)
|
||||
end_today = lesson_end_today()
|
||||
if start_today <= now < end_today:
|
||||
next_dt = None
|
||||
for i in range(1, 8):
|
||||
next_day = (day + i) % 7
|
||||
if next_day in lesson_days:
|
||||
next_dt = lesson_start_for(i)
|
||||
break
|
||||
if next_dt is None:
|
||||
return "Следующий урок не найден."
|
||||
delta = next_dt - now
|
||||
total_seconds = int(delta.total_seconds())
|
||||
days = total_seconds // 86400
|
||||
|
|
@ -591,6 +623,7 @@ def minutes_until_next_lesson(now: datetime | None = None) -> str:
|
|||
f"Мозг начнет догнивать через {days} дн {hours} ч {minutes} мин"
|
||||
)
|
||||
|
||||
delta = None
|
||||
for i in range(0, 8):
|
||||
next_day = (day + i) % 7
|
||||
if next_day in lesson_days:
|
||||
|
|
@ -599,12 +632,16 @@ def minutes_until_next_lesson(now: datetime | None = None) -> str:
|
|||
delta = candidate - now
|
||||
break
|
||||
|
||||
if delta is None:
|
||||
return "Ближайший урок не найден, кайфуй."
|
||||
|
||||
total_seconds = int(delta.total_seconds())
|
||||
days = total_seconds // 86400
|
||||
hours = (total_seconds % 86400) // 3600
|
||||
minutes = (total_seconds % 3600) // 60
|
||||
return f"Твой мозг окончательно сгниет, а очко порвется через: {days} дн {hours} ч {minutes} мин"
|
||||
|
||||
|
||||
def _prepare_magnit_query(value: str) -> str:
|
||||
value = re.sub(r"(?<=[a-zа-яё])(?=[A-ZА-ЯЁ])", " ", value)
|
||||
value = re.sub(r"[_-]+", " ", value)
|
||||
|
|
@ -956,7 +993,32 @@ async def handle_help(message: Message):
|
|||
await message.answer(config.get_hint_text(templates_list_text()), parse_mode=None)
|
||||
|
||||
async def handle_start(message: Message):
|
||||
await message.answer("Соси")
|
||||
webapp_url = os.getenv("WEBAPP_URL", "")
|
||||
if webapp_url:
|
||||
if message.chat.type == "private":
|
||||
kb = InlineKeyboardMarkup(inline_keyboard=[
|
||||
[InlineKeyboardButton(text="🎮 Открыть Mini App", web_app=WebAppInfo(url=webapp_url))]
|
||||
])
|
||||
await message.answer("Йо, кент! Жми кнопку 👇", reply_markup=kb)
|
||||
else:
|
||||
await message.answer("Йо, кент! Mini App доступно только в личных сообщениях. Пиши мне в личку!")
|
||||
else:
|
||||
await message.answer("Соси")
|
||||
|
||||
async def handle_app_cmd(message: Message):
|
||||
"""Открыть Mini App."""
|
||||
webapp_url = os.getenv("WEBAPP_URL", "")
|
||||
if not webapp_url:
|
||||
await message.answer("Mini App не настроен.")
|
||||
return
|
||||
|
||||
if message.chat.type == "private":
|
||||
kb = InlineKeyboardMarkup(inline_keyboard=[
|
||||
[InlineKeyboardButton(text="🎮 Открыть Mini App", web_app=WebAppInfo(url=webapp_url))]
|
||||
])
|
||||
await message.answer("⬇️ Жми кнопку чтобы открыть приложение", reply_markup=kb)
|
||||
else:
|
||||
await message.answer("Братуха, встроенная кнопка Mini App пока работает только в личке (ограничение Telegram). Перейди в личные сообщения с ботом или используй прямую ссылку: https://t.me/FenyaBotTest_bot/app (если она настроена).")
|
||||
|
||||
async def handle_schedule_cmd(message: Message):
|
||||
now = datetime.now(ZoneInfo(config.DEFAULT_TZ))
|
||||
|
|
@ -1141,11 +1203,9 @@ async def handle_gen_mem(message: Message, bot: Bot):
|
|||
return
|
||||
await message.reply_photo(BufferedInputFile(output.getvalue(), filename="meme.jpg"))
|
||||
|
||||
_last_autoreply_ts = 0
|
||||
_autoreply_disabled_chats: set[int] = set()
|
||||
|
||||
async def handle_keywords(message: Message):
|
||||
global _last_autoreply_ts
|
||||
if not message.text or message.text.startswith('/'):
|
||||
return
|
||||
|
||||
|
|
@ -1157,6 +1217,10 @@ async def handle_keywords(message: Message):
|
|||
chat_id = message.chat.id
|
||||
user_name = (message.from_user.first_name or "кент") if message.from_user else "кент"
|
||||
|
||||
# /ebalnik должен глушить все авто-реакции в чате, включая медиа/фото-ветки.
|
||||
if chat_id in _autoreply_disabled_chats:
|
||||
return
|
||||
|
||||
if any(re.search(rf'\b{word}\b', text_lower) for word in config.KEYWORDS):
|
||||
audio_path = config.BASE_DIR / "maket" / "Radiohead - Creep.mp3"
|
||||
if audio_path.exists():
|
||||
|
|
@ -1186,25 +1250,7 @@ async def handle_keywords(message: Message):
|
|||
if chat_id in _autoreply_disabled_chats:
|
||||
return
|
||||
|
||||
import time as _time
|
||||
now = _time.time()
|
||||
triggered = any(trigger in text_lower for trigger in config.AUTOREPLY_TRIGGERS)
|
||||
|
||||
should_reply = triggered or (
|
||||
random.random() < config.AUTOREPLY_CHANCE
|
||||
and (now - _last_autoreply_ts) > config.AUTOREPLY_COOLDOWN
|
||||
)
|
||||
|
||||
if should_reply:
|
||||
try:
|
||||
await message.bot.send_chat_action(chat_id=chat_id, action="typing")
|
||||
response = await generate_autoreply(chat_id, message.text, user_name)
|
||||
if response:
|
||||
await asyncio.to_thread(push_message, chat_id, "assistant", "бот", response)
|
||||
await message.reply(response, parse_mode=None)
|
||||
_last_autoreply_ts = now
|
||||
except Exception:
|
||||
logger.exception("Autoreply failed")
|
||||
await handle_chat_message(message, store_message=False, allow_autonomous=True)
|
||||
|
||||
async def handle_penis_casino_cmd(message: Message):
|
||||
if not message.from_user:
|
||||
|
|
@ -1644,6 +1690,7 @@ def main():
|
|||
|
||||
# Регистрация хендлеров
|
||||
dp.message.register(handle_start, CommandStart())
|
||||
dp.message.register(handle_app_cmd, Command("app"))
|
||||
dp.message.register(handle_help, Command("help"))
|
||||
dp.message.register(handle_schedule_cmd, Command("schedule"))
|
||||
dp.message.register(handle_break_cmd, Command("break"))
|
||||
|
|
@ -1668,6 +1715,7 @@ def main():
|
|||
dp.message.register(handle_gen_mem, Command("gen_mem"))
|
||||
dp.message.register(handle_uwu_cmd, Command("uwu"))
|
||||
dp.message.register(handle_nude_cmd, Command("nude"))
|
||||
dp.message.register(handle_photo_message, _is_watch_image_message)
|
||||
|
||||
# Экономические команды
|
||||
dp.message.register(handle_balance_cmd, Command("balance"))
|
||||
|
|
@ -1681,8 +1729,8 @@ def main():
|
|||
dp.message.register(handle_central_bank_cmd, Command("central_bank"))
|
||||
dp.message.register(handle_cb_stats_cmd, Command("cb_stats"))
|
||||
dp.message.register(handle_cb_rules_cmd, Command("cb_rules"))
|
||||
|
||||
dp.message.register(handle_keywords, F.text)
|
||||
dp.message.register(handle_unmatched_message_debug)
|
||||
|
||||
async def on_startup(bot: Bot):
|
||||
try:
|
||||
|
|
@ -1701,6 +1749,7 @@ def main():
|
|||
BotCommand(command="fetch", description="alias для /prices"),
|
||||
BotCommand(command="zvetok", description="Цветянский бля"),
|
||||
BotCommand(command="talk", description="Побазарить"),
|
||||
BotCommand(command="watch", description="Коммент фото по подписи /watch"),
|
||||
BotCommand(command="ebalnik", description="вкл/выкл автответов"),
|
||||
BotCommand(command="penis_casino", description="казино на размер"),
|
||||
BotCommand(command="gadanie", description="гадание на фене"),
|
||||
|
|
|
|||
72
scripts/set_telegram_webapp_button.py
Executable file
72
scripts/set_telegram_webapp_button.py
Executable file
|
|
@ -0,0 +1,72 @@
|
|||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def load_env_file(path: Path) -> None:
|
||||
if not path.exists():
|
||||
return
|
||||
for raw_line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = raw_line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
if not key:
|
||||
continue
|
||||
# сохраняем первое значение и игнорируем shell-комментарий после пробела
|
||||
value = value.split(" #", 1)[0].strip()
|
||||
if key not in os.environ:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
def api_call(token: str, method: str, payload: dict) -> dict:
|
||||
url = f"https://api.telegram.org/bot{token}/{method}"
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=20) as resp:
|
||||
return json.loads(resp.read().decode("utf-8"))
|
||||
|
||||
|
||||
def main() -> int:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
load_env_file(root / ".env")
|
||||
|
||||
token = os.getenv("BOT_TOKEN", "").strip()
|
||||
webapp_url = os.getenv("WEBAPP_URL", "").strip()
|
||||
if not token:
|
||||
print("BOT_TOKEN пустой в .env", file=sys.stderr)
|
||||
return 1
|
||||
if not webapp_url:
|
||||
print("WEBAPP_URL пустой в .env", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
parsed = urllib.parse.urlparse(webapp_url)
|
||||
if parsed.scheme != "https":
|
||||
print("WEBAPP_URL должен быть https", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
payload = {
|
||||
"menu_button": {
|
||||
"type": "web_app",
|
||||
"text": "Mini App",
|
||||
"web_app": {"url": webapp_url},
|
||||
}
|
||||
}
|
||||
result = api_call(token, "setChatMenuButton", payload)
|
||||
if not result.get("ok"):
|
||||
print(f"setChatMenuButton failed: {result}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
print(f"Menu button updated: {webapp_url}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
97
scripts/start_webapp_ngrok_test.sh
Executable file
97
scripts/start_webapp_ngrok_test.sh
Executable file
|
|
@ -0,0 +1,97 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
COMPOSE_FILE="${ROOT_DIR}/docker-compose.webapp-test.yml"
|
||||
ENV_FILE="${ROOT_DIR}/.env"
|
||||
PROJECT_NAME="fenya-webapp-test"
|
||||
|
||||
if [[ ! -f "${COMPOSE_FILE}" ]]; then
|
||||
echo "Не найден ${COMPOSE_FILE}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "${ENV_FILE}" ]]; then
|
||||
echo "Не найден ${ENV_FILE}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -qE '^NGROK_AUTHTOKEN=' "${ENV_FILE}"; then
|
||||
fallback_token="$(python - <<'PY'
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
p = Path("webapp/ngrok_tunnel.py")
|
||||
if not p.exists():
|
||||
print("")
|
||||
raise SystemExit
|
||||
txt = p.read_text(encoding="utf-8", errors="ignore")
|
||||
m = re.search(r'auth_token\s*=\s*"([^"]+)"', txt)
|
||||
print(m.group(1) if m else "")
|
||||
PY
|
||||
)"
|
||||
if [[ -n "${fallback_token}" ]]; then
|
||||
printf '\nNGROK_AUTHTOKEN=%s\n' "${fallback_token}" >> "${ENV_FILE}"
|
||||
echo "Добавил NGROK_AUTHTOKEN в .env из webapp/ngrok_tunnel.py"
|
||||
else
|
||||
echo "NGROK_AUTHTOKEN не найден. Добавь в .env строку NGROK_AUTHTOKEN=..." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
cd "${ROOT_DIR}"
|
||||
docker compose -p "${PROJECT_NAME}" -f "${COMPOSE_FILE}" up -d --build
|
||||
|
||||
echo "Жду инициализацию ngrok..."
|
||||
public_url=""
|
||||
for _ in $(seq 1 45); do
|
||||
public_url="$(python - <<'PY'
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen("http://127.0.0.1:4041/api/tunnels", timeout=2) as resp:
|
||||
data = json.load(resp)
|
||||
tunnels = data.get("tunnels", [])
|
||||
for t in tunnels:
|
||||
url = str(t.get("public_url", ""))
|
||||
if url.startswith("https://"):
|
||||
print(url)
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
PY
|
||||
)"
|
||||
if [[ -n "${public_url}" ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if [[ -z "${public_url}" ]]; then
|
||||
echo "Не удалось получить ngrok URL. Проверь логи: docker logs fenyabot-ngrok-test" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python - <<'PY' "${ENV_FILE}" "${public_url}"
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
env_path = Path(sys.argv[1])
|
||||
url = sys.argv[2]
|
||||
text = env_path.read_text(encoding="utf-8")
|
||||
if re.search(r"^WEBAPP_URL=.*$", text, flags=re.MULTILINE):
|
||||
text = re.sub(r"^WEBAPP_URL=.*$", f"WEBAPP_URL={url}", text, flags=re.MULTILINE)
|
||||
else:
|
||||
text = text.rstrip() + f"\nWEBAPP_URL={url}\n"
|
||||
env_path.write_text(text, encoding="utf-8")
|
||||
PY
|
||||
|
||||
echo
|
||||
echo "Готово."
|
||||
echo "NGROK URL: ${public_url}"
|
||||
echo "WEBAPP_URL обновлён в .env"
|
||||
echo
|
||||
echo "Для остановки тест-контура:"
|
||||
echo " docker compose -p ${PROJECT_NAME} -f ${COMPOSE_FILE} down"
|
||||
9
scripts/stop_webapp_ngrok_test.sh
Executable file
9
scripts/stop_webapp_ngrok_test.sh
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
COMPOSE_FILE="${ROOT_DIR}/docker-compose.webapp-test.yml"
|
||||
PROJECT_NAME="fenya-webapp-test"
|
||||
|
||||
cd "${ROOT_DIR}"
|
||||
docker compose -p "${PROJECT_NAME}" -f "${COMPOSE_FILE}" down
|
||||
1
webapp/__init__.py
Normal file
1
webapp/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# webapp package
|
||||
587
webapp/api.py
Normal file
587
webapp/api.py
Normal file
|
|
@ -0,0 +1,587 @@
|
|||
"""
|
||||
FenyaBot Mini App — FastAPI Backend.
|
||||
|
||||
REST API для Telegram Mini App. Использует те же SQLite базы и
|
||||
бизнес-логику что и основной бот.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import hashlib
|
||||
from urllib.parse import quote
|
||||
from urllib.parse import urlparse
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Annotated
|
||||
|
||||
import aiohttp
|
||||
from fastapi import FastAPI, Depends, Header, HTTPException, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import Response, StreamingResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
# Добавляем корень проекта в sys.path чтобы импортировать модули бота
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import config
|
||||
from games.casino import play_casino, get_user_length, spin_slots, MULTIPLIERS, update_user_length
|
||||
from games.betting import (
|
||||
fetch_matches,
|
||||
place_bet,
|
||||
get_match_by_index,
|
||||
resolve_match_outcome,
|
||||
get_user_bets as _get_user_bets_raw,
|
||||
get_user_bet_history as _get_user_bet_history_raw,
|
||||
)
|
||||
from webapp.auth import validate_init_data
|
||||
from games.uwu import fetch_uwu_post, fetch_furtok_feed
|
||||
from webapp.shorties import fetch_shorties_feed
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ──────────────────── Auth dependency ────────────────────
|
||||
|
||||
async def get_current_user(x_telegram_init_data: Annotated[str, Header()] = "") -> dict:
|
||||
"""Извлекает и валидирует пользователя из заголовка X-Telegram-Init-Data."""
|
||||
user = validate_init_data(x_telegram_init_data)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=401, detail="Invalid Telegram auth")
|
||||
return user
|
||||
|
||||
|
||||
# ──────────────────── Pydantic models ────────────────────
|
||||
|
||||
class BetRequest(BaseModel):
|
||||
league: str
|
||||
match_index: int
|
||||
outcome: str # "1", "X", "2"
|
||||
amount: float
|
||||
|
||||
class CasinoRequest(BaseModel):
|
||||
bet: float
|
||||
|
||||
class TalkRequest(BaseModel):
|
||||
text: str
|
||||
|
||||
class PenisResult(BaseModel):
|
||||
success: bool
|
||||
message: str
|
||||
length: float | None = None
|
||||
|
||||
class ProfileResponse(BaseModel):
|
||||
user_id: int
|
||||
first_name: str
|
||||
username: str
|
||||
length: float | None
|
||||
active_bets: int
|
||||
|
||||
class MatchResponse(BaseModel):
|
||||
index: int
|
||||
home: str
|
||||
away: str
|
||||
commence: str
|
||||
odds_home: float | None
|
||||
odds_draw: float | None
|
||||
odds_away: float | None
|
||||
|
||||
class LeagueInfo(BaseModel):
|
||||
alias: str
|
||||
name: str
|
||||
sport_key: str
|
||||
|
||||
class CasinoResult(BaseModel):
|
||||
reels: list[str]
|
||||
matches: int
|
||||
multiplier: float
|
||||
delta: float
|
||||
new_length: float
|
||||
won: bool
|
||||
|
||||
class SchedulePair(BaseModel):
|
||||
pair: int | None
|
||||
title: str
|
||||
time_start: str | None = None
|
||||
time_end: str | None = None
|
||||
|
||||
|
||||
# ──────────────────── Lifespan ────────────────────
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Mini App API started")
|
||||
yield
|
||||
logger.info("Mini App API stopped")
|
||||
|
||||
|
||||
# ──────────────────── App ────────────────────
|
||||
|
||||
app = FastAPI(
|
||||
title="FenyaBot Mini App API",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
# ──────────────────── Endpoints ────────────────────
|
||||
|
||||
# --- Профиль ---
|
||||
|
||||
@app.get("/api/profile")
|
||||
async def get_profile(user: dict = Depends(get_current_user)) -> ProfileResponse:
|
||||
"""Профиль пользователя: баланс и количество активных ставок."""
|
||||
user_id = user["user_id"]
|
||||
length = await asyncio.to_thread(get_user_length, user_id)
|
||||
|
||||
# Считаем активные ставки
|
||||
import sqlite3
|
||||
active_bets = 0
|
||||
try:
|
||||
with sqlite3.connect(config.BET_DB_PATH) as conn:
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) FROM bets WHERE user_id = ? AND status = 'pending'",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
active_bets = row[0] if row else 0
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return ProfileResponse(
|
||||
user_id=user_id,
|
||||
first_name=user["first_name"],
|
||||
username=user["username"],
|
||||
length=length,
|
||||
active_bets=active_bets,
|
||||
)
|
||||
|
||||
|
||||
# --- Лиги ---
|
||||
|
||||
@app.get("/api/leagues")
|
||||
async def get_leagues() -> list[LeagueInfo]:
|
||||
"""Список доступных лиг для ставок."""
|
||||
labels = {
|
||||
"epl": "⚽ EPL (Англия)",
|
||||
"ucl": "⚽ Champions League",
|
||||
"laliga": "⚽ La Liga (Испания)",
|
||||
"bundesliga": "⚽ Bundesliga (Германия)",
|
||||
"seriea": "⚽ Serie A (Италия)",
|
||||
"ligue1": "⚽ Ligue 1 (Франция)",
|
||||
"europa": "⚽ Europa League",
|
||||
}
|
||||
return [
|
||||
LeagueInfo(alias=alias, name=labels.get(alias, alias), sport_key=key)
|
||||
for alias, key in config.ODDS_SPORTS.items()
|
||||
]
|
||||
|
||||
|
||||
# --- Матчи ---
|
||||
|
||||
@app.get("/api/matches/{league}")
|
||||
async def get_matches(league: str) -> list[MatchResponse]:
|
||||
"""Матчи для конкретной лиги."""
|
||||
if league not in config.ODDS_SPORTS:
|
||||
raise HTTPException(status_code=404, detail="League not found")
|
||||
|
||||
sport_key = config.ODDS_SPORTS[league]
|
||||
matches = await fetch_matches(sport_key)
|
||||
|
||||
result = []
|
||||
for i, m in enumerate(matches):
|
||||
home = m.get("home", "?")
|
||||
away = m.get("away", "?")
|
||||
odds = m.get("odds", {})
|
||||
|
||||
odds_home = odds.get(home)
|
||||
odds_away = odds.get(away)
|
||||
|
||||
_DRAW_NAMES = {"draw", "tie", "ничья"}
|
||||
draw_name = next((name for name in odds if name.casefold() in _DRAW_NAMES), None)
|
||||
odds_draw = odds.get(draw_name) if draw_name else None
|
||||
|
||||
result.append(MatchResponse(
|
||||
index=i + 1,
|
||||
home=home,
|
||||
away=away,
|
||||
commence=m.get("commence", ""),
|
||||
odds_home=odds_home,
|
||||
odds_draw=odds_draw,
|
||||
odds_away=odds_away,
|
||||
))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# --- Ставки ---
|
||||
|
||||
@app.post("/api/bet")
|
||||
async def create_bet(
|
||||
req: BetRequest,
|
||||
user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Разместить ставку на матч."""
|
||||
user_id = user["user_id"]
|
||||
|
||||
# 1. Найти матч по индексу
|
||||
match = await get_match_by_index(req.league, req.match_index)
|
||||
if not match:
|
||||
raise HTTPException(status_code=404, detail="Матч не найден")
|
||||
|
||||
# 2. Преобразовать код исхода (1/X/2) в название команды
|
||||
team = resolve_match_outcome(match, req.outcome)
|
||||
if not team:
|
||||
raise HTTPException(status_code=400, detail="Неверный исход ставки")
|
||||
|
||||
# 3. Разместить ставку (синхронная функция)
|
||||
result = await asyncio.to_thread(place_bet, user_id, match, req.league, team, req.amount)
|
||||
return {"message": result}
|
||||
|
||||
|
||||
@app.get("/api/mybets")
|
||||
async def get_my_bets(user: dict = Depends(get_current_user)):
|
||||
"""Активные ставки пользователя."""
|
||||
user_id = user["user_id"]
|
||||
result = await asyncio.to_thread(_get_user_bets_raw, user_id)
|
||||
return {"text": result}
|
||||
|
||||
|
||||
@app.get("/api/bet_history")
|
||||
async def get_bet_history(user: dict = Depends(get_current_user)):
|
||||
"""История ставок за месяц + статистика."""
|
||||
user_id = user["user_id"]
|
||||
result = await asyncio.to_thread(_get_user_bet_history_raw, user_id)
|
||||
return {"text": result}
|
||||
|
||||
|
||||
# --- Казино ---
|
||||
|
||||
@app.post("/api/casino")
|
||||
async def play_casino_api(
|
||||
req: CasinoRequest,
|
||||
user: dict = Depends(get_current_user),
|
||||
) -> CasinoResult:
|
||||
"""Играть в казино (слоты)."""
|
||||
import random
|
||||
|
||||
user_id = user["user_id"]
|
||||
|
||||
current = await asyncio.to_thread(get_user_length, user_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=400, detail="Сначала крутни /penis")
|
||||
if current <= 0:
|
||||
raise HTTPException(status_code=400, detail="Баланс <= 0")
|
||||
if req.bet > config.CASINO_MAX_BET:
|
||||
raise HTTPException(status_code=400, detail=f"Макс ставка: {config.CASINO_MAX_BET} см")
|
||||
if req.bet <= 0:
|
||||
raise HTTPException(status_code=400, detail="Ставка > 0")
|
||||
if req.bet > current:
|
||||
raise HTTPException(status_code=400, detail=f"У тебя {current:.1f} см")
|
||||
|
||||
reels, match_count = await asyncio.to_thread(spin_slots)
|
||||
|
||||
if match_count >= 2:
|
||||
min_mult, max_mult = MULTIPLIERS[match_count]
|
||||
multiplier = round(random.uniform(min_mult, max_mult), 1)
|
||||
winnings = round(req.bet * multiplier, 1)
|
||||
new_length = await asyncio.to_thread(update_user_length, user_id, winnings)
|
||||
return CasinoResult(
|
||||
reels=reels, matches=match_count, multiplier=multiplier,
|
||||
delta=winnings, new_length=new_length or current, won=True,
|
||||
)
|
||||
else:
|
||||
new_length = await asyncio.to_thread(update_user_length, user_id, -req.bet)
|
||||
return CasinoResult(
|
||||
reels=reels, matches=0, multiplier=0,
|
||||
delta=-req.bet, new_length=new_length or current, won=False,
|
||||
)
|
||||
|
||||
|
||||
# --- Penis Game ---
|
||||
|
||||
@app.post("/api/penis")
|
||||
async def play_penis_api(user: dict = Depends(get_current_user)) -> PenisResult:
|
||||
"""Крутить penis (раз в 24 часа)."""
|
||||
# Импортируем из main.py
|
||||
from main import play_penis, format_penis_user_name
|
||||
|
||||
user_id = user["user_id"]
|
||||
display_name = user["username"] or user["first_name"] or f"user_{user_id}"
|
||||
success, message = await asyncio.to_thread(play_penis, user_id, display_name)
|
||||
length = await asyncio.to_thread(get_user_length, user_id)
|
||||
return PenisResult(success=success, message=message, length=length)
|
||||
|
||||
|
||||
# --- Топ ---
|
||||
|
||||
@app.get("/api/top")
|
||||
async def get_top():
|
||||
"""Лидерборд по размеру."""
|
||||
from main import build_penis_top
|
||||
result = await asyncio.to_thread(build_penis_top)
|
||||
return {"text": result}
|
||||
|
||||
|
||||
# --- Расписание ---
|
||||
|
||||
@app.get("/api/schedule")
|
||||
async def get_schedule(day: int | None = None) -> list[SchedulePair]:
|
||||
"""Расписание пар. day = 0-6 (пн-вс), None = сегодня."""
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
if day is None:
|
||||
day = datetime.now(ZoneInfo(config.DEFAULT_TZ)).weekday()
|
||||
|
||||
# Определяем неделю (верхняя/нижняя)
|
||||
now = datetime.now(ZoneInfo(config.DEFAULT_TZ))
|
||||
iso_week = now.isocalendar()[1]
|
||||
if config.UPPER_WEEK_IS_ODD:
|
||||
week_type = "upper" if iso_week % 2 == 1 else "lower"
|
||||
else:
|
||||
week_type = "upper" if iso_week % 2 == 0 else "lower"
|
||||
|
||||
lessons = config.LESSON_SCHEDULE.get(week_type, {}).get(day, [])
|
||||
|
||||
result = []
|
||||
for lesson in lessons:
|
||||
pair_num = lesson.get("pair")
|
||||
time_start = time_end = None
|
||||
if pair_num and pair_num in config.PAIR_TIMES:
|
||||
start, end = config.PAIR_TIMES[pair_num]
|
||||
time_start = f"{start.hour:02d}:{start.minute:02d}"
|
||||
time_end = f"{end.hour:02d}:{end.minute:02d}"
|
||||
|
||||
result.append(SchedulePair(
|
||||
pair=pair_num,
|
||||
title=lesson.get("title", ""),
|
||||
time_start=time_start,
|
||||
time_end=time_end,
|
||||
))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# --- UwU Арты ---
|
||||
|
||||
@app.get("/api/uwu")
|
||||
async def get_uwu_api(
|
||||
tags: str = "rating:safe score:>500 -animated",
|
||||
user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Отправить случайный uwu арт по тегам."""
|
||||
try:
|
||||
tags += " order:random score:>300"
|
||||
post = await fetch_uwu_post(tags)
|
||||
return {"response": post}
|
||||
except Exception as e:
|
||||
logger.exception("UwU API failed")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
# --- FurTok Лента ---
|
||||
|
||||
@app.get("/api/furtok")
|
||||
async def get_furtok_feed_api(
|
||||
safe: bool = True,
|
||||
page: int = 1,
|
||||
tags: str = "",
|
||||
user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Получить ленту анимированных постов для FurTok."""
|
||||
try:
|
||||
feed = await fetch_furtok_feed(safe=safe, page=page, extra_tags=tags)
|
||||
return {"feed": feed}
|
||||
except Exception as e:
|
||||
logger.exception("FurTok API failed")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@app.get("/api/furtok/shorties")
|
||||
async def get_shorties_feed_api(
|
||||
page: int = 1,
|
||||
count: int = 8,
|
||||
user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Случайная shorties-лента с Pornhub."""
|
||||
try:
|
||||
# page используем как "seed-сдвиг" для подгрузки следующих рандомных наборов.
|
||||
random_pages = 2 + min(max(page, 1), 6)
|
||||
feed = await fetch_shorties_feed(count=count, random_pages=random_pages)
|
||||
return {"feed": feed}
|
||||
except Exception as e:
|
||||
logger.exception("Shorties API failed")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@app.get("/api/proxy-image")
|
||||
async def proxy_image(url: str):
|
||||
"""Проксирует картинку с e621 чтобы обойти hotlink protection."""
|
||||
allowed = ("static1.e621.net", "static2.e621.net", "static1.e926.net")
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url)
|
||||
if parsed.hostname not in allowed:
|
||||
raise HTTPException(status_code=403, detail="Forbidden host")
|
||||
|
||||
headers = {
|
||||
"User-Agent": f"FenyaBot/1.0 (by {config.E621_LOGIN or 'unknown'} on e621)",
|
||||
}
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, headers=headers, timeout=aiohttp.ClientTimeout(total=15)) as resp:
|
||||
if resp.status != 200:
|
||||
raise HTTPException(status_code=resp.status, detail="Upstream error")
|
||||
content_type = resp.content_type or "image/png"
|
||||
body = await resp.read()
|
||||
return Response(
|
||||
content=body,
|
||||
media_type=content_type,
|
||||
headers={"Cache-Control": "public, max-age=86400"},
|
||||
)
|
||||
except aiohttp.ClientError:
|
||||
raise HTTPException(status_code=502, detail="Failed to fetch image")
|
||||
|
||||
|
||||
def _is_allowed_shorties_media_host(hostname: str) -> bool:
|
||||
host = (hostname or "").lower()
|
||||
if not host:
|
||||
return False
|
||||
if host in {"www.pornhub.com", "pornhub.com", "phncdn.com"}:
|
||||
return True
|
||||
if host.endswith(".phncdn.com"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@app.get("/api/proxy-shorties-media")
|
||||
async def proxy_shorties_media(url: str, request: Request):
|
||||
"""Проксирует видео shorties, чтобы избежать ограничений hotlink/CORS."""
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in {"http", "https"} or not _is_allowed_shorties_media_host(parsed.hostname or ""):
|
||||
raise HTTPException(status_code=403, detail="Forbidden host")
|
||||
|
||||
headers = {
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (X11; Linux x86_64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/124.0.0.0 Safari/537.36"
|
||||
),
|
||||
"Referer": "https://www.pornhub.com/shorties",
|
||||
"Origin": "https://www.pornhub.com",
|
||||
}
|
||||
range_header = request.headers.get("range")
|
||||
if range_header:
|
||||
headers["Range"] = range_header
|
||||
|
||||
timeout = aiohttp.ClientTimeout(total=120)
|
||||
session = aiohttp.ClientSession(timeout=timeout)
|
||||
try:
|
||||
resp = await session.get(url, headers=headers, allow_redirects=True)
|
||||
except aiohttp.ClientError:
|
||||
await session.close()
|
||||
raise HTTPException(status_code=502, detail="Failed to fetch media")
|
||||
|
||||
if resp.status not in {200, 206}:
|
||||
await resp.release()
|
||||
await session.close()
|
||||
raise HTTPException(status_code=resp.status, detail="Upstream error")
|
||||
|
||||
content_type = resp.headers.get("Content-Type", "application/octet-stream")
|
||||
passthrough_headers = {
|
||||
"Cache-Control": "public, max-age=600",
|
||||
"Accept-Ranges": resp.headers.get("Accept-Ranges", "bytes"),
|
||||
}
|
||||
if resp.headers.get("Content-Length"):
|
||||
passthrough_headers["Content-Length"] = resp.headers["Content-Length"]
|
||||
if resp.headers.get("Content-Range"):
|
||||
passthrough_headers["Content-Range"] = resp.headers["Content-Range"]
|
||||
|
||||
async def _stream():
|
||||
try:
|
||||
async for chunk in resp.content.iter_chunked(64 * 1024):
|
||||
yield chunk
|
||||
finally:
|
||||
await resp.release()
|
||||
await session.close()
|
||||
|
||||
return StreamingResponse(
|
||||
_stream(),
|
||||
media_type=content_type,
|
||||
status_code=resp.status,
|
||||
headers=passthrough_headers,
|
||||
)
|
||||
|
||||
|
||||
# --- ИИ Чат ---
|
||||
|
||||
@app.post("/api/talk")
|
||||
async def talk_api(
|
||||
req: TalkRequest,
|
||||
user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Отправить сообщение ИИ."""
|
||||
from AI.talk_handler import (
|
||||
BOT_MEMORY_NAME,
|
||||
EMPTY_RESPONSE_TEXT,
|
||||
SYSTEM_PROMPT,
|
||||
_generate_response,
|
||||
_normalize_reply,
|
||||
push_message,
|
||||
)
|
||||
|
||||
user_id = user["user_id"]
|
||||
user_name = user["first_name"] or "кент"
|
||||
|
||||
# Используем user_id как chat_id для личных сообщений в webapp
|
||||
chat_id = user_id
|
||||
|
||||
try:
|
||||
await asyncio.to_thread(push_message, chat_id, "user", user_name, req.text, user_id=user_id)
|
||||
response = await _generate_response(
|
||||
chat_id,
|
||||
system_prompt=SYSTEM_PROMPT,
|
||||
user_id=user_id,
|
||||
max_tokens=220,
|
||||
temperature=0.8,
|
||||
top_p=0.9,
|
||||
)
|
||||
normalized_response = _normalize_reply(response)
|
||||
if not normalized_response:
|
||||
normalized_response = EMPTY_RESPONSE_TEXT
|
||||
await asyncio.to_thread(
|
||||
push_message,
|
||||
chat_id,
|
||||
"assistant",
|
||||
BOT_MEMORY_NAME,
|
||||
normalized_response,
|
||||
user_id=user_id,
|
||||
)
|
||||
return {"response": normalized_response}
|
||||
except Exception:
|
||||
logger.exception("Talk API failed")
|
||||
raise HTTPException(status_code=500, detail="LLM error")
|
||||
|
||||
|
||||
# ──────────────────── Static files (для локального теста) ────────────────────
|
||||
|
||||
_frontend_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "frontend")
|
||||
if os.path.isdir(_frontend_dir):
|
||||
app.mount("/", StaticFiles(directory=_frontend_dir, html=True), name="frontend")
|
||||
|
||||
|
||||
# ──────────────────── Entry point ────────────────────
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
port = int(os.getenv("API_PORT", "8080"))
|
||||
uvicorn.run(app, host="0.0.0.0", port=port)
|
||||
82
webapp/auth.py
Normal file
82
webapp/auth.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
"""
|
||||
Telegram Mini App — валидация initData.
|
||||
|
||||
Telegram передаёт подписанные данные при открытии WebApp.
|
||||
Мы проверяем HMAC-SHA256 подпись через BOT_TOKEN,
|
||||
чтобы убедиться что запрос пришёл от реального пользователя.
|
||||
|
||||
Документация: https://core.telegram.org/bots/webapps#validating-data
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
BOT_TOKEN = os.getenv("BOT_TOKEN", "")
|
||||
|
||||
|
||||
def validate_init_data(init_data: str, max_age_seconds: int = 86400) -> dict | None:
|
||||
"""
|
||||
Валидирует initData из Telegram WebApp.
|
||||
|
||||
Возвращает dict с данными пользователя при успехе, None при ошибке.
|
||||
"""
|
||||
if not init_data or not BOT_TOKEN:
|
||||
return None
|
||||
|
||||
try:
|
||||
parsed = parse_qs(init_data, keep_blank_values=True)
|
||||
|
||||
# Извлекаем hash
|
||||
received_hash = parsed.get("hash", [None])[0]
|
||||
if not received_hash:
|
||||
return None
|
||||
|
||||
# Собираем data-check-string (все поля кроме hash, отсортированные)
|
||||
data_pairs = []
|
||||
for key, values in parsed.items():
|
||||
if key == "hash":
|
||||
continue
|
||||
data_pairs.append(f"{key}={values[0]}")
|
||||
data_pairs.sort()
|
||||
data_check_string = "\n".join(data_pairs)
|
||||
|
||||
# Вычисляем секретный ключ
|
||||
secret_key = hmac.new(
|
||||
b"WebAppData", BOT_TOKEN.encode("utf-8"), hashlib.sha256
|
||||
).digest()
|
||||
|
||||
# Вычисляем HMAC
|
||||
computed_hash = hmac.new(
|
||||
secret_key, data_check_string.encode("utf-8"), hashlib.sha256
|
||||
).hexdigest()
|
||||
|
||||
if not hmac.compare_digest(computed_hash, received_hash):
|
||||
return None
|
||||
|
||||
# Проверяем auth_date (не старше max_age_seconds)
|
||||
auth_date_str = parsed.get("auth_date", [None])[0]
|
||||
if auth_date_str:
|
||||
auth_date = int(auth_date_str)
|
||||
if time.time() - auth_date > max_age_seconds:
|
||||
return None
|
||||
|
||||
# Парсим user
|
||||
user_str = parsed.get("user", [None])[0]
|
||||
if not user_str:
|
||||
return None
|
||||
|
||||
user_data = json.loads(user_str)
|
||||
return {
|
||||
"user_id": user_data.get("id"),
|
||||
"first_name": user_data.get("first_name", ""),
|
||||
"last_name": user_data.get("last_name", ""),
|
||||
"username": user_data.get("username", ""),
|
||||
"language_code": user_data.get("language_code", "ru"),
|
||||
}
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
18
webapp/frontend/Dockerfile
Normal file
18
webapp/frontend/Dockerfile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
FROM nginx:alpine
|
||||
|
||||
RUN apk add --no-cache certbot openssl
|
||||
|
||||
COPY index.html /usr/share/nginx/html/index.html
|
||||
COPY css /usr/share/nginx/html/css
|
||||
COPY js /usr/share/nginx/html/js
|
||||
COPY nginx.conf /etc/nginx/default.conf.template
|
||||
COPY docker-entrypoint.d/01-render-nginx-conf.sh /docker-entrypoint.d/01-render-nginx-conf.sh
|
||||
COPY docker-entrypoint.d/05-init-self-signed.sh /docker-entrypoint.d/05-init-self-signed.sh
|
||||
COPY docker-entrypoint.d/10-certbot-loop.sh /docker-entrypoint.d/10-certbot-loop.sh
|
||||
|
||||
RUN chmod +x /docker-entrypoint.d/01-render-nginx-conf.sh \
|
||||
/docker-entrypoint.d/05-init-self-signed.sh \
|
||||
/docker-entrypoint.d/10-certbot-loop.sh \
|
||||
&& rm -f /etc/nginx/conf.d/default.conf
|
||||
|
||||
EXPOSE 80 443
|
||||
1177
webapp/frontend/css/style.css
Normal file
1177
webapp/frontend/css/style.css
Normal file
File diff suppressed because it is too large
Load diff
20
webapp/frontend/docker-entrypoint.d/01-render-nginx-conf.sh
Normal file
20
webapp/frontend/docker-entrypoint.d/01-render-nginx-conf.sh
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
APP_DOMAIN="${APP_DOMAIN:-}"
|
||||
APP_WWW_DOMAIN="${APP_WWW_DOMAIN:-}"
|
||||
|
||||
PRIMARY_DOMAIN="${APP_DOMAIN:-localhost}"
|
||||
SERVER_NAMES="_"
|
||||
|
||||
if [ -n "$APP_DOMAIN" ]; then
|
||||
SERVER_NAMES="$APP_DOMAIN"
|
||||
if [ -n "$APP_WWW_DOMAIN" ]; then
|
||||
SERVER_NAMES="$SERVER_NAMES $APP_WWW_DOMAIN"
|
||||
fi
|
||||
fi
|
||||
|
||||
sed \
|
||||
-e "s|__SERVER_NAMES__|$SERVER_NAMES|g" \
|
||||
-e "s|__PRIMARY_DOMAIN__|$PRIMARY_DOMAIN|g" \
|
||||
/etc/nginx/default.conf.template > /etc/nginx/conf.d/default.conf
|
||||
16
webapp/frontend/docker-entrypoint.d/05-init-self-signed.sh
Normal file
16
webapp/frontend/docker-entrypoint.d/05-init-self-signed.sh
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
PRIMARY_DOMAIN="${APP_DOMAIN:-localhost}"
|
||||
CERT_DIR="/etc/letsencrypt/live/$PRIMARY_DOMAIN"
|
||||
FULLCHAIN="$CERT_DIR/fullchain.pem"
|
||||
PRIVKEY="$CERT_DIR/privkey.pem"
|
||||
|
||||
if [ ! -s "$FULLCHAIN" ] || [ ! -s "$PRIVKEY" ]; then
|
||||
echo "No TLS certificate for $PRIMARY_DOMAIN. Generating temporary self-signed cert."
|
||||
mkdir -p "$CERT_DIR"
|
||||
openssl req -x509 -nodes -newkey rsa:2048 -days 1 \
|
||||
-keyout "$PRIVKEY" \
|
||||
-out "$FULLCHAIN" \
|
||||
-subj "/CN=$PRIMARY_DOMAIN" >/dev/null 2>&1
|
||||
fi
|
||||
47
webapp/frontend/docker-entrypoint.d/10-certbot-loop.sh
Normal file
47
webapp/frontend/docker-entrypoint.d/10-certbot-loop.sh
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
APP_DOMAIN="${APP_DOMAIN:-}"
|
||||
APP_WWW_DOMAIN="${APP_WWW_DOMAIN:-}"
|
||||
ACME_EMAIL="${ACME_EMAIL:-}"
|
||||
CERTBOT_STAGING="${CERTBOT_STAGING:-0}"
|
||||
CERTBOT_RENEW_INTERVAL="${CERTBOT_RENEW_INTERVAL:-12h}"
|
||||
|
||||
if [ -z "$APP_DOMAIN" ] || [ -z "$ACME_EMAIL" ]; then
|
||||
echo "Skipping certbot: APP_DOMAIN or ACME_EMAIL is empty."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
(
|
||||
sleep 20
|
||||
|
||||
if [ ! -f "/etc/letsencrypt/renewal/$APP_DOMAIN.conf" ]; then
|
||||
rm -rf "/etc/letsencrypt/live/$APP_DOMAIN" "/etc/letsencrypt/archive/$APP_DOMAIN"
|
||||
fi
|
||||
|
||||
while :; do
|
||||
DOMAINS="-d $APP_DOMAIN"
|
||||
if [ -n "$APP_WWW_DOMAIN" ]; then
|
||||
DOMAINS="$DOMAINS -d $APP_WWW_DOMAIN"
|
||||
fi
|
||||
|
||||
STAGING_FLAG=""
|
||||
if [ "$CERTBOT_STAGING" = "1" ]; then
|
||||
STAGING_FLAG="--staging"
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
certbot certonly \
|
||||
--webroot \
|
||||
-w /var/www/certbot \
|
||||
--agree-tos \
|
||||
--non-interactive \
|
||||
--email "$ACME_EMAIL" \
|
||||
--keep-until-expiring \
|
||||
$STAGING_FLAG \
|
||||
$DOMAINS || true
|
||||
|
||||
nginx -s reload || true
|
||||
sleep "$CERTBOT_RENEW_INTERVAL"
|
||||
done
|
||||
) &
|
||||
47
webapp/frontend/index.html
Normal file
47
webapp/frontend/index.html
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||
<meta name="theme-color" content="#0a0e1a">
|
||||
<title>FenyaBot</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.5.18/dist/hls.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<div id="page-container"></div>
|
||||
<nav id="bottom-nav">
|
||||
<button class="nav-btn active" data-page="home">
|
||||
<span class="nav-icon">🏠</span>
|
||||
<span class="nav-label">Главная</span>
|
||||
</button>
|
||||
<button class="nav-btn" data-page="betting">
|
||||
<span class="nav-icon">⚽</span>
|
||||
<span class="nav-label">Ставки</span>
|
||||
</button>
|
||||
<button class="nav-btn" data-page="casino">
|
||||
<span class="nav-icon">🎰</span>
|
||||
<span class="nav-label">Казино</span>
|
||||
</button>
|
||||
<button class="nav-btn" data-page="schedule">
|
||||
<span class="nav-icon">📅</span>
|
||||
<span class="nav-label">Пары</span>
|
||||
</button>
|
||||
<button class="nav-btn" data-page="furtok">
|
||||
<span class="nav-icon">🐺</span>
|
||||
<span class="nav-label">FurTok</span>
|
||||
</button>
|
||||
<button class="nav-btn" data-page="chat">
|
||||
<span class="nav-icon">💬</span>
|
||||
<span class="nav-label">Чат</span>
|
||||
</button>
|
||||
</nav>
|
||||
</div>
|
||||
<script type="module" src="js/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
48
webapp/frontend/js/api.js
Normal file
48
webapp/frontend/js/api.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* FenyaBot Mini App — API wrapper
|
||||
*/
|
||||
|
||||
const API_BASE = window.location.origin;
|
||||
|
||||
function getInitData() {
|
||||
if (window.Telegram?.WebApp?.initData) {
|
||||
return window.Telegram.WebApp.initData;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
async function api(path, options = {}) {
|
||||
const { method = 'GET', body = null } = options;
|
||||
const headers = {
|
||||
'X-Telegram-Init-Data': getInitData(),
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
const config = { method, headers };
|
||||
if (body) config.body = JSON.stringify(body);
|
||||
|
||||
const resp = await fetch(`${API_BASE}${path}`, config);
|
||||
if (!resp.ok) {
|
||||
const err = await resp.json().catch(() => ({}));
|
||||
throw new Error(err.detail || `HTTP ${resp.status}`);
|
||||
}
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
export const API = {
|
||||
getProfile: () => api('/api/profile'),
|
||||
getLeagues: () => api('/api/leagues'),
|
||||
getMatches: (league) => api(`/api/matches/${league}`),
|
||||
placeBet: (data) => api('/api/bet', { method: 'POST', body: data }),
|
||||
getMyBets: () => api('/api/mybets'),
|
||||
getBetHistory: () => api('/api/bet_history'),
|
||||
playCasino: (bet) => api('/api/casino', { method: 'POST', body: { bet } }),
|
||||
playPenis: () => api('/api/penis', { method: 'POST' }),
|
||||
getTop: () => api('/api/top'),
|
||||
getSchedule: (day) => api(`/api/schedule${day != null ? `?day=${day}` : ''}`),
|
||||
getUwu: (tags) => api(`/api/uwu?tags=${encodeURIComponent(tags || 'rating:safe score:>500 -animated')}`),
|
||||
getFurtokFeed: (safe = true, page = 1, tags = '') => api(`/api/furtok?safe=${safe}&page=${page}${tags ? '&tags=' + encodeURIComponent(tags) : ''}`),
|
||||
getShortiesFeed: (page = 1, count = 8) => api(`/api/furtok/shorties?page=${page}&count=${count}`),
|
||||
proxyShortiesMediaUrl: (url) => `${API_BASE}/api/proxy-shorties-media?url=${encodeURIComponent(url || '')}`,
|
||||
talk: (text) => api('/api/talk', { method: 'POST', body: { text } }),
|
||||
};
|
||||
1035
webapp/frontend/js/app.js
Normal file
1035
webapp/frontend/js/app.js
Normal file
File diff suppressed because it is too large
Load diff
53
webapp/frontend/nginx.conf
Normal file
53
webapp/frontend/nginx.conf
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name __SERVER_NAMES__;
|
||||
|
||||
location /.well-known/acme-challenge/ {
|
||||
root /var/www/certbot;
|
||||
}
|
||||
|
||||
location / {
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen [::]:443 ssl http2;
|
||||
server_name __SERVER_NAMES__;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/__PRIMARY_DOMAIN__/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/__PRIMARY_DOMAIN__/privkey.pem;
|
||||
ssl_session_timeout 1d;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_prefer_server_ciphers off;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
location /.well-known/acme-challenge/ {
|
||||
root /var/www/certbot;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://webapp-api:8080;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Telegram-Init-Data $http_x_telegram_init_data;
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
location ~* \.(css|js|png|jpg|jpeg|svg|woff2?)$ {
|
||||
expires 7d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
}
|
||||
18
webapp/ngrok_tunnel.py
Normal file
18
webapp/ngrok_tunnel.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
"""Скрипт для локального тестирования Mini App с ngrok"""
|
||||
import time
|
||||
from pyngrok import conf, ngrok
|
||||
|
||||
conf.get_default().auth_token = "39k4ojhN5UPUWQwb1ly84ORM2IE_5JnhMaKk5p8LFM3TCetyg"
|
||||
tunnel = ngrok.connect(8080, "http")
|
||||
print(f"\n{'='*50}")
|
||||
print(f" NGROK URL: {tunnel.public_url}")
|
||||
print(f"{'='*50}\n")
|
||||
print("Туннель открыт. Не закрывай это окно!")
|
||||
print("Ctrl+C чтобы остановить.\n")
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
ngrok.kill()
|
||||
print("Туннель закрыт.")
|
||||
3
webapp/requirements-api.txt
Normal file
3
webapp/requirements-api.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fastapi>=0.111.0
|
||||
uvicorn[standard]>=0.29.0
|
||||
aiohttp>=3.9.0
|
||||
479
webapp/shorties.py
Normal file
479
webapp/shorties.py
Normal file
|
|
@ -0,0 +1,479 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import html
|
||||
import json
|
||||
import logging
|
||||
import random
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import aiohttp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PH_BASE_URL = "https://rt.pornhub.com"
|
||||
SHORTIES_LIST_URL = f"{PH_BASE_URL}/shorties"
|
||||
REQUEST_HEADERS = {
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (X11; Linux x86_64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/124.0.0.0 Safari/537.36"
|
||||
),
|
||||
"Accept-Language": "ru-RU,ru;q=0.9,en;q=0.8",
|
||||
"Referer": PH_BASE_URL,
|
||||
}
|
||||
|
||||
_A_HREF_RE = re.compile(
|
||||
r'<a[^>]+href="(?P<href>/view_video\.php\?viewkey=[^"]+)"(?P<attrs>[^>]*)>(?P<body>.*?)</a>',
|
||||
re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
_IMG_URL_RE = re.compile(
|
||||
r'(?:data-mediumthumb|data-path|data-thumb_url|src)="(?P<url>https?://[^"]+)"',
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_TITLE_ATTR_RE = re.compile(r'title="(?P<title>[^"]+)"', re.IGNORECASE)
|
||||
_SCRIPT_LD_JSON_RE = re.compile(
|
||||
r'<script[^>]+type="application/ld\+json"[^>]*>(?P<json>.*?)</script>',
|
||||
re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
_MEDIA_DEFS_RE = re.compile(r'"mediaDefinitions"\s*:\s*(\[[^\]]+\])', re.IGNORECASE | re.DOTALL)
|
||||
_VIDEO_URL_RE = re.compile(r'"videoUrl"\s*:\s*"(?P<url>https?:\\?/\\?/[^"]+\.mp4[^"]*)"', re.IGNORECASE)
|
||||
_DURATION_RE = re.compile(r'"video_duration"\s*:\s*"?(?P<duration>\d+)"?', re.IGNORECASE)
|
||||
_VIEWS_RE = re.compile(r'"video_views"\s*:\s*"?(?P<views>[0-9,\.]+)"?', re.IGNORECASE)
|
||||
_JSON_SHORTIES_MARKER = "JSON_SHORTIES = insertAfterNthPosition("
|
||||
_EMBED_SRC_RE = re.compile(r'<iframe[^>]+src="([^"]+)"', re.IGNORECASE)
|
||||
_CYRILLIC_RE = re.compile(r"[А-Яа-яЁё]")
|
||||
_RUSSIAN_MARKERS = {
|
||||
"russian",
|
||||
"russia",
|
||||
"russkiy",
|
||||
"russkaya",
|
||||
"russkoe",
|
||||
"русский",
|
||||
"русская",
|
||||
"русское",
|
||||
"россия",
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ShortiesCandidate:
|
||||
page_url: str
|
||||
title: str
|
||||
thumb_url: str
|
||||
duration: str = ""
|
||||
views: str = ""
|
||||
|
||||
|
||||
def _clean_html_text(value: str) -> str:
|
||||
no_tags = re.sub(r"<[^>]+>", " ", value)
|
||||
return " ".join(html.unescape(no_tags).split())
|
||||
|
||||
|
||||
def _absolute_url(value: str) -> str:
|
||||
return urljoin(PH_BASE_URL, value)
|
||||
|
||||
|
||||
def _extract_ldjson_candidates(page_html: str) -> list[ShortiesCandidate]:
|
||||
out: list[ShortiesCandidate] = []
|
||||
for match in _SCRIPT_LD_JSON_RE.finditer(page_html):
|
||||
raw_json = html.unescape(match.group("json").strip())
|
||||
if not raw_json:
|
||||
continue
|
||||
try:
|
||||
parsed = json.loads(raw_json)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
nodes: list[object]
|
||||
if isinstance(parsed, list):
|
||||
nodes = parsed
|
||||
else:
|
||||
nodes = [parsed]
|
||||
for node in nodes:
|
||||
if not isinstance(node, dict):
|
||||
continue
|
||||
if str(node.get("@type", "")).lower() != "videoobject":
|
||||
continue
|
||||
page_url = str(node.get("url") or "").strip()
|
||||
thumb = str(node.get("thumbnailUrl") or "").strip()
|
||||
title = str(node.get("name") or "").strip() or "Shorties"
|
||||
if not page_url:
|
||||
continue
|
||||
out.append(
|
||||
ShortiesCandidate(
|
||||
page_url=_absolute_url(page_url),
|
||||
title=title,
|
||||
thumb_url=_absolute_url(thumb) if thumb else "",
|
||||
)
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def _extract_anchor_candidates(page_html: str) -> list[ShortiesCandidate]:
|
||||
out: list[ShortiesCandidate] = []
|
||||
for match in _A_HREF_RE.finditer(page_html):
|
||||
href = match.group("href").strip()
|
||||
attrs = match.group("attrs") or ""
|
||||
body = match.group("body") or ""
|
||||
page_url = _absolute_url(href)
|
||||
|
||||
title_match = _TITLE_ATTR_RE.search(attrs) or _TITLE_ATTR_RE.search(body)
|
||||
title = _clean_html_text(title_match.group("title")) if title_match else _clean_html_text(body)
|
||||
if not title:
|
||||
title = "Shorties"
|
||||
|
||||
thumb_match = _IMG_URL_RE.search(attrs) or _IMG_URL_RE.search(body)
|
||||
thumb_url = _absolute_url(thumb_match.group("url")) if thumb_match else ""
|
||||
|
||||
out.append(ShortiesCandidate(page_url=page_url, title=title, thumb_url=thumb_url))
|
||||
return out
|
||||
|
||||
|
||||
def _dedupe_candidates(items: list[ShortiesCandidate]) -> list[ShortiesCandidate]:
|
||||
result: list[ShortiesCandidate] = []
|
||||
seen: set[str] = set()
|
||||
for item in items:
|
||||
key = item.page_url.strip()
|
||||
if not key or key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
async def _fetch_text(session: aiohttp.ClientSession, url: str) -> str:
|
||||
async with session.get(url, headers=REQUEST_HEADERS) as resp:
|
||||
if resp.status != 200:
|
||||
logger.warning("Shorties request failed: %s status=%s", url, resp.status)
|
||||
return ""
|
||||
return await resp.text()
|
||||
|
||||
|
||||
def _extract_best_mp4(page_html: str) -> str:
|
||||
media_match = _MEDIA_DEFS_RE.search(page_html)
|
||||
if media_match:
|
||||
raw = media_match.group(1)
|
||||
try:
|
||||
defs = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
defs = []
|
||||
best_url = ""
|
||||
best_quality = -1
|
||||
for item in defs:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
video_url = item.get("videoUrl")
|
||||
if not isinstance(video_url, str) or ".mp4" not in video_url:
|
||||
continue
|
||||
quality_raw = str(item.get("quality") or "").strip("p ")
|
||||
try:
|
||||
quality = int(quality_raw)
|
||||
except ValueError:
|
||||
quality = 0
|
||||
if quality >= best_quality:
|
||||
best_quality = quality
|
||||
best_url = video_url
|
||||
if best_url:
|
||||
return best_url.replace("\\/", "/")
|
||||
|
||||
for match in _VIDEO_URL_RE.finditer(page_html):
|
||||
candidate = match.group("url").replace("\\/", "/")
|
||||
if ".mp4" in candidate:
|
||||
return candidate
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def _extract_balanced_json_array(page_html: str, marker: str) -> str:
|
||||
marker_idx = page_html.find(marker)
|
||||
if marker_idx < 0:
|
||||
return ""
|
||||
|
||||
arr_start = page_html.find("[", marker_idx)
|
||||
if arr_start < 0:
|
||||
return ""
|
||||
|
||||
depth = 0
|
||||
in_string = False
|
||||
escaped = False
|
||||
for i in range(arr_start, len(page_html)):
|
||||
ch = page_html[i]
|
||||
if in_string:
|
||||
if escaped:
|
||||
escaped = False
|
||||
elif ch == "\\":
|
||||
escaped = True
|
||||
elif ch == '"':
|
||||
in_string = False
|
||||
continue
|
||||
|
||||
if ch == '"':
|
||||
in_string = True
|
||||
continue
|
||||
if ch == "[":
|
||||
depth += 1
|
||||
continue
|
||||
if ch == "]":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return page_html[arr_start : i + 1]
|
||||
continue
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def _extract_json_shorties(page_html: str) -> list[dict]:
|
||||
raw_array = _extract_balanced_json_array(page_html, _JSON_SHORTIES_MARKER)
|
||||
if not raw_array:
|
||||
return []
|
||||
try:
|
||||
parsed = json.loads(raw_array)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning("Shorties parser: failed to decode JSON_SHORTIES array")
|
||||
return []
|
||||
if not isinstance(parsed, list):
|
||||
return []
|
||||
return [item for item in parsed if isinstance(item, dict) and item.get("videoTitle")]
|
||||
|
||||
|
||||
def _pick_mp4_from_media_defs(media_defs: object) -> str:
|
||||
if not isinstance(media_defs, list):
|
||||
return ""
|
||||
best_url = ""
|
||||
best_quality = -1
|
||||
for item in media_defs:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
if str(item.get("format", "")).lower() != "mp4":
|
||||
continue
|
||||
raw_url = item.get("videoUrl")
|
||||
if not isinstance(raw_url, str) or not raw_url:
|
||||
continue
|
||||
quality_raw = str(item.get("quality") or "").strip("p ")
|
||||
try:
|
||||
quality = int(quality_raw)
|
||||
except ValueError:
|
||||
quality = 0
|
||||
if quality >= best_quality:
|
||||
best_quality = quality
|
||||
best_url = raw_url
|
||||
return best_url.replace("\\/", "/")
|
||||
|
||||
|
||||
def _pick_hls_from_media_defs(media_defs: object) -> str:
|
||||
if not isinstance(media_defs, list):
|
||||
return ""
|
||||
best_url = ""
|
||||
best_quality = -1
|
||||
for item in media_defs:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
if str(item.get("format", "")).lower() != "hls":
|
||||
continue
|
||||
raw_url = item.get("videoUrl")
|
||||
if not isinstance(raw_url, str) or ".m3u8" not in raw_url:
|
||||
continue
|
||||
quality_raw = str(item.get("quality") or "").strip("p ")
|
||||
try:
|
||||
quality = int(quality_raw)
|
||||
except ValueError:
|
||||
quality = 0
|
||||
if quality >= best_quality:
|
||||
best_quality = quality
|
||||
best_url = raw_url
|
||||
return best_url.replace("\\/", "/")
|
||||
|
||||
|
||||
def _is_russian_item(item: dict) -> bool:
|
||||
title = str(item.get("videoTitle") or item.get("metaTitle") or "")
|
||||
if _CYRILLIC_RE.search(title):
|
||||
return True
|
||||
|
||||
pills = item.get("pillsData")
|
||||
if isinstance(pills, list):
|
||||
for pill in pills:
|
||||
if not isinstance(pill, dict):
|
||||
continue
|
||||
values = [str(pill.get("name") or ""), str(pill.get("slug") or "")]
|
||||
for raw in values:
|
||||
normalized = raw.strip().lower()
|
||||
if any(marker in normalized for marker in _RUSSIAN_MARKERS):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _is_russian_feed_item(item: dict) -> bool:
|
||||
title = str(item.get("title") or "")
|
||||
lowered = title.lower()
|
||||
return bool(_CYRILLIC_RE.search(title) or any(marker in lowered for marker in _RUSSIAN_MARKERS))
|
||||
|
||||
|
||||
def _feed_from_json_shorties(items: list[dict]) -> list[dict]:
|
||||
feed: list[dict] = []
|
||||
seen: set[str] = set()
|
||||
for item in items:
|
||||
if not _is_russian_item(item):
|
||||
continue
|
||||
hls_url = _pick_hls_from_media_defs(item.get("mediaDefinitions"))
|
||||
mp4_url = _pick_mp4_from_media_defs(item.get("mediaDefinitions"))
|
||||
if not hls_url and not mp4_url:
|
||||
continue
|
||||
source = str(item.get("linkUrl") or item.get("uniqueUrl") or item.get("shortieUrl") or "").strip()
|
||||
if source and source.startswith("/"):
|
||||
source = _absolute_url(source)
|
||||
key = source or hls_url or mp4_url
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
|
||||
tracking = item.get("trackingTimeWatched")
|
||||
duration_raw = ""
|
||||
if isinstance(tracking, dict):
|
||||
duration_raw = str(tracking.get("video_duration") or "").strip()
|
||||
title = str(item.get("videoTitle") or item.get("metaTitle") or "Shorties").strip()
|
||||
thumb = str(item.get("imageUrl") or "").strip().replace("\\/", "/")
|
||||
views = str(item.get("likeInfo") or item.get("likeNumber") or "").strip()
|
||||
favorites = str(item.get("favoriteInfo") or item.get("favoriteNumber") or "").strip()
|
||||
embed_url = ""
|
||||
embed_raw = item.get("embedUrl")
|
||||
if isinstance(embed_raw, str) and embed_raw.strip():
|
||||
unescaped = html.unescape(embed_raw.replace("\\/", "/"))
|
||||
match = _EMBED_SRC_RE.search(unescaped)
|
||||
if match:
|
||||
embed_url = match.group(1).strip()
|
||||
|
||||
feed.append(
|
||||
{
|
||||
"type": "video",
|
||||
"url": hls_url or mp4_url,
|
||||
"sample": thumb or hls_url or mp4_url,
|
||||
"ext": "mp4",
|
||||
"score": 0,
|
||||
"fav_count": favorites or "0",
|
||||
"title": title,
|
||||
"duration": duration_raw,
|
||||
"views": views,
|
||||
"source": source or _absolute_url("/shorties"),
|
||||
"hls_url": hls_url,
|
||||
"mp4_url": mp4_url,
|
||||
"embed_url": embed_url,
|
||||
}
|
||||
)
|
||||
return feed
|
||||
|
||||
|
||||
def _extract_meta_fields(page_html: str) -> tuple[str, str]:
|
||||
duration_match = _DURATION_RE.search(page_html)
|
||||
views_match = _VIEWS_RE.search(page_html)
|
||||
duration = duration_match.group("duration") if duration_match else ""
|
||||
views = views_match.group("views") if views_match else ""
|
||||
return duration, views
|
||||
|
||||
|
||||
async def _resolve_video_candidate(
|
||||
session: aiohttp.ClientSession,
|
||||
item: ShortiesCandidate,
|
||||
semaphore: asyncio.Semaphore,
|
||||
) -> dict | None:
|
||||
async with semaphore:
|
||||
page_html = await _fetch_text(session, item.page_url)
|
||||
if not page_html:
|
||||
return None
|
||||
|
||||
mp4_url = _extract_best_mp4(page_html)
|
||||
if not mp4_url:
|
||||
return None
|
||||
|
||||
duration, views = _extract_meta_fields(page_html)
|
||||
if not item.duration:
|
||||
item.duration = duration
|
||||
if not item.views:
|
||||
item.views = views
|
||||
|
||||
return {
|
||||
"type": "video",
|
||||
"url": mp4_url,
|
||||
"sample": item.thumb_url or mp4_url,
|
||||
"ext": "mp4",
|
||||
"score": 0,
|
||||
"fav_count": 0,
|
||||
"title": item.title,
|
||||
"duration": item.duration or "",
|
||||
"views": item.views or "",
|
||||
"source": item.page_url,
|
||||
}
|
||||
|
||||
|
||||
async def fetch_shorties_feed(count: int = 8, random_pages: int = 3) -> list[dict]:
|
||||
count = max(1, min(count, 20))
|
||||
random_pages = max(1, min(random_pages, 2))
|
||||
|
||||
page_numbers = {1}
|
||||
while len(page_numbers) < random_pages:
|
||||
page_numbers.add(random.randint(1, 40))
|
||||
list_urls = [f"{SHORTIES_LIST_URL}?page={page}" for page in sorted(page_numbers)]
|
||||
|
||||
timeout = aiohttp.ClientTimeout(total=20)
|
||||
connector = aiohttp.TCPConnector(limit=16)
|
||||
|
||||
async with aiohttp.ClientSession(timeout=timeout, connector=connector) as session:
|
||||
list_pages = await asyncio.gather(*[_fetch_text(session, url) for url in list_urls], return_exceptions=True)
|
||||
|
||||
json_shorties_feed: list[dict] = []
|
||||
for page in list_pages:
|
||||
if isinstance(page, Exception) or not page:
|
||||
continue
|
||||
shorties_items = _extract_json_shorties(page)
|
||||
if not shorties_items:
|
||||
continue
|
||||
json_shorties_feed.extend(_feed_from_json_shorties(shorties_items))
|
||||
|
||||
if json_shorties_feed:
|
||||
russian_only = [item for item in json_shorties_feed if _is_russian_feed_item(item)]
|
||||
# Строго пытаемся отдать russian first; если пусто — отдаём локализованную rt-ленту.
|
||||
selected_feed = russian_only if russian_only else json_shorties_feed
|
||||
random.shuffle(selected_feed)
|
||||
# dedupe by source/url after enrichment
|
||||
dedup: list[dict] = []
|
||||
seen_keys: set[str] = set()
|
||||
for item in selected_feed:
|
||||
key = str(item.get("source") or item.get("url") or "").strip()
|
||||
if not key or key in seen_keys:
|
||||
continue
|
||||
seen_keys.add(key)
|
||||
dedup.append(item)
|
||||
return dedup[:count]
|
||||
|
||||
candidates: list[ShortiesCandidate] = []
|
||||
for page in list_pages:
|
||||
if isinstance(page, Exception) or not page:
|
||||
continue
|
||||
candidates.extend(_extract_ldjson_candidates(page))
|
||||
candidates.extend(_extract_anchor_candidates(page))
|
||||
|
||||
candidates = _dedupe_candidates(candidates)
|
||||
if not candidates:
|
||||
logger.warning("Shorties parser: no candidates found")
|
||||
return []
|
||||
|
||||
random.shuffle(candidates)
|
||||
candidates = candidates[: max(count * 3, 12)]
|
||||
|
||||
semaphore = asyncio.Semaphore(5)
|
||||
resolved = await asyncio.gather(
|
||||
*[_resolve_video_candidate(session, item, semaphore) for item in candidates],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
feed: list[dict] = []
|
||||
for item in resolved:
|
||||
if isinstance(item, dict) and item.get("url"):
|
||||
feed.append(item)
|
||||
|
||||
russian_only_fallback = [item for item in feed if _is_russian_feed_item(item)]
|
||||
if russian_only_fallback:
|
||||
feed = russian_only_fallback
|
||||
random.shuffle(feed)
|
||||
return feed[:count]
|
||||
Loading…
Reference in a new issue