Обновление загрузчика аватарок

This commit is contained in:
Whyverum
2024-12-23 22:07:12 +07:00
parent 2e75264af9
commit dc6c70cf77
4 changed files with 50 additions and 55 deletions

View File

@@ -1,32 +1,29 @@
# BotCode/routers/downloads/download_chat_avatar.py
# Функция скачивания аватара чата
import os
import requests
from aiogram import Router
from aiogram.types import Chat
import aiohttp
from BotLibrary import *
# Создание роутера и настройка экспорта модулей
__all__ = ("router", "download_chat_avatar",)
router = Router(name="avatar_chat_router")
__all__ = ("download_chat_avatar",)
log_type = "AvatarChat"
# Функция закачки аватарок чатов
async def download_chat_avatar(message, chat: Chat):
async def download_chat_avatar(message):
try:
chat = message.chat
# Проверка типа чата (группа или супергруппа)
if chat.type in ["group", "supergroup"]:
# Получаем информацию о чате (включая фото)
chat_info = await bot.get_chat(chat.id)
chat_id = chat.id
# Проверка наличия аватара
# Проверка на наличие фотографий чата
if not chat_info.photo:
text_error = f"Чат с ID {chat_id} не имеет аватара."
logger.bind(log_type=log_type, user=chat_id).error(text_error)
text_error = f"Чат с ID {chat.id} не имеет аватара."
logger.bind(log_type=log_type, user=chat.id).error(text_error)
return text_error
# Получаем file_id для фото (высокое качество приоритетно)
file_id = (
getattr(chat_info.photo, 'big_file_id', None) or
getattr(chat_info.photo, 'medium_file_id', None) or
@@ -34,39 +31,41 @@ async def download_chat_avatar(message, chat: Chat):
)
if not file_id:
text_error = f"Не удалось получить file_id аватара чата с ID {chat_id}."
logger.bind(log_type=log_type, user=chat_id).error(text_error)
text_error = f"Не удалось получить file_id аватара чата с ID {chat.id}."
logger.bind(log_type=log_type, user=chat.id).error(text_error)
return text_error
# Получаем file_info для фото
file_info = await bot.get_file(file_id)
# Строим URL для скачивания файла
file_url = f"https://api.telegram.org/file/bot{bot.token}/{file_info.file_path}"
# Формируем путь для сохранения фото
chat_id = find_chat_id(message)
save_dir = f"{ImportantPath.chat_avatar}/{chat_id}"
chat_id = find_imp_id(chat.id)
save_dir = f"{ProjectPath.chat_avatar}/{chat_id}"
os.makedirs(save_dir, exist_ok=True)
file_extension = os.path.splitext(file_info.file_path)[-1]
file_name = f"avatar{file_extension}"
save_path = os.path.join(save_dir, file_name)
# Скачиваем аватар
response = requests.get(file_url, stream=True)
if response.status_code == 200:
with open(save_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
return f"Фото аватара чата с ID {chat_id} успешно скачано."
else:
text_error = f"Не удалось скачать фото аватара чата с ID {chat_id}. Статус: {response.status_code}"
logger.bind(log_type=log_type, user=chat_id).error(text_error)
return text_error
# Асинхронное скачивание
async with aiohttp.ClientSession() as session:
async with session.get(file_url) as response:
if response.status == 200:
with open(save_path, "wb") as file:
while True:
chunk = await response.content.read(8192)
if not chunk:
break
file.write(chunk)
return f"Фото аватара чата с ID {chat_id} успешно скачано."
else:
text_error = f"Не удалось скачать фото аватара чата с ID {chat_id}. Статус: {response.status}"
logger.bind(log_type=log_type, user=chat.id).error(text_error)
return text_error
else:
return
except Exception as e:
chat = message.chat
text_error = f"Ошибка при скачивании фото аватара чата с ID {chat.id}: {e}"
logger.bind(log_type=log_type, user=chat.id).exception(text_error)
logger.bind(log_type=log_type, user=chat.id).error(text_error)
return text_error