forked from zovos/bot_tg
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
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)
|
||
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 :(")
|