fix tag count limit

This commit is contained in:
bakatrouble 2023-03-24 17:25:28 +03:00
parent 547132d5d6
commit 3bee7c0bc2

63
main.py
View File

@ -23,37 +23,38 @@ dp = Dispatcher(bot)
async def check_updates():
async with redis.lock('e621:update'):
tag_list = [t.decode() for t in await redis.smembers('e621:subs')]
tags = ' '.join(f'~{tag}' for tag in tag_list)
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)
caption = f'Monitored tags: {" ".join(monitored_tags)}\n' \
f'Artist: {" ".join(post.tags.artist)}\n' \
f'Character: {" ".join(post.tags.character)}\n\n' \
f'https://e621.net/posts/{post.id}'
if post.file.url:
try:
logging.warning(post.file.url)
if post.file.ext == 'webm':
await bot.send_video(int(os.environ['SEND_CHAT']),
post.file.url,
width=post.file.width,
height=post.file.height,
thumb=post.preview.url,
caption=caption)
else:
await bot.send_photo(int(os.environ['SEND_CHAT']),
post.file.url,
caption=caption)
await redis.sadd('e621:sent', post.id)
except exceptions.TelegramAPIError as e:
logging.exception(e)
for tl_idx in range(0, len(tag_list), 40):
tags = ' '.join(f'~{tag}' for tag in tag_list[tl_idx: tl_idx + 40])
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)
caption = f'Monitored tags: {" ".join(monitored_tags)}\n' \
f'Artist: {" ".join(post.tags.artist)}\n' \
f'Character: {" ".join(post.tags.character)}\n\n' \
f'https://e621.net/posts/{post.id}'
if post.file.url:
try:
logging.warning(post.file.url)
if post.file.ext == 'webm':
await bot.send_video(int(os.environ['SEND_CHAT']),
post.file.url,
width=post.file.width,
height=post.file.height,
thumb=post.preview.url,
caption=caption)
else:
await bot.send_photo(int(os.environ['SEND_CHAT']),
post.file.url,
caption=caption)
await redis.sadd('e621:sent', post.id)
except exceptions.TelegramAPIError as e:
logging.exception(e)
@dp.message_handler(commands=['add'])