download files instead of passing by link

This commit is contained in:
bakatrouble 2023-03-30 20:41:55 +03:00
parent 3bee7c0bc2
commit 8729994bf8

28
main.py
View File

@ -1,7 +1,9 @@
import asyncio
import logging
import os
from io import BytesIO
import httpx
import redis.asyncio as aioredis
from aiogram import Bot, Dispatcher
from aiogram.types import Message
@ -41,17 +43,21 @@ async def check_updates():
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)
async with httpx.AsyncClient() as client:
io = BytesIO()
file = io.write((await client.get(post.file.url)).content)
file.name = f'file.{post.file.ext}'
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,
caption=caption)
else:
await bot.send_photo(int(os.environ['SEND_CHAT']),
file,
caption=caption)
await redis.sadd('e621:sent', post.id)
except exceptions.TelegramAPIError as e:
logging.exception(e)