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

10
main.py
View File

@ -1,7 +1,9 @@
import asyncio import asyncio
import logging import logging
import os import os
from io import BytesIO
import httpx
import redis.asyncio as aioredis import redis.asyncio as aioredis
from aiogram import Bot, Dispatcher from aiogram import Bot, Dispatcher
from aiogram.types import Message from aiogram.types import Message
@ -41,16 +43,20 @@ async def check_updates():
if post.file.url: if post.file.url:
try: try:
logging.warning(post.file.url) logging.warning(post.file.url)
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': if post.file.ext == 'webm':
await bot.send_video(int(os.environ['SEND_CHAT']), await bot.send_video(int(os.environ['SEND_CHAT']),
post.file.url, file,
width=post.file.width, width=post.file.width,
height=post.file.height, height=post.file.height,
thumb=post.preview.url, thumb=post.preview.url,
caption=caption) caption=caption)
else: else:
await bot.send_photo(int(os.environ['SEND_CHAT']), await bot.send_photo(int(os.environ['SEND_CHAT']),
post.file.url, file,
caption=caption) caption=caption)
await redis.sadd('e621:sent', post.id) await redis.sadd('e621:sent', post.id)
except exceptions.TelegramAPIError as e: except exceptions.TelegramAPIError as e: