Cache stickers

This commit is contained in:
Dan 2018-12-17 12:42:29 +01:00
parent ac8258f451
commit e9a362923f
2 changed files with 32 additions and 25 deletions

View File

@ -17,8 +17,8 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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(

View File

@ -16,9 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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(