From e5e0b17809b06f9fe6ddf4a82ce710b5a691a0ff Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 15 Dec 2018 21:40:44 +0100 Subject: [PATCH] Refactor Photo and PhotoSize --- .../client/types/messages_and_media/photo.py | 61 ++++++++++++++++++- .../types/messages_and_media/photo_size.py | 37 ++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/types/messages_and_media/photo.py b/pyrogram/client/types/messages_and_media/photo.py index 4037025b..b1021d93 100644 --- a/pyrogram/client/types/messages_and_media/photo.py +++ b/pyrogram/client/types/messages_and_media/photo.py @@ -16,7 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from base64 import b64encode +from struct import pack + +from pyrogram.api import types from pyrogram.api.core import Object +from .photo_size import PhotoSize +from ...ext.utils import encode class Photo(Object): @@ -35,7 +41,60 @@ class Photo(Object): ID = 0xb0700027 - def __init__(self, id: str, date: int, sizes: list): + def __init__(self, id: str, date: int, sizes: list, *, + client=None, raw=None): self.id = id self.date = date self.sizes = sizes + + self._client = client + self._raw = raw + + @staticmethod + def parse(client, photo: types.Photo): + if isinstance(photo, types.Photo): + raw_sizes = photo.sizes + sizes = [] + + for raw_size in raw_sizes: + if isinstance(raw_size, (types.PhotoSize, types.PhotoCachedSize)): + + if isinstance(raw_size, types.PhotoSize): + file_size = raw_size.size + elif isinstance(raw_size, types.PhotoCachedSize): + file_size = len(raw_size.bytes) + else: + file_size = 0 + + loc = raw_size.location + + if isinstance(loc, types.FileLocation): + size = PhotoSize( + file_id=encode( + pack( + "