diff --git a/bot.py b/bot.py index 8a21c1b..3420454 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,6 @@ import os import logging +import re import httpx from aiogram import Bot, Dispatcher, types from aiogram.filters import Command, CommandObject @@ -15,97 +16,126 @@ logger = logging.getLogger(__name__) bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML)) dp = Dispatcher() +# Словари для красоты +LANG_FLAGS = {"en": "🇺🇸", "ja": "🇯🇵", "ru": "🇷🇺", "zh": "🇨🇳", "ko": "🇰🇷", "fr": "🇫🇷", "de": "🇩🇪"} +PLATFORM_ICONS = {"win": "🖥️", "swi": "🎮", "ps4": "🟦", "and": "📱", "ios": "🍏"} + +def clean_text(text: str) -> str: + """Очистка описаний от специфических тегов VNDB [url], [b] и т.д.""" + if not text: return "No description available." + text = re.sub(r'\[url=?.*?\](.*?)\[/url\]', r'\1', text) + text = text.replace("[b]", "").replace("[/b]", "") + text = text.replace("[i]", "").replace("[/i]", "") + return text[:600] + "..." if len(text) > 600 else text + async def fetch_vndb(endpoint: str, filters: list, fields: str): payload = {"filters": filters, "fields": fields} - async with httpx.AsyncClient(timeout=10.0) as client: + async with httpx.AsyncClient(timeout=15.0) as client: try: response = await client.post(f"{API_URL}/{endpoint}", json=payload) if response.status_code == 200: return response.json().get("results", []) - logger.error(f"VNDB Error {response.status_code} on {endpoint}: {response.text}") + logger.error(f"VNDB Error {response.status_code}: {response.text}") return None except Exception as e: logger.error(f"Request failed: {e}") return None -@dp.message(Command("start", "help")) -async def cmd_help(message: types.Message): - await message.answer( - "🤖 VNDB Bot\n\n" - "• /vn <id или название>\n" - "• /char <id или название>\n" - "• /release <id или название>" - ) - @dp.message(Command("vn")) async def handle_vn(message: types.Message, command: CommandObject): - if not command.args: return await message.answer("Введите название или ID (v17)") + if not command.args: return await message.answer("Usage: /vn ") - # Если это ID (начинается с 'v' + цифры) - if command.args.startswith('v') and command.args[1:].isdigit(): - filt = ["id", "=", command.args] - else: - filt = ["search", "=", command.args] - - # Важно: для VN используем alttitle вместо original - res = await fetch_vndb("vn", filt, "id, title, alttitle, released, rating, votecount") + filt = ["id", "=", command.args] if command.args.startswith('v') and command.args[1:].isdigit() else ["search", "=", command.args] + # Запрашиваем расширенные поля + fields = "id, title, alttitle, released, rating, votecount, description, languages, platforms, developers{name}" + res = await fetch_vndb("vn", filt, fields) - if not res: return await message.answer("❌ Ничего не найдено.") + if not res: return await message.answer("❌ Not found.") if len(res) > 1 and not command.args.startswith('v'): - out = ["🔍 Результаты поиска:"] - for i in res[:10]: - out.append(f"• {i['title']} ({i['id']})") + out = ["🔍 Multiple results:"] + for i in res[:10]: out.append(f"• {i['title']} ({i['id']})") return await message.answer("\n".join(out)) v = res[0] - rating = f"{v['rating']/10} ⭐" if v.get('rating') else "N/A" - text = (f"📖 {v['title']}\n" - f"Original: {v.get('alttitle', 'N/A')}\n" - f"Released: {v.get('released', 'N/A')}\n" - f"Rating: {rating} ({v.get('votecount', 0)} votes)\n" - f"https://vndb.org/{v['id']}") + langs = " ".join([LANG_FLAGS.get(l, l.upper()) for l in v.get('languages', [])]) + plats = " ".join([PLATFORM_ICONS.get(p, p.upper()) for p in v.get('platforms', [])]) + devs = ", ".join([d['name'] for d in v.get('developers', [])]) + + text = ( + f"📖 {v['title']}\n" + f"{v.get('alttitle', '')}\n\n" + f"🗓 Released: {v.get('released', 'N/A')}\n" + f"⭐ Rating: {v['rating']/10 if v.get('rating') else 'N/A'} ({v.get('votecount', 0)} votes)\n" + f"🏢 Developer: {devs or 'N/A'}\n" + f"🌍 Langs: {langs}\n" + f"🎮 Platforms: {plats}\n\n" + f"📝 Description:\n{clean_text(v.get('description'))}\n\n" + f"🔗 https://vndb.org/{v['id']}" + ) await message.answer(text) @dp.message(Command("char")) async def handle_char(message: types.Message, command: CommandObject): - if not command.args: return await message.answer("Введите имя или ID (c1)") + if not command.args: return await message.answer("Usage: /char ") filt = ["id", "=", command.args] if command.args.startswith('c') and command.args[1:].isdigit() else ["search", "=", command.args] + fields = "id, name, original, description, gender, age, blood_type" + res = await fetch_vndb("character", filt, fields) - # Для персонажей original работает - res = await fetch_vndb("character", filt, "id, name, original") - if not res: return await message.answer("❌ Не найдено.") + if not res: return await message.answer("❌ Not found.") if len(res) > 1 and not command.args.startswith('c'): - out = ["👤 Персонажи:"] + out = ["👤 Multiple results:"] for i in res[:10]: out.append(f"• {i['name']} ({i['id']})") return await message.answer("\n".join(out)) c = res[0] - await message.answer(f"👤 {c['name']}\nOriginal: {c.get('original', 'N/A')}\nhttps://vndb.org/{c['id']}") + gender = {"m": "Male ♂️", "f": "Female ♀️", "both": "Both 🚻"}.get(c.get('gender'), 'Unknown') + + text = ( + f"👤 {c['name']} ({c.get('original', '')})\n\n" + f"⚧ Gender: {gender}\n" + f"🎂 Age: {c.get('age') or 'Unknown'}\n" + f"🩸 Blood Type: {c.get('blood_type') or 'N/A'}\n\n" + f"📝 Description:\n{clean_text(c.get('description'))}\n\n" + f"🔗 https://vndb.org/{c['id']}" + ) + await message.answer(text) @dp.message(Command("release")) async def handle_rel(message: types.Message, command: CommandObject): - if not command.args: return await message.answer("Введите название или ID (r1)") + if not command.args: return await message.answer("Usage: /release ") filt = ["id", "=", command.args] if command.args.startswith('r') and command.args[1:].isdigit() else ["search", "=", command.args] + fields = "id, title, alttitle, released, languages, platforms, extlinks{url, label}" + res = await fetch_vndb("release", filt, fields) - res = await fetch_vndb("release", filt, "id, title, alttitle, released") - if not res: return await message.answer("❌ Не найдено.") + if not res: return await message.answer("❌ Not found.") if len(res) > 1 and not command.args.startswith('r'): - out = ["💿 Релизы:"] + out = ["💿 Multiple results:"] for i in res[:10]: out.append(f"• {i['title']} ({i['id']})") return await message.answer("\n".join(out)) r = res[0] - await message.answer(f"💿 {r['title']}\nReleased: {r.get('released', 'N/A')}\nhttps://vndb.org/{r['id']}") + links = "\n".join([f"🔗 {l['label']}" for l in r.get('extlinks', [])[:5]]) + + text = ( + f"💿 {r['title']}\n" + f"{r.get('alttitle', '')}\n\n" + f"🗓 Released: {r.get('released', 'N/A')}\n" + f"🌍 Langs: {' '.join([LANG_FLAGS.get(l, l.upper()) for l in r.get('languages', [])])}\n\n" + f"Links & Stores:\n{links or 'No links available'}\n\n" + f"🔗 https://vndb.org/{r['id']}" + ) + await message.answer(text, disable_web_page_preview=True) -# Прямые команды поиска (алиасы для удобства) -@dp.message(Command("search")) -async def search_alias(message: types.Message, command: CommandObject): - await handle_vn(message, command) +@dp.message(Command("start", "help", "search")) +async def cmd_start(message: types.Message, command: CommandObject): + if message.text.startswith("/search") and command.args: + return await handle_vn(message, command) + await message.answer("🤖 VNDB Bot\n/vn [id/name]\n/char [id/name]\n/release [id/name]") if __name__ == "__main__": dp.run_polling(bot) \ No newline at end of file