From e9a362923f849bc80ce4dc8cb69efe1c19f56e36 Mon Sep 17 00:00:00 2001
From: Dan <14043624+delivrance@users.noreply.github.com>
Date: Mon, 17 Dec 2018 12:42:29 +0100
Subject: [PATCH] Cache stickers
---
.../types/messages_and_media/message.py | 31 +++++--------------
.../types/messages_and_media/sticker.py | 26 ++++++++++++++--
2 files changed, 32 insertions(+), 25 deletions(-)
diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py
index 41766332..66072d22 100644
--- a/pyrogram/client/types/messages_and_media/message.py
+++ b/pyrogram/client/types/messages_and_media/message.py
@@ -17,8 +17,8 @@
# along with Pyrogram. If not, see .
import pyrogram
-from pyrogram.api import types, functions
-from pyrogram.api.errors import MessageIdsEmpty, StickersetInvalid
+from pyrogram.api import types
+from pyrogram.api.errors import MessageIdsEmpty
from .contact import Contact
from .location import Location
from .message_entity import MessageEntity
@@ -442,21 +442,12 @@ class Message(PyrogramType):
else:
video = pyrogram.Video.parse(client, doc, video_attributes, file_name)
elif types.DocumentAttributeSticker in attributes:
- image_size_attributes = attributes.get(types.DocumentAttributeImageSize, None)
- sticker_attribute = attributes[types.DocumentAttributeSticker]
-
- if isinstance(sticker_attribute.stickerset, types.InputStickerSetID):
- try:
- set_name = client.send(
- functions.messages.GetStickerSet(sticker_attribute.stickerset)
- ).set.short_name
- except StickersetInvalid:
- set_name = None
- else:
- set_name = None
-
- sticker = pyrogram.Sticker.parse(client, doc, image_size_attributes,
- set_name, sticker_attribute, file_name)
+ sticker = pyrogram.Sticker.parse(
+ client, doc,
+ attributes.get(types.DocumentAttributeImageSize, None),
+ attributes[types.DocumentAttributeSticker],
+ file_name
+ )
else:
document = pyrogram.Document.parse(client, doc, file_name)
elif isinstance(media, types.MessageMediaWebPage):
@@ -517,12 +508,6 @@ class Message(PyrogramType):
raw=message
)
- if parsed_message.text:
- parsed_message.text.init(parsed_message._client, parsed_message.entities or [])
-
- if parsed_message.caption:
- parsed_message.caption.init(parsed_message._client, parsed_message.caption_entities or [])
-
if message.reply_to_msg_id and replies:
try:
parsed_message.reply_to_message = client.get_messages(
diff --git a/pyrogram/client/types/messages_and_media/sticker.py b/pyrogram/client/types/messages_and_media/sticker.py
index e6285895..f2414311 100644
--- a/pyrogram/client/types/messages_and_media/sticker.py
+++ b/pyrogram/client/types/messages_and_media/sticker.py
@@ -16,9 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
+from functools import lru_cache
from struct import pack
-from pyrogram.api import types
+from pyrogram.api import types, functions
+from pyrogram.api.errors import StickersetInvalid
from .photo_size import PhotoSize
from ..pyrogram_type import PyrogramType
from ...ext.utils import encode
@@ -79,8 +81,28 @@ class Sticker(PyrogramType):
self.mask_position = mask_position
@staticmethod
- def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize, set_name: str,
+ @lru_cache(maxsize=256)
+ def get_sticker_set_name(send, input_sticker_set_id):
+ try:
+ return send(
+ functions.messages.GetStickerSet(
+ types.InputStickerSetID(*input_sticker_set_id)
+ )
+ ).set.short_name
+ except StickersetInvalid:
+ return None
+
+ @staticmethod
+ def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize,
sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker":
+ sticker_set = sticker_attributes.stickerset
+
+ if isinstance(sticker_set, types.InputStickerSetID):
+ input_sticker_set_id = (sticker_set.id, sticker_set.access_hash)
+ set_name = Sticker.get_sticker_set_name(client.send, input_sticker_set_id)
+ else:
+ set_name = None
+
return Sticker(
file_id=encode(
pack(