2023-03-22 15:07:42 +00:00
|
|
|
import asyncio
|
2023-03-22 16:10:44 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2023-03-30 17:41:55 +00:00
|
|
|
from io import BytesIO
|
2023-03-30 17:50:09 +00:00
|
|
|
from PIL import Image
|
2023-03-22 15:07:42 +00:00
|
|
|
|
2023-03-30 17:41:55 +00:00
|
|
|
import httpx
|
2023-03-22 15:07:42 +00:00
|
|
|
import redis.asyncio as aioredis
|
|
|
|
from aiogram import Bot, Dispatcher
|
2023-03-31 10:18:11 +00:00
|
|
|
from aiogram.types import Message, ParseMode
|
2023-03-22 16:28:33 +00:00
|
|
|
from aiogram.utils import executor, exceptions
|
2023-03-22 16:10:44 +00:00
|
|
|
import dotenv
|
2023-03-22 15:07:42 +00:00
|
|
|
|
|
|
|
from e621 import E621
|
|
|
|
|
2023-03-22 16:10:44 +00:00
|
|
|
dotenv.load_dotenv('.env')
|
|
|
|
|
2023-03-22 15:07:42 +00:00
|
|
|
redis = aioredis.from_url('redis://localhost')
|
|
|
|
e621 = E621()
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2023-03-22 16:10:44 +00:00
|
|
|
bot = Bot(token=os.environ['BOT_TOKEN'])
|
2023-03-22 15:07:42 +00:00
|
|
|
dp = Dispatcher(bot)
|
|
|
|
|
|
|
|
|
|
|
|
async def check_updates():
|
2023-03-30 20:00:56 +00:00
|
|
|
logging.warning('Waiting for lock...')
|
2023-03-22 15:07:42 +00:00
|
|
|
async with redis.lock('e621:update'):
|
2023-03-30 20:00:56 +00:00
|
|
|
logging.warning('Lock acquired...')
|
2023-03-22 15:07:42 +00:00
|
|
|
tag_list = [t.decode() for t in await redis.smembers('e621:subs')]
|
2023-03-30 19:18:03 +00:00
|
|
|
tag_list.sort()
|
2023-03-24 14:25:28 +00:00
|
|
|
for tl_idx in range(0, len(tag_list), 40):
|
|
|
|
tags = ' '.join(f'~{tag}' for tag in tag_list[tl_idx: tl_idx + 40])
|
2023-03-30 20:00:05 +00:00
|
|
|
logging.warning(tags)
|
2023-03-24 14:25:28 +00:00
|
|
|
posts = await e621.get_posts(tags)
|
|
|
|
if not posts:
|
|
|
|
return
|
|
|
|
already_sent = await redis.smismember('e621:sent', [p.id for p in posts])
|
|
|
|
for i in range(len(posts)):
|
|
|
|
if already_sent[i]:
|
|
|
|
continue
|
|
|
|
post = posts[i]
|
|
|
|
monitored_tags = set(post.tags.flatten()) & set(tag_list)
|
2023-03-31 10:18:11 +00:00
|
|
|
artist_tags = post.tags.artist
|
|
|
|
character_tags = post.tags.character
|
|
|
|
copyright_tags = post.tags.copyright
|
|
|
|
caption = '\n'.join(l for l in [
|
|
|
|
f'Monitored tags: <b>{" ".join(monitored_tags)}</b>',
|
|
|
|
artist_tags and f'Artist: <b>{" ".join(artist_tags)}</b>',
|
|
|
|
character_tags and f'Character: <b>{", ".join(character_tags)}</b>',
|
|
|
|
copyright_tags and f'Copyright: <b>{", ".join(copyright_tags)}</b>',
|
|
|
|
f'\nhttps://e621.net/posts/{post.id}'
|
|
|
|
] if l)
|
2023-03-24 14:25:28 +00:00
|
|
|
if post.file.url:
|
|
|
|
try:
|
|
|
|
logging.warning(post.file.url)
|
2023-03-30 17:41:55 +00:00
|
|
|
async with httpx.AsyncClient() as client:
|
2023-03-30 17:50:09 +00:00
|
|
|
file = BytesIO()
|
|
|
|
file.write((await client.get(post.file.url)).content)
|
2023-03-30 17:41:55 +00:00
|
|
|
file.name = f'file.{post.file.ext}'
|
2023-03-30 17:50:09 +00:00
|
|
|
file.seek(0)
|
2023-03-30 17:41:55 +00:00
|
|
|
if post.file.ext == 'webm':
|
|
|
|
await bot.send_video(int(os.environ['SEND_CHAT']),
|
|
|
|
file,
|
|
|
|
width=post.file.width,
|
|
|
|
height=post.file.height,
|
|
|
|
thumb=post.preview.url,
|
2023-03-31 10:18:11 +00:00
|
|
|
caption=caption,
|
|
|
|
parse_mode=ParseMode.HTML)
|
2023-03-30 17:50:09 +00:00
|
|
|
elif post.file.ext == 'gif':
|
|
|
|
await bot.send_animation(int(os.environ['SEND_CHAT']),
|
|
|
|
file,
|
|
|
|
width=post.file.width,
|
|
|
|
height=post.file.height,
|
|
|
|
thumb=post.preview.url,
|
2023-03-31 10:18:11 +00:00
|
|
|
caption=caption,
|
|
|
|
parse_mode=ParseMode.HTML)
|
2023-03-30 17:50:09 +00:00
|
|
|
elif post.file.ext in ('png', 'jpg'):
|
2023-03-30 17:53:02 +00:00
|
|
|
if post.file.size > 10000000:
|
2023-03-30 19:15:27 +00:00
|
|
|
logging.warning('compressing')
|
2023-03-30 18:00:45 +00:00
|
|
|
dl_im = Image.open(file).convert('RGBA')
|
2023-03-30 19:20:36 +00:00
|
|
|
if dl_im.size[0] > 2000 or dl_im.size[1] > 2000:
|
|
|
|
larger_dimension = max(dl_im.size)
|
|
|
|
ratio = 2000 / larger_dimension
|
|
|
|
dl_im = dl_im.resize((int(dl_im.size[0] * ratio), int(dl_im.size[1] * ratio)),
|
|
|
|
Image.LANCZOS)
|
2023-03-30 18:00:45 +00:00
|
|
|
im = Image.new('RGBA', dl_im.size, (255, 255, 255))
|
|
|
|
composite = Image.alpha_composite(im, dl_im).convert('RGB')
|
2023-03-30 19:21:45 +00:00
|
|
|
file = BytesIO()
|
2023-03-30 18:00:45 +00:00
|
|
|
composite.save(file, format='JPEG')
|
|
|
|
file.seek(0)
|
2023-03-30 18:04:23 +00:00
|
|
|
file.name = 'file.jpg'
|
2023-03-30 17:41:55 +00:00
|
|
|
await bot.send_photo(int(os.environ['SEND_CHAT']),
|
2023-03-30 17:53:25 +00:00
|
|
|
file,
|
2023-03-31 10:18:11 +00:00
|
|
|
caption=caption,
|
|
|
|
parse_mode=ParseMode.HTML)
|
2023-03-24 14:25:28 +00:00
|
|
|
await redis.sadd('e621:sent', post.id)
|
|
|
|
except exceptions.TelegramAPIError as e:
|
|
|
|
logging.exception(e)
|
2023-03-22 15:07:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dp.message_handler(commands=['add'])
|
|
|
|
async def add_tag(msg: Message):
|
|
|
|
args = msg.get_args()
|
|
|
|
if not args:
|
|
|
|
await msg.reply('Please provide tag to subscribe to')
|
|
|
|
return
|
|
|
|
for tag in args.split():
|
|
|
|
await redis.sadd('e621:subs', tag)
|
|
|
|
await msg.reply(f'Tags {args} added')
|
|
|
|
|
|
|
|
|
|
|
|
@dp.message_handler(regexp=r'^\/del_\S+$')
|
|
|
|
async def del_tag(msg: Message):
|
|
|
|
args = msg.text[5:]
|
|
|
|
if not args:
|
|
|
|
await msg.reply('Please provide tag to subscribe to')
|
|
|
|
return
|
|
|
|
if ' ' in args:
|
|
|
|
await msg.reply('Tag should not contain spaces')
|
|
|
|
return
|
|
|
|
if not await redis.sismember('e621:subs', args):
|
|
|
|
await msg.reply('Tag not found')
|
|
|
|
return
|
|
|
|
await redis.srem('e621:subs', args)
|
2023-03-31 10:18:11 +00:00
|
|
|
await msg.reply(f'Tag {args} removed')
|
2023-03-22 15:07:42 +00:00
|
|
|
|
|
|
|
|
2023-04-10 00:58:54 +00:00
|
|
|
@dp.message_handler(commands=['del'])
|
|
|
|
async def del_command(msg: Message):
|
|
|
|
args = msg.get_args()
|
|
|
|
if not args:
|
|
|
|
await msg.reply('Please provide tag to subscribe to')
|
|
|
|
return
|
|
|
|
for tag in args.split():
|
|
|
|
await redis.srem('e621:subs', tag)
|
|
|
|
await msg.reply(f'Tags {args} removed')
|
|
|
|
|
|
|
|
|
2023-03-22 15:07:42 +00:00
|
|
|
@dp.message_handler(commands=['list'])
|
|
|
|
async def list_tags(msg: Message):
|
|
|
|
tags = [t.decode() for t in await redis.smembers('e621:subs')]
|
2023-03-30 20:12:00 +00:00
|
|
|
tags.sort()
|
2023-03-22 15:07:42 +00:00
|
|
|
lines = []
|
|
|
|
for tag in tags:
|
2023-04-10 00:55:29 +00:00
|
|
|
entry = f'- {tag} [/del_{tag}]'
|
|
|
|
if len('\n'.join(lines + [entry])) > 4096:
|
|
|
|
lines = "\n".join(lines)
|
|
|
|
await msg.reply(f'Monitored tags:\n\n{lines}')
|
|
|
|
lines = [entry]
|
|
|
|
else:
|
|
|
|
lines.append(f'- {tag} [/del_{tag}]')
|
|
|
|
|
2023-03-22 15:07:42 +00:00
|
|
|
lines = "\n".join(lines)
|
|
|
|
await msg.reply(f'Monitored tags:\n\n{lines}')
|
|
|
|
|
|
|
|
|
|
|
|
@dp.message_handler(commands=['update'])
|
|
|
|
async def update(msg: Message):
|
|
|
|
await check_updates()
|
|
|
|
|
|
|
|
|
|
|
|
async def background_on_start():
|
|
|
|
while True:
|
2023-03-30 19:38:14 +00:00
|
|
|
logging.warning('Checking updates...')
|
2023-03-30 20:08:02 +00:00
|
|
|
try:
|
|
|
|
await check_updates()
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
2023-03-30 19:59:26 +00:00
|
|
|
logging.warning('Sleeping...')
|
2023-03-22 15:07:42 +00:00
|
|
|
await asyncio.sleep(60)
|
|
|
|
|
|
|
|
|
|
|
|
async def on_bot_startup(dp: Dispatcher):
|
|
|
|
asyncio.create_task(background_on_start())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
executor.start_polling(dp, on_startup=on_bot_startup)
|