From 2eef1d5fcf0e06705910704a6907c8f1131405f0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 13 Apr 2021 11:16:45 +0200 Subject: [PATCH] Accommodate changes to photo file ids --- pyrogram/client.py | 3 +- pyrogram/types/messages_and_media/photo.py | 50 +++++------ .../types/messages_and_media/thumbnail.py | 84 +++++++++---------- pyrogram/types/user_and_chats/chat_photo.py | 24 +++--- 4 files changed, 75 insertions(+), 86 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index 5f80182d..0dc344ec 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -874,8 +874,7 @@ class Client(Methods, Scaffold): location = raw.types.InputPeerPhotoFileLocation( peer=peer, - volume_id=file_id.volume_id, - local_id=file_id.local_id, + photo_id=file_id.media_id, big=file_id.thumbnail_source == ThumbnailSource.CHAT_PHOTO_BIG ) elif file_type == FileType.PHOTO: diff --git a/pyrogram/types/messages_and_media/photo.py b/pyrogram/types/messages_and_media/photo.py index c350b6a7..a512d17b 100644 --- a/pyrogram/types/messages_and_media/photo.py +++ b/pyrogram/types/messages_and_media/photo.py @@ -82,21 +82,25 @@ class Photo(Object): @staticmethod def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> "Photo": if isinstance(photo, raw.types.Photo): - try: - progressive = next(p for p in photo.sizes if isinstance(p, raw.types.PhotoSizeProgressive)) - except StopIteration: - photo_size_objs = [p for p in photo.sizes if isinstance(p, raw.types.PhotoSize)] - photo_size_objs.sort(key=lambda p: p.size) + photos: List[raw.types.PhotoSize] = [] - big = photo_size_objs[-1] - else: - big = raw.types.PhotoSize( - type=progressive.type, - location=progressive.location, - w=progressive.w, - h=progressive.h, - size=sorted(progressive.sizes)[-1] - ) + for p in photo.sizes: + if isinstance(p, raw.types.PhotoSize): + photos.append(p) + + if isinstance(p, raw.types.PhotoSizeProgressive): + photos.append( + raw.types.PhotoSize( + type=p.type, + w=p.w, + h=p.h, + size=max(p.sizes) + ) + ) + + photos.sort(key=lambda p: p.size) + + main = photos[-1] return Photo( file_id=FileId( @@ -107,19 +111,17 @@ class Photo(Object): file_reference=photo.file_reference, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_file_type=FileType.PHOTO, - thumbnail_size=big.type, - volume_id=big.location.volume_id, - local_id=big.location.local_id + thumbnail_size=main.type, + volume_id=0, + local_id=0 ).encode(), file_unique_id=FileUniqueId( - file_unique_type=FileUniqueType.PHOTO, - media_id=photo.id, - volume_id=big.location.volume_id, - local_id=big.location.local_id + file_unique_type=FileUniqueType.DOCUMENT, + media_id=photo.id ).encode(), - width=big.w, - height=big.h, - file_size=big.size, + width=main.w, + height=main.h, + file_size=main.size, date=photo.date, ttl_seconds=ttl_seconds, thumbs=types.Thumbnail._parse(client, photo), diff --git a/pyrogram/types/messages_and_media/thumbnail.py b/pyrogram/types/messages_and_media/thumbnail.py index 7b26c5b8..1a55c18c 100644 --- a/pyrogram/types/messages_and_media/thumbnail.py +++ b/pyrogram/types/messages_and_media/thumbnail.py @@ -16,11 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Union, List, Optional +from typing import List, Optional, Union import pyrogram from pyrogram import raw -from pyrogram import types from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource from ..object import Object @@ -65,55 +64,48 @@ class Thumbnail(Object): self.file_size = file_size @staticmethod - def _parse( - client, - media: Union["raw.types.Photo", "raw.types.Document"] - ) -> Optional[List[Union["types.StrippedThumbnail", "Thumbnail"]]]: + def _parse(client, media: Union["raw.types.Photo", "raw.types.Document"]) -> Optional[List["Thumbnail"]]: if isinstance(media, raw.types.Photo): - raw_thumbnails = media.sizes[:-1] + raw_thumbs = [i for i in media.sizes if isinstance(i, raw.types.PhotoSize)] + raw_thumbs.sort(key=lambda p: p.size) + raw_thumbs = raw_thumbs[:-1] + + file_type = FileType.PHOTO elif isinstance(media, raw.types.Document): - raw_thumbnails = media.thumbs - - if not raw_thumbnails: - return None + raw_thumbs = media.thumbs + file_type = FileType.THUMBNAIL else: - return None + return - thumbnails = [] + parsed_thumbs = [] - file_type = FileType.PHOTO if isinstance(media, raw.types.Photo) else FileType.THUMBNAIL - thumbnail_file_type = file_type + for thumb in raw_thumbs: + if not isinstance(thumb, raw.types.PhotoSize): + continue - for thumbnail in raw_thumbnails: - # TODO: Enable this - # if isinstance(thumbnail, types.PhotoStrippedSize): - # thumbnails.append(StrippedThumbnail._parse(client, thumbnail)) - if isinstance(thumbnail, raw.types.PhotoSize): - thumbnails.append( - Thumbnail( - file_id=FileId( - file_type=file_type, - dc_id=media.dc_id, - media_id=media.id, - access_hash=media.access_hash, - file_reference=media.file_reference, - thumbnail_file_type=thumbnail_file_type, - thumbnail_source=ThumbnailSource.THUMBNAIL, - thumbnail_size=thumbnail.type, - volume_id=thumbnail.location.volume_id, - local_id=thumbnail.location.local_id - ).encode(), - file_unique_id=FileUniqueId( - file_unique_type=FileUniqueType.PHOTO, - media_id=media.id, - volume_id=thumbnail.location.volume_id, - local_id=thumbnail.location.local_id - ).encode(), - width=thumbnail.w, - height=thumbnail.h, - file_size=thumbnail.size, - client=client - ) + parsed_thumbs.append( + Thumbnail( + file_id=FileId( + file_type=file_type, + dc_id=media.dc_id, + media_id=media.id, + access_hash=media.access_hash, + file_reference=media.file_reference, + thumbnail_file_type=file_type, + thumbnail_source=ThumbnailSource.THUMBNAIL, + thumbnail_size=thumb.type, + volume_id=0, + local_id=0 + ).encode(), + file_unique_id=FileUniqueId( + file_unique_type=FileUniqueType.DOCUMENT, + media_id=media.id + ).encode(), + width=thumb.w, + height=thumb.h, + file_size=thumb.size, + client=client ) + ) - return thumbnails or None + return parsed_thumbs or None diff --git a/pyrogram/types/user_and_chats/chat_photo.py b/pyrogram/types/user_and_chats/chat_photo.py index cd10c3c2..959d1732 100644 --- a/pyrogram/types/user_and_chats/chat_photo.py +++ b/pyrogram/types/user_and_chats/chat_photo.py @@ -72,40 +72,36 @@ class ChatPhoto(Object): if not isinstance(chat_photo, (raw.types.UserProfilePhoto, raw.types.ChatPhoto)): return None - media_id = chat_photo.photo_id if isinstance(chat_photo, raw.types.UserProfilePhoto) else 0 - return ChatPhoto( small_file_id=FileId( file_type=FileType.CHAT_PHOTO, dc_id=chat_photo.dc_id, - media_id=media_id, + media_id=chat_photo.photo_id, access_hash=0, - volume_id=chat_photo.photo_small.volume_id, + volume_id=0, thumbnail_source=ThumbnailSource.CHAT_PHOTO_SMALL, - local_id=chat_photo.photo_small.local_id, + local_id=0, chat_id=peer_id, chat_access_hash=peer_access_hash ).encode(), small_photo_unique_id=FileUniqueId( - file_unique_type=FileUniqueType.PHOTO, - volume_id=chat_photo.photo_small.volume_id, - local_id=chat_photo.photo_small.local_id + file_unique_type=FileUniqueType.DOCUMENT, + media_id=chat_photo.photo_id ).encode(), big_file_id=FileId( file_type=FileType.CHAT_PHOTO, dc_id=chat_photo.dc_id, - media_id=media_id, + media_id=chat_photo.photo_id, access_hash=0, - volume_id=chat_photo.photo_big.volume_id, + volume_id=0, thumbnail_source=ThumbnailSource.CHAT_PHOTO_BIG, - local_id=chat_photo.photo_big.local_id, + local_id=0, chat_id=peer_id, chat_access_hash=peer_access_hash ).encode(), big_photo_unique_id=FileUniqueId( - file_unique_type=FileUniqueType.PHOTO, - volume_id=chat_photo.photo_big.volume_id, - local_id=chat_photo.photo_big.local_id + file_unique_type=FileUniqueType.DOCUMENT, + media_id=chat_photo.photo_id ).encode(), client=client )