mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-04 20:33:09 +03:00
Support PhotoSize in .download_media (#669)
This simplifies downloading thumbnails (and any other PhotoSize).
This commit is contained in:
parent
e3adec5ea9
commit
dd6802e032
|
@ -82,8 +82,8 @@ from .tl.types import (
|
||||||
InputDocument, InputMediaDocument, Document, MessageEntityTextUrl,
|
InputDocument, InputMediaDocument, Document, MessageEntityTextUrl,
|
||||||
InputMessageEntityMentionName, DocumentAttributeVideo,
|
InputMessageEntityMentionName, DocumentAttributeVideo,
|
||||||
UpdateEditMessage, UpdateEditChannelMessage, UpdateShort, Updates,
|
UpdateEditMessage, UpdateEditChannelMessage, UpdateShort, Updates,
|
||||||
MessageMediaWebPage, ChannelParticipantsSearch
|
MessageMediaWebPage, ChannelParticipantsSearch, PhotoSize, PhotoCachedSize,
|
||||||
)
|
PhotoSizeEmpty)
|
||||||
from .tl.types.messages import DialogsSlice
|
from .tl.types.messages import DialogsSlice
|
||||||
from .extensions import markdown, html
|
from .extensions import markdown, html
|
||||||
|
|
||||||
|
@ -1720,7 +1720,8 @@ class TelegramClient(TelegramBareClient):
|
||||||
date = datetime.now()
|
date = datetime.now()
|
||||||
media = message
|
media = message
|
||||||
|
|
||||||
if isinstance(media, (MessageMediaPhoto, Photo)):
|
if isinstance(media, (MessageMediaPhoto, Photo,
|
||||||
|
PhotoSize, PhotoCachedSize)):
|
||||||
return self._download_photo(
|
return self._download_photo(
|
||||||
media, file, date, progress_callback
|
media, file, date, progress_callback
|
||||||
)
|
)
|
||||||
|
@ -1738,24 +1739,39 @@ class TelegramClient(TelegramBareClient):
|
||||||
# Determine the photo and its largest size
|
# Determine the photo and its largest size
|
||||||
if isinstance(photo, MessageMediaPhoto):
|
if isinstance(photo, MessageMediaPhoto):
|
||||||
photo = photo.photo
|
photo = photo.photo
|
||||||
if not isinstance(photo, Photo):
|
if isinstance(photo, Photo):
|
||||||
|
for size in reversed(photo.sizes):
|
||||||
|
if not isinstance(size, PhotoSizeEmpty):
|
||||||
|
photo = size
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
if not isinstance(photo, (PhotoSize, PhotoCachedSize)):
|
||||||
return
|
return
|
||||||
|
|
||||||
largest_size = photo.sizes[-1]
|
|
||||||
file_size = largest_size.size
|
|
||||||
largest_size = largest_size.location
|
|
||||||
|
|
||||||
file = self._get_proper_filename(file, 'photo', '.jpg', date=date)
|
file = self._get_proper_filename(file, 'photo', '.jpg', date=date)
|
||||||
|
if isinstance(photo, PhotoCachedSize):
|
||||||
|
# No need to download anything, simply write the bytes
|
||||||
|
if isinstance(file, str):
|
||||||
|
helpers.ensure_parent_dir_exists(file)
|
||||||
|
f = open(file, 'wb')
|
||||||
|
else:
|
||||||
|
f = file
|
||||||
|
try:
|
||||||
|
f.write(photo.bytes)
|
||||||
|
finally:
|
||||||
|
if isinstance(file, str):
|
||||||
|
f.close()
|
||||||
|
return file
|
||||||
|
|
||||||
# Download the media with the largest size input file location
|
|
||||||
self.download_file(
|
self.download_file(
|
||||||
InputFileLocation(
|
InputFileLocation(
|
||||||
volume_id=largest_size.volume_id,
|
volume_id=photo.location.volume_id,
|
||||||
local_id=largest_size.local_id,
|
local_id=photo.location.local_id,
|
||||||
secret=largest_size.secret
|
secret=photo.location.secret
|
||||||
),
|
),
|
||||||
file,
|
file,
|
||||||
file_size=file_size,
|
file_size=photo.size,
|
||||||
progress_callback=progress_callback
|
progress_callback=progress_callback
|
||||||
)
|
)
|
||||||
return file
|
return file
|
||||||
|
|
Loading…
Reference in New Issue
Block a user