resize images before sending

This commit is contained in:
2023-03-30 20:50:09 +03:00
parent 8729994bf8
commit 02e5eeb7d5
3 changed files with 115 additions and 8 deletions

23
main.py
View File

@@ -2,6 +2,7 @@ import asyncio
import logging
import os
from io import BytesIO
from PIL import Image
import httpx
import redis.asyncio as aioredis
@@ -44,9 +45,10 @@ async def check_updates():
try:
logging.warning(post.file.url)
async with httpx.AsyncClient() as client:
io = BytesIO()
file = io.write((await client.get(post.file.url)).content)
file = BytesIO()
file.write((await client.get(post.file.url)).content)
file.name = f'file.{post.file.ext}'
file.seek(0)
if post.file.ext == 'webm':
await bot.send_video(int(os.environ['SEND_CHAT']),
file,
@@ -54,9 +56,22 @@ async def check_updates():
height=post.file.height,
thumb=post.preview.url,
caption=caption)
else:
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,
caption=caption)
elif post.file.ext in ('png', 'jpg'):
file_resized = BytesIO()
im = Image.open(file)
im.thumbnail((2048, 2048), Image.ANTIALIAS)
im.save(file_resized, format='JPEG')
im.name = 'file.jpg'
im.seek(0)
await bot.send_photo(int(os.environ['SEND_CHAT']),
file,
im,
caption=caption)
await redis.sadd('e621:sent', post.id)
except exceptions.TelegramAPIError as e: