Pyrofork: Add Story support for download_media method and Add Story.download bound method

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-12-30 15:32:41 +07:00
parent 943a4d86a1
commit 29ae15e660
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
3 changed files with 80 additions and 4 deletions

View file

@ -692,6 +692,7 @@ def pyrogram_api():
story="""
Story
Story.delete
Story.download
Story.edit
Story.edit_animation
Story.edit_caption

View file

@ -31,7 +31,7 @@ DEFAULT_DOWNLOAD_DIR = "downloads/"
class DownloadMedia:
async def download_media(
self: "pyrogram.Client",
message: Union["types.Message", str],
message: Union["types.Message", "types.Story", str],
file_name: str = DEFAULT_DOWNLOAD_DIR,
in_memory: bool = False,
block: bool = True,
@ -43,8 +43,8 @@ class DownloadMedia:
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
message (:obj:`~pyrogram.types.Message` | ``str``):
Pass a Message containing the media, the media itself (message.audio, message.video, ...) or a file id
message (:obj:`~pyrogram.types.Message` | :obj:`~pyrogram.types.Story` | ``str``):
Pass a Message/Story containing the media, the media itself (message.audio, message.video, ...) or a file id
as string.
file_name (``str``, *optional*):
@ -122,7 +122,7 @@ class DownloadMedia:
available_media = ("audio", "document", "photo", "sticker", "animation", "video", "voice", "video_note",
"new_chat_photo")
if isinstance(message, types.Message):
if isinstance(message, types.Message) or isinstance(message, types.Story):
for kind in available_media:
media = getattr(message, kind, None)

View file

@ -1825,3 +1825,78 @@ class Story(Object, Update):
forward_from_chat_id=self.from_user.id if self.from_user is not None else self.sender_chat.id,
forward_from_story_id=self.id
)
async def download(
self,
file_name: str = "",
in_memory: bool = False,
block: bool = True,
progress: Callable = None,
progress_args: tuple = ()
) -> str:
"""Bound method *download* of :obj:`~pyrogram.types.Story`.
Use as a shortcut for:
.. code-block:: python
await client.download_media(story)
Example:
.. code-block:: python
await story.download()
Parameters:
file_name (``str``, *optional*):
A custom *file_name* to be used instead of the one provided by Telegram.
By default, all files are downloaded in the *downloads* folder in your working directory.
You can also specify a path for downloading files in a custom location: paths that end with "/"
are considered directories. All non-existent folders will be created automatically.
in_memory (``bool``, *optional*):
Pass True to download the media in-memory.
A binary file-like object with its attribute ".name" set will be returned.
Defaults to False.
block (``bool``, *optional*):
Blocks the code execution until the file has been downloaded.
Defaults to True.
progress (``Callable``, *optional*):
Pass a callback function to view the file transmission progress.
The function must take *(current, total)* as positional arguments (look at Other Parameters below for a
detailed description) and will be called back each time a new file chunk has been successfully
transmitted.
progress_args (``tuple``, *optional*):
Extra custom arguments for the progress callback function.
You can pass anything you need to be available in the progress callback scope; for example, a Message
object or a Client instance in order to edit the message with the updated progress status.
Other Parameters:
current (``int``):
The amount of bytes transmitted so far.
total (``int``):
The total size of the file.
*args (``tuple``, *optional*):
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:
On success, the absolute path of the downloaded file as string is returned, None otherwise.
Raises:
RPCError: In case of a Telegram RPC error.
``ValueError``: If the message doesn't contain any downloadable media
"""
return await self._client.download_media(
message=self,
file_name=file_name,
in_memory=in_memory,
block=block,
progress=progress,
progress_args=progress_args,
)