Skip to content

Commit 2e46514

Browse files
committed
Refactor Sticker parsing
1 parent 95de5f7 commit 2e46514

3 files changed

Lines changed: 26 additions & 28 deletions

File tree

pyrogram/methods/messages/get_custom_emoji_stickers.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,7 @@ async def get_custom_emoji_stickers(
4747
stickers = []
4848
for item in result:
4949
attributes = {type(i): i for i in item.attributes}
50-
51-
sticker = await types.Sticker._parse(
52-
self, item,
53-
attributes[raw.types.DocumentAttributeImageSize] if raw.types.DocumentAttributeImageSize in attributes else None,
54-
attributes[raw.types.DocumentAttributeCustomEmoji],
55-
attributes[raw.types.DocumentAttributeFilename].file_name,
56-
attributes[raw.types.DocumentAttributeVideo] if raw.types.DocumentAttributeVideo in attributes else None
57-
)
58-
50+
sticker = await types.Sticker._parse(self, item, attributes)
5951
stickers.append(sticker)
6052

6153
return pyrogram.types.List(stickers)

pyrogram/types/messages_and_media/message.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,7 @@ async def _parse(
696696
animation = types.Animation._parse(client, doc, video_attributes, file_name)
697697
media_type = enums.MessageMediaType.ANIMATION
698698
elif raw.types.DocumentAttributeSticker in attributes:
699-
sticker = await types.Sticker._parse(
700-
client, doc,
701-
attributes.get(raw.types.DocumentAttributeImageSize, None),
702-
attributes[raw.types.DocumentAttributeSticker],
703-
file_name,
704-
attributes.get(raw.types.DocumentAttributeVideo, None)
705-
)
699+
sticker = await types.Sticker._parse(client, doc, attributes)
706700
media_type = enums.MessageMediaType.STICKER
707701
elif raw.types.DocumentAttributeVideo in attributes:
708702
video_attributes = attributes[raw.types.DocumentAttributeVideo]

pyrogram/types/messages_and_media/sticker.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from datetime import datetime
20-
from typing import List
20+
from typing import List, Dict, Type
2121

2222
import pyrogram
2323
from pyrogram import raw, utils
@@ -50,9 +50,6 @@ class Sticker(Object):
5050
is_video (``bool``):
5151
True, if the sticker is a video sticker
5252
53-
duration (``int``):
54-
Video sticker duration in seconds.
55-
5653
file_name (``str``, *optional*):
5754
Sticker file name.
5855
@@ -87,7 +84,6 @@ def __init__(
8784
height: int,
8885
is_animated: bool,
8986
is_video: bool,
90-
duration: int = None,
9187
file_name: str = None,
9288
mime_type: str = None,
9389
file_size: int = None,
@@ -150,11 +146,16 @@ async def _get_sticker_set_name(invoke, input_sticker_set_id):
150146
async def _parse(
151147
client,
152148
sticker: "raw.types.Document",
153-
image_size_attributes: "raw.types.DocumentAttributeImageSize",
154-
sticker_attributes: "raw.types.DocumentAttributeSticker",
155-
file_name: str,
156-
video_attributes: "raw.types.DocumentAttributeVideo"
149+
document_attributes: Dict[Type["raw.base.DocumentAttribute"], "raw.base.DocumentAttribute"],
157150
) -> "Sticker":
151+
sticker_attributes = document_attributes.get(
152+
raw.types.DocumentAttributeSticker,
153+
document_attributes[raw.types.DocumentAttributeCustomEmoji]
154+
)
155+
image_size_attributes = document_attributes.get(raw.types.DocumentAttributeImageSize, None)
156+
file_name = getattr(document_attributes.get(raw.types.DocumentAttributeFilename, None), "file_name", None)
157+
video_attributes = document_attributes.get(raw.types.DocumentAttributeVideo, None)
158+
158159
sticker_set = sticker_attributes.stickerset
159160

160161
if isinstance(sticker_set, raw.types.InputStickerSetID):
@@ -175,11 +176,22 @@ async def _parse(
175176
file_unique_type=FileUniqueType.DOCUMENT,
176177
media_id=sticker.id
177178
).encode(),
178-
width=image_size_attributes.w if image_size_attributes else (video_attributes.w if video_attributes else 512),
179-
height=image_size_attributes.h if image_size_attributes else (video_attributes.h if video_attributes else 512),
179+
width=(
180+
image_size_attributes.w
181+
if image_size_attributes
182+
else video_attributes.w
183+
if video_attributes
184+
else 512
185+
),
186+
height=(
187+
image_size_attributes.h
188+
if image_size_attributes
189+
else video_attributes.h
190+
if video_attributes
191+
else 512
192+
),
180193
is_animated=sticker.mime_type == "application/x-tgsticker",
181194
is_video=sticker.mime_type == "video/webm",
182-
duration=video_attributes.duration if video_attributes else None,
183195
# TODO: mask_position
184196
set_name=set_name,
185197
emoji=sticker_attributes.alt or None,

0 commit comments

Comments
 (0)