2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Revamp Thumbnail type (ex PhotoSize)

This commit is contained in:
Dan 2019-05-29 09:47:14 +02:00
parent 807dcb67be
commit aaa569a08d

View File

@ -17,15 +17,16 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from struct import pack from struct import pack
from typing import List, Union from typing import Union, List
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.client.ext.utils import encode from pyrogram.client.ext.utils import encode
from .stripped_thumbnail import StrippedThumbnail
from ..pyrogram_type import PyrogramType from ..pyrogram_type import PyrogramType
class PhotoSize(PyrogramType): class Thumbnail(PyrogramType):
"""One size of a photo or a file/sticker thumbnail. """One size of a photo or a file/sticker thumbnail.
Parameters: Parameters:
@ -61,30 +62,44 @@ class PhotoSize(PyrogramType):
self.file_size = file_size self.file_size = file_size
@staticmethod @staticmethod
def _parse(client, thumbs: List) -> Union["PhotoSize", None]: def _parse(
if not thumbs: client,
media: Union[types.Photo, types.Document]
) -> Union[List[Union[StrippedThumbnail, "Thumbnail"]], None]:
if isinstance(media, types.Photo):
raw_thumbnails = media.sizes[:-1]
media_type = 0
elif isinstance(media, types.Document):
raw_thumbnails = media.thumbs
media_type = 14
if not raw_thumbnails:
return None
else:
return None return None
photo_size = thumbs[-1] thumbnails = []
if not isinstance(photo_size, (types.PhotoSize, types.PhotoCachedSize, types.PhotoStrippedSize)): for thumbnail in raw_thumbnails:
return None # TODO: Enable this
# if isinstance(thumbnail, types.PhotoStrippedSize):
loc = photo_size.location # thumbnails.append(StrippedThumbnail._parse(client, thumbnail))
if isinstance(thumbnail, types.PhotoSize):
if not isinstance(loc, types.FileLocation): thumbnails.append(
return None Thumbnail(
file_id=encode(
return PhotoSize( pack(
file_id=encode( "<iiqqc",
pack( media_type, media.dc_id,
"<iiqqqqi", media.id, media.access_hash,
0, loc.dc_id, 0, 0, thumbnail.type.encode()
loc.volume_id, loc.secret, loc.local_id )
),
width=thumbnail.w,
height=thumbnail.h,
file_size=thumbnail.size,
client=client
)
) )
),
width=getattr(photo_size, "w", 0), return thumbnails or None
height=getattr(photo_size, "h", 0),
file_size=getattr(photo_size, "size", len(getattr(photo_size, "bytes", b""))),
client=client
)