Accommodate changes to photo file ids

This commit is contained in:
Dan 2021-04-13 11:16:45 +02:00
parent 416c351f6b
commit 2eef1d5fcf
4 changed files with 75 additions and 86 deletions

View File

@ -874,8 +874,7 @@ class Client(Methods, Scaffold):
location = raw.types.InputPeerPhotoFileLocation( location = raw.types.InputPeerPhotoFileLocation(
peer=peer, peer=peer,
volume_id=file_id.volume_id, photo_id=file_id.media_id,
local_id=file_id.local_id,
big=file_id.thumbnail_source == ThumbnailSource.CHAT_PHOTO_BIG big=file_id.thumbnail_source == ThumbnailSource.CHAT_PHOTO_BIG
) )
elif file_type == FileType.PHOTO: elif file_type == FileType.PHOTO:

View File

@ -82,21 +82,25 @@ class Photo(Object):
@staticmethod @staticmethod
def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> "Photo": def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> "Photo":
if isinstance(photo, raw.types.Photo): if isinstance(photo, raw.types.Photo):
try: photos: List[raw.types.PhotoSize] = []
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)
big = photo_size_objs[-1] for p in photo.sizes:
else: if isinstance(p, raw.types.PhotoSize):
big = raw.types.PhotoSize( photos.append(p)
type=progressive.type,
location=progressive.location, if isinstance(p, raw.types.PhotoSizeProgressive):
w=progressive.w, photos.append(
h=progressive.h, raw.types.PhotoSize(
size=sorted(progressive.sizes)[-1] 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( return Photo(
file_id=FileId( file_id=FileId(
@ -107,19 +111,17 @@ class Photo(Object):
file_reference=photo.file_reference, file_reference=photo.file_reference,
thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_source=ThumbnailSource.THUMBNAIL,
thumbnail_file_type=FileType.PHOTO, thumbnail_file_type=FileType.PHOTO,
thumbnail_size=big.type, thumbnail_size=main.type,
volume_id=big.location.volume_id, volume_id=0,
local_id=big.location.local_id local_id=0
).encode(), ).encode(),
file_unique_id=FileUniqueId( file_unique_id=FileUniqueId(
file_unique_type=FileUniqueType.PHOTO, file_unique_type=FileUniqueType.DOCUMENT,
media_id=photo.id, media_id=photo.id
volume_id=big.location.volume_id,
local_id=big.location.local_id
).encode(), ).encode(),
width=big.w, width=main.w,
height=big.h, height=main.h,
file_size=big.size, file_size=main.size,
date=photo.date, date=photo.date,
ttl_seconds=ttl_seconds, ttl_seconds=ttl_seconds,
thumbs=types.Thumbnail._parse(client, photo), thumbs=types.Thumbnail._parse(client, photo),

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, List, Optional from typing import List, Optional, Union
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram import types
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
from ..object import Object from ..object import Object
@ -65,31 +64,26 @@ class Thumbnail(Object):
self.file_size = file_size self.file_size = file_size
@staticmethod @staticmethod
def _parse( def _parse(client, media: Union["raw.types.Photo", "raw.types.Document"]) -> Optional[List["Thumbnail"]]:
client,
media: Union["raw.types.Photo", "raw.types.Document"]
) -> Optional[List[Union["types.StrippedThumbnail", "Thumbnail"]]]:
if isinstance(media, raw.types.Photo): 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): elif isinstance(media, raw.types.Document):
raw_thumbnails = media.thumbs raw_thumbs = media.thumbs
file_type = FileType.THUMBNAIL
if not raw_thumbnails:
return None
else: else:
return None return
thumbnails = [] parsed_thumbs = []
file_type = FileType.PHOTO if isinstance(media, raw.types.Photo) else FileType.THUMBNAIL for thumb in raw_thumbs:
thumbnail_file_type = file_type if not isinstance(thumb, raw.types.PhotoSize):
continue
for thumbnail in raw_thumbnails: parsed_thumbs.append(
# TODO: Enable this
# if isinstance(thumbnail, types.PhotoStrippedSize):
# thumbnails.append(StrippedThumbnail._parse(client, thumbnail))
if isinstance(thumbnail, raw.types.PhotoSize):
thumbnails.append(
Thumbnail( Thumbnail(
file_id=FileId( file_id=FileId(
file_type=file_type, file_type=file_type,
@ -97,23 +91,21 @@ class Thumbnail(Object):
media_id=media.id, media_id=media.id,
access_hash=media.access_hash, access_hash=media.access_hash,
file_reference=media.file_reference, file_reference=media.file_reference,
thumbnail_file_type=thumbnail_file_type, thumbnail_file_type=file_type,
thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_source=ThumbnailSource.THUMBNAIL,
thumbnail_size=thumbnail.type, thumbnail_size=thumb.type,
volume_id=thumbnail.location.volume_id, volume_id=0,
local_id=thumbnail.location.local_id local_id=0
).encode(), ).encode(),
file_unique_id=FileUniqueId( file_unique_id=FileUniqueId(
file_unique_type=FileUniqueType.PHOTO, file_unique_type=FileUniqueType.DOCUMENT,
media_id=media.id, media_id=media.id
volume_id=thumbnail.location.volume_id,
local_id=thumbnail.location.local_id
).encode(), ).encode(),
width=thumbnail.w, width=thumb.w,
height=thumbnail.h, height=thumb.h,
file_size=thumbnail.size, file_size=thumb.size,
client=client client=client
) )
) )
return thumbnails or None return parsed_thumbs or None

View File

@ -72,40 +72,36 @@ class ChatPhoto(Object):
if not isinstance(chat_photo, (raw.types.UserProfilePhoto, raw.types.ChatPhoto)): if not isinstance(chat_photo, (raw.types.UserProfilePhoto, raw.types.ChatPhoto)):
return None return None
media_id = chat_photo.photo_id if isinstance(chat_photo, raw.types.UserProfilePhoto) else 0
return ChatPhoto( return ChatPhoto(
small_file_id=FileId( small_file_id=FileId(
file_type=FileType.CHAT_PHOTO, file_type=FileType.CHAT_PHOTO,
dc_id=chat_photo.dc_id, dc_id=chat_photo.dc_id,
media_id=media_id, media_id=chat_photo.photo_id,
access_hash=0, access_hash=0,
volume_id=chat_photo.photo_small.volume_id, volume_id=0,
thumbnail_source=ThumbnailSource.CHAT_PHOTO_SMALL, thumbnail_source=ThumbnailSource.CHAT_PHOTO_SMALL,
local_id=chat_photo.photo_small.local_id, local_id=0,
chat_id=peer_id, chat_id=peer_id,
chat_access_hash=peer_access_hash chat_access_hash=peer_access_hash
).encode(), ).encode(),
small_photo_unique_id=FileUniqueId( small_photo_unique_id=FileUniqueId(
file_unique_type=FileUniqueType.PHOTO, file_unique_type=FileUniqueType.DOCUMENT,
volume_id=chat_photo.photo_small.volume_id, media_id=chat_photo.photo_id
local_id=chat_photo.photo_small.local_id
).encode(), ).encode(),
big_file_id=FileId( big_file_id=FileId(
file_type=FileType.CHAT_PHOTO, file_type=FileType.CHAT_PHOTO,
dc_id=chat_photo.dc_id, dc_id=chat_photo.dc_id,
media_id=media_id, media_id=chat_photo.photo_id,
access_hash=0, access_hash=0,
volume_id=chat_photo.photo_big.volume_id, volume_id=0,
thumbnail_source=ThumbnailSource.CHAT_PHOTO_BIG, thumbnail_source=ThumbnailSource.CHAT_PHOTO_BIG,
local_id=chat_photo.photo_big.local_id, local_id=0,
chat_id=peer_id, chat_id=peer_id,
chat_access_hash=peer_access_hash chat_access_hash=peer_access_hash
).encode(), ).encode(),
big_photo_unique_id=FileUniqueId( big_photo_unique_id=FileUniqueId(
file_unique_type=FileUniqueType.PHOTO, file_unique_type=FileUniqueType.DOCUMENT,
volume_id=chat_photo.photo_big.volume_id, media_id=chat_photo.photo_id
local_id=chat_photo.photo_big.local_id
).encode(), ).encode(),
client=client client=client
) )