import os
import logging
import re
import httpx
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command, CommandObject
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
TOKEN = os.getenv("TELEGRAM_TOKEN")
API_URL = os.getenv("VNDB_API_URL", "https://api.vndb.org/kana")
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()
def clean_text(text: str) -> str:
"""Очистка текста от VNDB-тегов и ограничение длины."""
if not text: return "No description available."
# Убираем [url] и другие теги
text = re.sub(r'\[url=?.*?\](.*?)\[/url\]', r'\1', text)
text = re.sub(r'\[/?b\]', '', text)
text = re.sub(r'\[/?i\]', '', text)
text = text.replace('"', "'")
return text[:700] + "..." if len(text) > 700 else text
async def fetch_vndb(endpoint: str, filters: list, fields: str):
payload = {"filters": filters, "fields": fields}
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}: {response.text}")
return None
except Exception as e:
logger.error(f"Connection error: {e}")
return None
@dp.message(Command("vn"))
async def handle_vn(message: types.Message, command: CommandObject):
if not command.args: return await message.answer("Example: /vn Steins Gate")
is_id = command.args.startswith('v') and command.args[1:].isdigit()
filt = ["id", "=", command.args] if is_id 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("❌ Not found.")
# Если поиск выдал список, а не конкретный ID
if len(res) > 1 and not is_id:
out = ["🔍 Select VN by ID:"]
for i in res[:10]:
out.append(f"• {i['title']} — {i['id']}")
return await message.answer("\n".join(out))
v = res[0]
langs = ", ".join([l.upper() for l in v.get('languages', [])])
plats = ", ".join([p.upper() for p in v.get('platforms', [])])
devs = ", ".join([d['name'] for d in v.get('developers', [])])
text = (
f"TITLE: {v['title']}\n"
f"ORIGINAL: {v.get('alttitle') or 'N/A'}\n\n"
f"RELEASED: {v.get('released', 'N/A')}\n"
f"DEVELOPER: {devs or 'N/A'}\n"
f"RATING: {v['rating']/10 if v.get('rating') else 'N/A'} ({v.get('votecount', 0)} votes)\n"
f"LANGUAGES: {langs or 'N/A'}\n"
f"PLATFORMS: {plats or 'N/A'}\n\n"
f"DESCRIPTION:\n{clean_text(v.get('description'))}\n\n"
f"VNDB LINK: 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("Example: /char Kurisu")
is_id = command.args.startswith('c') and command.args[1:].isdigit()
filt = ["id", "=", command.args] if is_id else ["search", "=", command.args]
fields = "id, name, original, description, gender, age, blood_type"
res = await fetch_vndb("character", filt, fields)
if not res: return await message.answer("❌ Not found.")
if len(res) > 1 and not is_id:
out = ["👤 Select Character by ID:"]
for i in res[:10]: out.append(f"• {i['name']} — {i['id']}")
return await message.answer("\n".join(out))
c = res[0]
gender = {"m": "MALE", "f": "FEMALE", "both": "BOTH"}.get(c.get('gender'), 'UNKNOWN')
text = (
f"NAME: {c['name']}\n"
f"ORIGINAL: {c.get('original') or 'N/A'}\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"VNDB LINK: 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("Example: /release Steins Gate")
is_id = command.args.startswith('r') and command.args[1:].isdigit()
filt = ["id", "=", command.args] if is_id else ["search", "=", command.args]
fields = "id, title, alttitle, released, languages, platforms, extlinks{url, label}"
res = await fetch_vndb("release", filt, fields)
if not res: return await message.answer("❌ Not found.")
if len(res) > 1 and not is_id:
out = ["💿 Select Release by ID:"]
for i in res[:10]: out.append(f"• {i['title']} — {i['id']}")
return await message.answer("\n".join(out))
r = res[0]
langs = ", ".join([l.upper() for l in r.get('languages', [])])
plats = ", ".join([p.upper() for p in r.get('platforms', [])])
# Ограничиваем ссылки, чтобы не спамить
links = "\n".join([f"• {l['label']}" for l in r.get('extlinks', [])[:8]])
text = (
f"RELEASE TITLE: {r['title']}\n"
f"ORIGINAL: {r.get('alttitle') or 'N/A'}\n\n"
f"DATE: {r.get('released', 'N/A')}\n"
f"LANGUAGES: {langs}\n"
f"PLATFORMS: {plats}\n\n"
f"LINKS / STORES:\n{links or 'No links available'}\n\n"
f"VNDB LINK: https://vndb.org/{r['id']}"
)
await message.answer(text, disable_web_page_preview=True)
@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 Technical Bot\n\n"
"Commands:\n"
"/vn [name/id]\n"
"/char [name/id]\n"
"/release [name/id]"
)
if __name__ == "__main__":
dp.run_polling(bot)