Skip to content

Commit a1fbdfd

Browse files
authored
Add download bound method to the class ExternalReplyInfo (#144)
1 parent 3b57c8e commit a1fbdfd

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ def get_title_list(s: str) -> list:
760760
Message.pay
761761
Message.star
762762
UserGift.toggle
763+
ExternalReplyInfo.download
763764
""",
764765
chat="""
765766
Chat

pyrogram/types/input_message_content/external_reply_info.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,96 @@ async def _parse(
338338
poll=poll,
339339
venue=venue
340340
)
341+
342+
343+
async def download(
344+
self,
345+
file_name: str = "",
346+
in_memory: bool = False,
347+
block: bool = True,
348+
idx: int = None,
349+
progress: Callable = None,
350+
progress_args: tuple = ()
351+
) -> Optional[Union[str, "io.BytesIO", list[str], list["io.BytesIO"]]]:
352+
"""Bound method *download* of :obj:`~pyrogram.types.ExternalReplyInfo`.
353+
354+
Use as a shortcut for:
355+
356+
.. code-block:: python
357+
358+
await client.download_media(message.external_reply.document)
359+
360+
Example:
361+
.. code-block:: python
362+
363+
await message.external_reply.download()
364+
365+
Parameters:
366+
file_name (``str``, *optional*):
367+
A custom *file_name* to be used instead of the one provided by Telegram.
368+
By default, all files are downloaded in the *downloads* folder in your working directory.
369+
You can also specify a path for downloading files in a custom location: paths that end with "/"
370+
are considered directories. All non-existent folders will be created automatically.
371+
372+
in_memory (``bool``, *optional*):
373+
Pass True to download the media in-memory.
374+
A binary file-like object with its attribute ".name" set will be returned.
375+
Defaults to False.
376+
377+
block (``bool``, *optional*):
378+
Blocks the code execution until the file has been downloaded.
379+
Defaults to True.
380+
381+
idx (``int``, *optional*):
382+
In case of a :obj:`~pyrogram.types.PaidMediaInfo` with more than one ``paid_media``, the zero based index of the :obj:`~pyrogram.types.PaidMedia` to download. Raises ``IndexError`` if the index specified does not exist in the original ``message``.
383+
384+
progress (``Callable``, *optional*):
385+
Pass a callback function to view the file transmission progress.
386+
The function must take *(current, total)* as positional arguments (look at Other Parameters below for a
387+
detailed description) and will be called back each time a new file chunk has been successfully
388+
transmitted.
389+
390+
progress_args (``tuple``, *optional*):
391+
Extra custom arguments for the progress callback function.
392+
You can pass anything you need to be available in the progress callback scope; for example, a Message
393+
object or a Client instance in order to edit the message with the updated progress status.
394+
395+
Other Parameters:
396+
current (``int``):
397+
The amount of bytes transmitted so far.
398+
399+
total (``int``):
400+
The total size of the file.
401+
402+
*args (``tuple``, *optional*):
403+
Extra custom arguments as defined in the ``progress_args`` parameter.
404+
You can either keep ``*args`` or add every single extra argument in your function signature.
405+
406+
Returns:
407+
``str`` | ``None`` | :obj:`io.BytesIO`: On success, the absolute path of the downloaded file is returned,
408+
otherwise, in case the download failed or was deliberately stopped with
409+
:meth:`~pyrogram.Client.stop_transmission`, None is returned.
410+
Otherwise, in case ``in_memory=True``, a binary file-like object with its attribute ".name" set is returned.
411+
If the message is a :obj:`~pyrogram.types.PaidMediaInfo` with more than one ``paid_media`` containing ``minithumbnail`` and ``idx`` is not specified, then a list of paths or binary file-like objects is returned.
412+
413+
Raises:
414+
RPCError: In case of a Telegram RPC error.
415+
IndexError: In case of wrong value of ``idx``.
416+
ValueError: If the message doesn't contain any downloadable media.
417+
418+
"""
419+
message = getattr(self, self.media.value, None)
420+
if not message:
421+
raise ValueError(
422+
f"The reply doesn't contain any downloadable media"
423+
)
424+
425+
return await self._client.download_media(
426+
message=message,
427+
file_name=file_name,
428+
in_memory=in_memory,
429+
block=block,
430+
idx=idx,
431+
progress=progress,
432+
progress_args=progress_args,
433+
)

0 commit comments

Comments
 (0)