import aiohttp
from aiogram import types
import config
import logging
logger = logging.getLogger(__name__)
async def handle_uwu_cmd(message: types.Message):
args = message.text.split(maxsplit=1)
if len(args) > 1 and args[1].strip() == "-h":
help_msg = (
"🐈 Справка по тегам e621:\n\n"
"Перечислите любые теги через пробел после команды.\n"
"• rating: rating:safe (безопасные), rating:questionable, rating:explicit (NSFW)\n"
"• score: score:>500 (искать популярные посты с рейтингом больше 500)\n"
"• Исключить тег: поставьте минус, например -fox\n\n"
"Пример: /uwu wolf -rating:explicit score:>100\n"
"По умолчанию: rating:safe score:>500"
)
await message.reply(help_msg, parse_mode="HTML")
return
tags = "rating:safe score:>500"
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)"
}
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()
posts = data.get("posts", [])
if not posts:
await message.reply("Ничего не найдено по этим тегам :(")
return
post = posts[0]
file_url = post.get("file", {}).get("url")
if not file_url:
await message.reply("У найденного поста нет прямого URL изображения :(")
return
await message.answer_photo(photo=types.URLInputFile(file_url), caption="Ня :3")
except Exception as e:
logger.exception("Error in uwu command")
await message.reply("Произошла ошибка при обращении к API :(")