mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2026-01-08 16:04:51 +00:00
Add support for downloading photos not contained inside a Message
This commit is contained in:
parent
a8664c9315
commit
f997e97494
1 changed files with 56 additions and 6 deletions
|
|
@ -337,13 +337,17 @@ class Client:
|
||||||
size=document.size,
|
size=document.size,
|
||||||
progress=progress
|
progress=progress
|
||||||
)
|
)
|
||||||
elif isinstance(media, types.MessageMediaPhoto):
|
elif isinstance(media, (types.MessageMediaPhoto, types.Photo)):
|
||||||
photo = media.photo
|
if isinstance(media, types.MessageMediaPhoto):
|
||||||
|
photo = media.photo
|
||||||
|
else:
|
||||||
|
photo = media
|
||||||
|
|
||||||
if isinstance(photo, types.Photo):
|
if isinstance(photo, types.Photo):
|
||||||
if not file_name:
|
if not file_name:
|
||||||
file_name = "photo_{}.jpg".format(
|
file_name = "photo_{}_{}.jpg".format(
|
||||||
datetime.fromtimestamp(photo.date).strftime("%Y-%m-%d_%H-%M-%S")
|
datetime.fromtimestamp(photo.date).strftime("%Y-%m-%d_%H-%M-%S"),
|
||||||
|
self.rnd_id()
|
||||||
)
|
)
|
||||||
|
|
||||||
photo_loc = photo.sizes[-1].location
|
photo_loc = photo.sizes[-1].location
|
||||||
|
|
@ -2587,11 +2591,15 @@ class Client:
|
||||||
Raises:
|
Raises:
|
||||||
:class:`pyrogram.Error`
|
:class:`pyrogram.Error`
|
||||||
"""
|
"""
|
||||||
if isinstance(message, types.Message):
|
if isinstance(message, (types.Message, types.Photo)):
|
||||||
done = Event()
|
done = Event()
|
||||||
media = message.media
|
|
||||||
path = [None]
|
path = [None]
|
||||||
|
|
||||||
|
if isinstance(message, types.Message):
|
||||||
|
media = message.media
|
||||||
|
else:
|
||||||
|
media = message
|
||||||
|
|
||||||
if media is not None:
|
if media is not None:
|
||||||
self.download_queue.put((media, file_name, done, progress, path))
|
self.download_queue.put((media, file_name, done, progress, path))
|
||||||
else:
|
else:
|
||||||
|
|
@ -2602,6 +2610,48 @@ class Client:
|
||||||
|
|
||||||
return path[0]
|
return path[0]
|
||||||
|
|
||||||
|
def download_photo(self,
|
||||||
|
photo: types.Photo or types.UserProfilePhoto or types.ChatPhoto,
|
||||||
|
file_name: str = None,
|
||||||
|
block: bool = True):
|
||||||
|
"""Use this method to download a photo not contained inside a Message.
|
||||||
|
For example, a photo of a User or a Chat/Channel.
|
||||||
|
|
||||||
|
Photos are saved in the *downloads* folder.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
photo (:obj:`Photo <pyrogram.api.types.Photo>` | :obj:`UserProfilePhoto <pyrogram.api.types.UserProfilePhoto>` | :obj:`ChatPhoto <pyrogram.api.types.ChatPhoto>`):
|
||||||
|
The photo object.
|
||||||
|
|
||||||
|
file_name (:obj:`str`, optional):
|
||||||
|
Specify a custom *file_name* to be used.
|
||||||
|
|
||||||
|
block (:obj:`bool`, optional):
|
||||||
|
Blocks the code execution until the photo has been downloaded.
|
||||||
|
Defaults to True.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The relative path of the downloaded photo.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
:class:`pyrogram.Error`
|
||||||
|
"""
|
||||||
|
if isinstance(photo, (types.UserProfilePhoto, types.ChatPhoto)):
|
||||||
|
photo = types.Photo(
|
||||||
|
id=0,
|
||||||
|
access_hash=0,
|
||||||
|
date=int(time.time()),
|
||||||
|
sizes=[types.PhotoSize(
|
||||||
|
type="",
|
||||||
|
location=photo.photo_big,
|
||||||
|
w=0,
|
||||||
|
h=0,
|
||||||
|
size=0
|
||||||
|
)]
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.download_media(photo, file_name, block)
|
||||||
|
|
||||||
def add_contacts(self, contacts: list):
|
def add_contacts(self, contacts: list):
|
||||||
"""Use this method to add contacts to your Telegram address book.
|
"""Use this method to add contacts to your Telegram address book.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue