yt-dlp-bot/app_bot/bot/core/handlers/abstract.py

40 lines
1.3 KiB
Python
Raw Normal View History

2022-11-03 01:56:19 +08:00
import logging
2024-04-26 04:09:50 +08:00
from abc import ABC, abstractmethod
2022-11-03 01:56:19 +08:00
from typing import TYPE_CHECKING
from yt_shared.enums import TaskSource, TelegramChatType
2024-03-21 01:25:07 +08:00
from yt_shared.schemas.base_rabbit import BaseRabbitDownloadPayload
2022-11-03 01:56:19 +08:00
2024-03-14 05:51:30 +08:00
from bot.core.schema import AnonymousUserSchema, UserSchema
2022-11-03 01:56:19 +08:00
if TYPE_CHECKING:
2024-03-21 01:25:07 +08:00
from bot.bot import VideoBotClient
2022-11-03 01:56:19 +08:00
2024-04-26 04:09:50 +08:00
class AbstractDownloadHandler(ABC):
2022-11-03 01:56:19 +08:00
def __init__(
self,
2024-03-21 01:25:07 +08:00
body: BaseRabbitDownloadPayload,
bot: 'VideoBotClient',
2022-11-03 01:56:19 +08:00
) -> None:
self._log = logging.getLogger(self.__class__.__name__)
self._body = body
self._bot = bot
self._receiving_users = self._get_receiving_users()
2024-04-26 04:09:50 +08:00
@abstractmethod
2023-09-21 02:05:41 +08:00
async def handle(self) -> None:
2022-11-03 01:56:19 +08:00
pass
def _get_sender_id(self) -> int | None:
if self._body.context.source is TaskSource.API:
return None
if self._body.context.from_chat_type is TelegramChatType.PRIVATE:
return self._body.context.from_user_id
return self._body.context.from_chat_id
2023-04-17 04:32:47 +08:00
def _get_receiving_users(self) -> list[AnonymousUserSchema | UserSchema]:
2022-11-03 01:56:19 +08:00
if self._body.context.source is TaskSource.API:
return self._bot.conf.telegram.api.upload_to_chat_ids.copy()
return [self._bot.allowed_users[self._get_sender_id()]]