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(
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:

View File

@ -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),

View File

@ -16,11 +16,10 @@
# 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 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,31 +64,26 @@ 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(
parsed_thumbs.append(
Thumbnail(
file_id=FileId(
file_type=file_type,
@ -97,23 +91,21 @@ class Thumbnail(Object):
media_id=media.id,
access_hash=media.access_hash,
file_reference=media.file_reference,
thumbnail_file_type=thumbnail_file_type,
thumbnail_file_type=file_type,
thumbnail_source=ThumbnailSource.THUMBNAIL,
thumbnail_size=thumbnail.type,
volume_id=thumbnail.location.volume_id,
local_id=thumbnail.location.local_id
thumbnail_size=thumb.type,
volume_id=0,
local_id=0
).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
file_unique_type=FileUniqueType.DOCUMENT,
media_id=media.id
).encode(),
width=thumbnail.w,
height=thumbnail.h,
file_size=thumbnail.size,
width=thumb.w,
height=thumb.h,
file_size=thumb.size,
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)):
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
)