yt-dlp-bot/app_bot/bot/core/service.py

34 lines
1.1 KiB
Python
Raw Normal View History

2022-02-04 06:21:27 +08:00
import logging
2022-11-03 01:56:19 +08:00
from yt_shared.enums import TaskSource
2022-02-04 06:21:27 +08:00
from yt_shared.rabbit.publisher import Publisher
2022-06-14 04:46:54 +08:00
from yt_shared.schemas.url import URL
2022-02-04 06:21:27 +08:00
from yt_shared.schemas.video import VideoPayload
2022-06-11 04:35:48 +08:00
class URLService:
2022-02-04 06:21:27 +08:00
def __init__(self) -> None:
self._log = logging.getLogger(self.__class__.__name__)
self._publisher = Publisher()
2022-06-14 04:46:54 +08:00
async def process_url(self, url: URL) -> bool:
return await self._send_to_worker(url)
2022-02-04 06:21:27 +08:00
2022-11-03 01:56:19 +08:00
async def process_urls(self, urls: list[URL]) -> None:
for url in urls:
await self._send_to_worker(url)
2022-06-14 04:46:54 +08:00
async def _send_to_worker(self, url: URL) -> bool:
payload = VideoPayload(
url=url.url,
message_id=url.message_id,
from_user_id=url.from_user_id,
2022-11-03 01:56:19 +08:00
from_chat_id=url.from_chat_id,
from_chat_type=url.from_chat_type,
2022-06-14 04:46:54 +08:00
source=TaskSource.BOT,
)
2022-02-04 06:21:27 +08:00
is_sent = await self._publisher.send_for_download(payload)
if not is_sent:
2022-06-14 04:46:54 +08:00
self._log.error('Failed to publish URL %s to message broker', url.url)
2022-02-04 06:21:27 +08:00
return is_sent