2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Add progress_args to download_media as well

This commit is contained in:
Dan 2018-05-06 15:45:42 +02:00
parent 00420aec92
commit f5521370bb

View File

@ -689,7 +689,7 @@ class Client:
final_file_path = "" final_file_path = ""
try: try:
media, file_name, done, progress, path = media media, file_name, done, progress, progress_args, path = media
file_id = media.file_id file_id = media.file_id
size = media.file_size size = media.file_size
@ -755,7 +755,8 @@ class Client:
local_id=local_id, local_id=local_id,
secret=secret, secret=secret,
size=size, size=size,
progress=progress progress=progress,
progress_args=progress_args
) )
if temp_file_path: if temp_file_path:
@ -2961,7 +2962,8 @@ class Client:
secret: int = None, secret: int = None,
version: int = 0, version: int = 0,
size: int = None, size: int = None,
progress: callable = None) -> str: progress: callable = None,
progress_args: tuple = None) -> str:
with self.media_sessions_lock: with self.media_sessions_lock:
session = self.media_sessions.get(dc_id, None) session = self.media_sessions.get(dc_id, None)
@ -3047,7 +3049,7 @@ class Client:
offset += limit offset += limit
if progress: if progress:
progress(min(offset, size), size) progress(self, min(offset, size), size, *progress_args)
r = session.send( r = session.send(
functions.upload.GetFile( functions.upload.GetFile(
@ -3130,7 +3132,7 @@ class Client:
offset += limit offset += limit
if progress: if progress:
progress(min(offset, size), size) progress(self, min(offset, size), size, *progress_args)
if len(chunk) < limit: if len(chunk) < limit:
break break
@ -3409,7 +3411,8 @@ class Client:
message: pyrogram_types.Message or str, message: pyrogram_types.Message or str,
file_name: str = "", file_name: str = "",
block: bool = True, block: bool = True,
progress: callable = None): progress: callable = None,
progress_args: tuple = None):
"""Use this method to download the media from a Message. """Use this method to download the media from a Message.
Args: Args:
@ -3429,16 +3432,27 @@ class Client:
progress (``callable``): progress (``callable``):
Pass a callback function to view the download progress. Pass a callback function to view the download progress.
The function must accept two arguments (current, total). The function must take *(client, current, total, \*args)* as positional arguments (look at the section
Note that this will not work in case you are downloading a media using a *file_id*. below for a detailed description).
progress_args (``tuple``):
Extra custom arguments for the progress callback function. Useful, for example, if you want to pass
a chat_id and a message_id in order to edit a message with the updated progress.
Other Parameters: Other Parameters:
client (:obj:`Client <pyrogram.Client>`):
The Client itself, useful when you want to call other API methods inside the callback function.
current (``int``): current (``int``):
The amount of bytes downloaded so far. The amount of bytes downloaded so far.
total (``int``): total (``int``):
The size of the file. The size of the file.
*args (``tuple``):
Extra custom arguments as defined in the *progress_args* parameter.
You can either keep *\*args* or add every single extra argument in your function signature.
Returns: Returns:
On success, the absolute path of the downloaded file as string is returned, None otherwise. On success, the absolute path of the downloaded file as string is returned, None otherwise.
@ -3484,7 +3498,7 @@ class Client:
done = Event() done = Event()
path = [None] path = [None]
self.download_queue.put((media, file_name, done, progress, path)) self.download_queue.put((media, file_name, done, progress, progress_args, path))
if block: if block:
done.wait() done.wait()