yt-dlp-bot/bot/core/callbacks.py

53 lines
1.7 KiB
Python
Raw Normal View History

2022-02-04 06:21:27 +08:00
import logging
2022-06-11 04:35:48 +08:00
from pyrogram.enums import ParseMode
from pyrogram.types import Message
2022-02-04 06:21:27 +08:00
2022-06-14 04:46:54 +08:00
from core.bot import VideoBot
2022-06-11 04:35:48 +08:00
from core.service import URLService
from core.utils import bold
2022-11-03 01:56:19 +08:00
from yt_shared.enums import TelegramChatType
2022-02-04 06:21:27 +08:00
from yt_shared.emoji import SUCCESS_EMOJI
2022-06-14 04:46:54 +08:00
from yt_shared.schemas.url import URL
2022-02-04 06:21:27 +08:00
class TelegramCallback:
_MSG_SEND_OK = f'{SUCCESS_EMOJI} {bold("URL sent for download")}'
_MSG_SEND_FAIL = f'🛑 {bold("Failed to send URL for download")}'
def __init__(self) -> None:
self._log = logging.getLogger(self.__class__.__name__)
2022-06-11 04:35:48 +08:00
self._url_service = URLService()
2022-02-04 06:21:27 +08:00
2022-06-14 07:24:25 +08:00
@staticmethod
async def on_start(client: VideoBot, message: Message) -> None:
await message.reply(
bold('Send video URL to start processing'),
parse_mode=ParseMode.HTML,
reply_to_message_id=message.id,
)
2022-06-14 04:46:54 +08:00
async def on_message(self, client: VideoBot, message: Message) -> None:
2022-02-04 06:21:27 +08:00
"""Receive video URL and send to the download worker."""
2022-11-03 01:56:19 +08:00
self._log.debug(message)
urls = self._get_urls(message)
await self._url_service.process_urls(urls=urls)
2022-02-04 06:21:27 +08:00
await message.reply(
2022-11-03 01:56:19 +08:00
self._MSG_SEND_OK,
2022-06-11 04:35:48 +08:00
parse_mode=ParseMode.HTML,
reply_to_message_id=message.id,
)
2022-11-03 01:56:19 +08:00
@staticmethod
def _get_urls(message: Message) -> list[URL]:
return [
URL(
url=url,
from_chat_id=message.chat.id,
from_chat_type=TelegramChatType(message.chat.type.value),
from_user_id=message.from_user.id,
message_id=message.id,
)
for url in message.text.splitlines()
]