mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 05:18:10 +00:00
Refactor Sticker parsing
This commit is contained in:
parent
95de5f7eae
commit
2e46514012
@ -47,15 +47,7 @@ class GetCustomEmojiStickers:
|
|||||||
stickers = []
|
stickers = []
|
||||||
for item in result:
|
for item in result:
|
||||||
attributes = {type(i): i for i in item.attributes}
|
attributes = {type(i): i for i in item.attributes}
|
||||||
|
sticker = await types.Sticker._parse(self, 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
|
|
||||||
)
|
|
||||||
|
|
||||||
stickers.append(sticker)
|
stickers.append(sticker)
|
||||||
|
|
||||||
return pyrogram.types.List(stickers)
|
return pyrogram.types.List(stickers)
|
||||||
|
@ -696,13 +696,7 @@ class Message(Object, Update):
|
|||||||
animation = types.Animation._parse(client, doc, video_attributes, file_name)
|
animation = types.Animation._parse(client, doc, video_attributes, file_name)
|
||||||
media_type = enums.MessageMediaType.ANIMATION
|
media_type = enums.MessageMediaType.ANIMATION
|
||||||
elif raw.types.DocumentAttributeSticker in attributes:
|
elif raw.types.DocumentAttributeSticker in attributes:
|
||||||
sticker = await types.Sticker._parse(
|
sticker = await types.Sticker._parse(client, doc, attributes)
|
||||||
client, doc,
|
|
||||||
attributes.get(raw.types.DocumentAttributeImageSize, None),
|
|
||||||
attributes[raw.types.DocumentAttributeSticker],
|
|
||||||
file_name,
|
|
||||||
attributes.get(raw.types.DocumentAttributeVideo, None)
|
|
||||||
)
|
|
||||||
media_type = enums.MessageMediaType.STICKER
|
media_type = enums.MessageMediaType.STICKER
|
||||||
elif raw.types.DocumentAttributeVideo in attributes:
|
elif raw.types.DocumentAttributeVideo in attributes:
|
||||||
video_attributes = attributes[raw.types.DocumentAttributeVideo]
|
video_attributes = attributes[raw.types.DocumentAttributeVideo]
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List
|
from typing import List, Dict, Type
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw, utils
|
from pyrogram import raw, utils
|
||||||
@ -50,9 +50,6 @@ class Sticker(Object):
|
|||||||
is_video (``bool``):
|
is_video (``bool``):
|
||||||
True, if the sticker is a video sticker
|
True, if the sticker is a video sticker
|
||||||
|
|
||||||
duration (``int``):
|
|
||||||
Video sticker duration in seconds.
|
|
||||||
|
|
||||||
file_name (``str``, *optional*):
|
file_name (``str``, *optional*):
|
||||||
Sticker file name.
|
Sticker file name.
|
||||||
|
|
||||||
@ -87,7 +84,6 @@ class Sticker(Object):
|
|||||||
height: int,
|
height: int,
|
||||||
is_animated: bool,
|
is_animated: bool,
|
||||||
is_video: bool,
|
is_video: bool,
|
||||||
duration: int = None,
|
|
||||||
file_name: str = None,
|
file_name: str = None,
|
||||||
mime_type: str = None,
|
mime_type: str = None,
|
||||||
file_size: int = None,
|
file_size: int = None,
|
||||||
@ -150,11 +146,16 @@ class Sticker(Object):
|
|||||||
async def _parse(
|
async def _parse(
|
||||||
client,
|
client,
|
||||||
sticker: "raw.types.Document",
|
sticker: "raw.types.Document",
|
||||||
image_size_attributes: "raw.types.DocumentAttributeImageSize",
|
document_attributes: Dict[Type["raw.base.DocumentAttribute"], "raw.base.DocumentAttribute"],
|
||||||
sticker_attributes: "raw.types.DocumentAttributeSticker",
|
|
||||||
file_name: str,
|
|
||||||
video_attributes: "raw.types.DocumentAttributeVideo"
|
|
||||||
) -> "Sticker":
|
) -> "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
|
sticker_set = sticker_attributes.stickerset
|
||||||
|
|
||||||
if isinstance(sticker_set, raw.types.InputStickerSetID):
|
if isinstance(sticker_set, raw.types.InputStickerSetID):
|
||||||
@ -175,11 +176,22 @@ class Sticker(Object):
|
|||||||
file_unique_type=FileUniqueType.DOCUMENT,
|
file_unique_type=FileUniqueType.DOCUMENT,
|
||||||
media_id=sticker.id
|
media_id=sticker.id
|
||||||
).encode(),
|
).encode(),
|
||||||
width=image_size_attributes.w if image_size_attributes else (video_attributes.w if video_attributes else 512),
|
width=(
|
||||||
height=image_size_attributes.h if image_size_attributes else (video_attributes.h if video_attributes else 512),
|
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_animated=sticker.mime_type == "application/x-tgsticker",
|
||||||
is_video=sticker.mime_type == "video/webm",
|
is_video=sticker.mime_type == "video/webm",
|
||||||
duration=video_attributes.duration if video_attributes else None,
|
|
||||||
# TODO: mask_position
|
# TODO: mask_position
|
||||||
set_name=set_name,
|
set_name=set_name,
|
||||||
emoji=sticker_attributes.alt or None,
|
emoji=sticker_attributes.alt or None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user