Accommodate changes to photo file ids
This commit is contained in:
parent
416c351f6b
commit
2eef1d5fcf
@ -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:
|
||||||
|
@ -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),
|
||||||
|
@ -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,55 +64,48 @@ 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
|
Thumbnail(
|
||||||
# if isinstance(thumbnail, types.PhotoStrippedSize):
|
file_id=FileId(
|
||||||
# thumbnails.append(StrippedThumbnail._parse(client, thumbnail))
|
file_type=file_type,
|
||||||
if isinstance(thumbnail, raw.types.PhotoSize):
|
dc_id=media.dc_id,
|
||||||
thumbnails.append(
|
media_id=media.id,
|
||||||
Thumbnail(
|
access_hash=media.access_hash,
|
||||||
file_id=FileId(
|
file_reference=media.file_reference,
|
||||||
file_type=file_type,
|
thumbnail_file_type=file_type,
|
||||||
dc_id=media.dc_id,
|
thumbnail_source=ThumbnailSource.THUMBNAIL,
|
||||||
media_id=media.id,
|
thumbnail_size=thumb.type,
|
||||||
access_hash=media.access_hash,
|
volume_id=0,
|
||||||
file_reference=media.file_reference,
|
local_id=0
|
||||||
thumbnail_file_type=thumbnail_file_type,
|
).encode(),
|
||||||
thumbnail_source=ThumbnailSource.THUMBNAIL,
|
file_unique_id=FileUniqueId(
|
||||||
thumbnail_size=thumbnail.type,
|
file_unique_type=FileUniqueType.DOCUMENT,
|
||||||
volume_id=thumbnail.location.volume_id,
|
media_id=media.id
|
||||||
local_id=thumbnail.location.local_id
|
).encode(),
|
||||||
).encode(),
|
width=thumb.w,
|
||||||
file_unique_id=FileUniqueId(
|
height=thumb.h,
|
||||||
file_unique_type=FileUniqueType.PHOTO,
|
file_size=thumb.size,
|
||||||
media_id=media.id,
|
client=client
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return thumbnails or None
|
return parsed_thumbs or None
|
||||||
|
@ -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
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user