From 95de5f7eae4f53b600310f165b6f973c6f8895f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=D1=91Nya?= <47749563+L3Nya@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:50:48 +0300 Subject: [PATCH 1/2] Fix determining video sticker resolution. Add sticker duration to Sticker type (#1065) --- .../methods/messages/get_custom_emoji_stickers.py | 5 +++-- pyrogram/types/messages_and_media/message.py | 3 ++- pyrogram/types/messages_and_media/sticker.py | 12 +++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pyrogram/methods/messages/get_custom_emoji_stickers.py b/pyrogram/methods/messages/get_custom_emoji_stickers.py index de5d8d4a..8e1b2a4c 100644 --- a/pyrogram/methods/messages/get_custom_emoji_stickers.py +++ b/pyrogram/methods/messages/get_custom_emoji_stickers.py @@ -50,9 +50,10 @@ class GetCustomEmojiStickers: sticker = await types.Sticker._parse( self, item, - attributes[raw.types.DocumentAttributeImageSize], + attributes[raw.types.DocumentAttributeImageSize] if raw.types.DocumentAttributeImageSize in attributes else None, attributes[raw.types.DocumentAttributeCustomEmoji], - attributes[raw.types.DocumentAttributeFilename].file_name + attributes[raw.types.DocumentAttributeFilename].file_name, + attributes[raw.types.DocumentAttributeVideo] if raw.types.DocumentAttributeVideo in attributes else None ) stickers.append(sticker) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 56e4d7fe..06d9d716 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -700,7 +700,8 @@ class Message(Object, Update): client, doc, attributes.get(raw.types.DocumentAttributeImageSize, None), attributes[raw.types.DocumentAttributeSticker], - file_name + file_name, + attributes.get(raw.types.DocumentAttributeVideo, None) ) media_type = enums.MessageMediaType.STICKER elif raw.types.DocumentAttributeVideo in attributes: diff --git a/pyrogram/types/messages_and_media/sticker.py b/pyrogram/types/messages_and_media/sticker.py index 201b579f..019fc180 100644 --- a/pyrogram/types/messages_and_media/sticker.py +++ b/pyrogram/types/messages_and_media/sticker.py @@ -50,6 +50,9 @@ class Sticker(Object): is_video (``bool``): True, if the sticker is a video sticker + duration (``int``): + Video sticker duration in seconds. + file_name (``str``, *optional*): Sticker file name. @@ -84,6 +87,7 @@ class Sticker(Object): height: int, is_animated: bool, is_video: bool, + duration: int = None, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -148,7 +152,8 @@ class Sticker(Object): sticker: "raw.types.Document", image_size_attributes: "raw.types.DocumentAttributeImageSize", sticker_attributes: "raw.types.DocumentAttributeSticker", - file_name: str + file_name: str, + video_attributes: "raw.types.DocumentAttributeVideo" ) -> "Sticker": sticker_set = sticker_attributes.stickerset @@ -170,10 +175,11 @@ class Sticker(Object): file_unique_type=FileUniqueType.DOCUMENT, media_id=sticker.id ).encode(), - width=image_size_attributes.w if image_size_attributes else 512, - height=image_size_attributes.h if image_size_attributes else 512, + width=image_size_attributes.w if image_size_attributes else (video_attributes.w if video_attributes else 512), + height=image_size_attributes.h if image_size_attributes else (video_attributes.h if video_attributes else 512), is_animated=sticker.mime_type == "application/x-tgsticker", is_video=sticker.mime_type == "video/webm", + duration=video_attributes.duration if video_attributes else None, # TODO: mask_position set_name=set_name, emoji=sticker_attributes.alt or None, From 2e46514012167b84d5f984fcd00917af0420866d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:19:01 +0200 Subject: [PATCH 2/2] Refactor Sticker parsing --- .../messages/get_custom_emoji_stickers.py | 10 +----- pyrogram/types/messages_and_media/message.py | 8 +---- pyrogram/types/messages_and_media/sticker.py | 36 ++++++++++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/pyrogram/methods/messages/get_custom_emoji_stickers.py b/pyrogram/methods/messages/get_custom_emoji_stickers.py index 8e1b2a4c..f781bf6e 100644 --- a/pyrogram/methods/messages/get_custom_emoji_stickers.py +++ b/pyrogram/methods/messages/get_custom_emoji_stickers.py @@ -47,15 +47,7 @@ class GetCustomEmojiStickers: stickers = [] for item in result: attributes = {type(i): i for i in item.attributes} - - sticker = await types.Sticker._parse( - self, item, - attributes[raw.types.DocumentAttributeImageSize] if raw.types.DocumentAttributeImageSize in attributes else None, - attributes[raw.types.DocumentAttributeCustomEmoji], - attributes[raw.types.DocumentAttributeFilename].file_name, - attributes[raw.types.DocumentAttributeVideo] if raw.types.DocumentAttributeVideo in attributes else None - ) - + sticker = await types.Sticker._parse(self, item, attributes) stickers.append(sticker) return pyrogram.types.List(stickers) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 06d9d716..146e37c4 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -696,13 +696,7 @@ class Message(Object, Update): animation = types.Animation._parse(client, doc, video_attributes, file_name) media_type = enums.MessageMediaType.ANIMATION elif raw.types.DocumentAttributeSticker in attributes: - sticker = await types.Sticker._parse( - client, doc, - attributes.get(raw.types.DocumentAttributeImageSize, None), - attributes[raw.types.DocumentAttributeSticker], - file_name, - attributes.get(raw.types.DocumentAttributeVideo, None) - ) + sticker = await types.Sticker._parse(client, doc, attributes) media_type = enums.MessageMediaType.STICKER elif raw.types.DocumentAttributeVideo in attributes: video_attributes = attributes[raw.types.DocumentAttributeVideo] diff --git a/pyrogram/types/messages_and_media/sticker.py b/pyrogram/types/messages_and_media/sticker.py index 019fc180..56ddf2af 100644 --- a/pyrogram/types/messages_and_media/sticker.py +++ b/pyrogram/types/messages_and_media/sticker.py @@ -17,7 +17,7 @@ # along with Pyrogram. If not, see . from datetime import datetime -from typing import List +from typing import List, Dict, Type import pyrogram from pyrogram import raw, utils @@ -50,9 +50,6 @@ class Sticker(Object): is_video (``bool``): True, if the sticker is a video sticker - duration (``int``): - Video sticker duration in seconds. - file_name (``str``, *optional*): Sticker file name. @@ -87,7 +84,6 @@ class Sticker(Object): height: int, is_animated: bool, is_video: bool, - duration: int = None, file_name: str = None, mime_type: str = None, file_size: int = None, @@ -150,11 +146,16 @@ class Sticker(Object): async def _parse( client, sticker: "raw.types.Document", - image_size_attributes: "raw.types.DocumentAttributeImageSize", - sticker_attributes: "raw.types.DocumentAttributeSticker", - file_name: str, - video_attributes: "raw.types.DocumentAttributeVideo" + document_attributes: Dict[Type["raw.base.DocumentAttribute"], "raw.base.DocumentAttribute"], ) -> "Sticker": + sticker_attributes = document_attributes.get( + raw.types.DocumentAttributeSticker, + document_attributes[raw.types.DocumentAttributeCustomEmoji] + ) + image_size_attributes = document_attributes.get(raw.types.DocumentAttributeImageSize, None) + file_name = getattr(document_attributes.get(raw.types.DocumentAttributeFilename, None), "file_name", None) + video_attributes = document_attributes.get(raw.types.DocumentAttributeVideo, None) + sticker_set = sticker_attributes.stickerset if isinstance(sticker_set, raw.types.InputStickerSetID): @@ -175,11 +176,22 @@ class Sticker(Object): file_unique_type=FileUniqueType.DOCUMENT, media_id=sticker.id ).encode(), - width=image_size_attributes.w if image_size_attributes else (video_attributes.w if video_attributes else 512), - height=image_size_attributes.h if image_size_attributes else (video_attributes.h if video_attributes else 512), + width=( + image_size_attributes.w + if image_size_attributes + else video_attributes.w + if video_attributes + else 512 + ), + height=( + image_size_attributes.h + if image_size_attributes + else video_attributes.h + if video_attributes + else 512 + ), is_animated=sticker.mime_type == "application/x-tgsticker", is_video=sticker.mime_type == "video/webm", - duration=video_attributes.duration if video_attributes else None, # TODO: mask_position set_name=set_name, emoji=sticker_attributes.alt or None,