From e3dcb6e59bc2167e50f603aba785dc994a6f7fb1 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Mon, 13 May 2024 00:19:24 +0800 Subject: [PATCH] fix send gif --- main.py | 5 +++++ tweet.py | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 7cf61ed..671dd72 100644 --- a/main.py +++ b/main.py @@ -52,6 +52,11 @@ async def url_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: media, caption=tweet.message_text, reply_to_message_id=update.message.message_id, + ) if not tweet.is_single_gif else await update.effective_message.reply_animation( + media[0][0], + caption=tweet.message_text, + reply_to_message_id=update.message.message_id, + has_spoiler=media[0][1] ) url = tweet.url if context.user_data.get('edit_before_forward', False): diff --git a/tweet.py b/tweet.py index 7ca2bdf..36e6003 100644 --- a/tweet.py +++ b/tweet.py @@ -7,8 +7,7 @@ from telegram import ( InlineQueryResultVideo, InlineQueryResultMpeg4Gif, InputMediaPhoto, - InputMediaVideo, - InputMediaAnimation + InputMediaVideo ) from common import x_url_regex, x_media_regex, x_tco_regex, logger @@ -125,6 +124,7 @@ class TGTweet(Tweet): def __init__(self, url: str): self._url: str = url self._api_param: tuple[str] = self._tweet_id + self._is_single_gif: bool = False assert self._api_param async def __aenter__(self): @@ -179,6 +179,10 @@ class TGTweet(Tweet): sensitive = self._tweet['possibly_sensitive'] return id, author, author_id, text, media, sensitive + @property + def is_single_gif(self) -> bool: + return self._is_single_gif + @property def message_text(self) -> str: return message_raw_text.format( @@ -219,7 +223,7 @@ class TGTweet(Tweet): ) @property - def pm_media_generator(self) -> Generator[InputMediaPhoto | InputMediaVideo | InputMediaAnimation, None, None]: + def pm_media_generator(self) -> Generator[InputMediaPhoto | InputMediaVideo | tuple[str, bool], None, None]: for tweet_media in self.media: logger.info(str(tweet_media)) if tweet_media.type == "image": @@ -234,7 +238,10 @@ class TGTweet(Tweet): thumbnail=tweet_media.thumb ) elif tweet_media.type == "gif": - yield InputMediaAnimation( + if len(self.media) == 1: + self._is_single_gif = True + yield tweet_media.url, self.sensitive + yield InputMediaVideo( media=tweet_media.url, has_spoiler=self.sensitive, thumbnail=tweet_media.thumb