Fix #163 [Caption too long]

This commit is contained in:
Taras Terletskyi 2023-09-23 19:18:05 +03:00
parent a3f121b615
commit 412ef95c74
3 changed files with 18 additions and 7 deletions

View file

@ -62,6 +62,7 @@ def get_main_config() -> ConfigSchema:
class BotSettings(Settings): class BotSettings(Settings):
TG_MAX_MSG_SIZE: int TG_MAX_MSG_SIZE: int
TG_MAX_CAPTION_SIZE: int
settings = BotSettings() settings = BotSettings()

View file

@ -18,7 +18,7 @@ from yt_shared.schemas.success import SuccessDownloadPayload
from yt_shared.utils.tasks.abstract import AbstractTask from yt_shared.utils.tasks.abstract import AbstractTask
from yt_shared.utils.tasks.tasks import create_task from yt_shared.utils.tasks.tasks import create_task
from bot.core.config.config import get_main_config from bot.core.config.config import get_main_config, settings
from bot.core.config.schema import AnonymousUserSchema, UserSchema, VideoCaptionSchema from bot.core.config.schema import AnonymousUserSchema, UserSchema, VideoCaptionSchema
from bot.core.utils import bold from bot.core.utils import bold
@ -89,6 +89,16 @@ class AbstractUploadTask(AbstractTask, metaclass=abc.ABCMeta):
self._log.exception('Exception in upload task for "%s"', self._filename) self._log.exception('Exception in upload task for "%s"', self._filename)
raise raise
@abc.abstractmethod
def _generate_caption_items(self) -> list[str]:
pass
def _generate_file_caption(self) -> str:
caption = '\n'.join(self._generate_caption_items())
if len(caption) > settings.TG_MAX_CAPTION_SIZE:
return caption[: settings.TG_MAX_CAPTION_SIZE]
return caption
async def _send_upload_text(self) -> None: async def _send_upload_text(self) -> None:
text = ( text = (
f'⬆️ {bold("Uploading")} {self._filename}\n' f'⬆️ {bold("Uploading")} {self._filename}\n'
@ -223,14 +233,13 @@ class AudioUploadTask(AbstractUploadTask):
self._create_cache_task(cache_object=audio) self._create_cache_task(cache_object=audio)
def _generate_file_caption(self) -> str: def _generate_caption_items(self) -> list[str]:
caption_items = ( return [
f'{bold("Title:")} {self._media_object.title}', f'{bold("Title:")} {self._media_object.title}',
f'{bold("Filename:")} {self._filename}', f'{bold("Filename:")} {self._filename}',
f'{bold("URL:")} {self._ctx.context.url}', f'{bold("URL:")} {self._ctx.context.url}',
f'{bold("Size:")} {self._media_object.file_size_human()}', f'{bold("Size:")} {self._media_object.file_size_human()}',
) ]
return '\n'.join(caption_items)
class VideoUploadTask(AbstractUploadTask): class VideoUploadTask(AbstractUploadTask):
@ -255,7 +264,7 @@ class VideoUploadTask(AbstractUploadTask):
return self._bot.conf.telegram.api.video_caption return self._bot.conf.telegram.api.video_caption
return self._users[0].upload.video_caption return self._users[0].upload.video_caption
def _generate_file_caption(self) -> str: def _generate_caption_items(self) -> list[str]:
caption_items = [] caption_items = []
caption_conf = self._get_caption_conf() caption_conf = self._get_caption_conf()
@ -267,7 +276,7 @@ class VideoUploadTask(AbstractUploadTask):
caption_items.append(f'👀 {self._ctx.context.url}') caption_items.append(f'👀 {self._ctx.context.url}')
if caption_conf.include_size: if caption_conf.include_size:
caption_items.append(f'💾 {self._media_object.file_size_human()}') caption_items.append(f'💾 {self._media_object.file_size_human()}')
return '\n'.join(caption_items) return caption_items
def _generate_send_media_coroutine(self, chat_id: int) -> Coroutine: def _generate_send_media_coroutine(self, chat_id: int) -> Coroutine:
kwargs = { kwargs = {

View file

@ -1,3 +1,4 @@
APPLICATION_NAME=yt_bot APPLICATION_NAME=yt_bot
TG_MAX_MSG_SIZE=4096 TG_MAX_MSG_SIZE=4096
TG_MAX_CAPTION_SIZE=1024