From 29ae15e660d690196d34cf3fc4a20a3ad5cc7dac Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 30 Dec 2023 15:32:41 +0700 Subject: [PATCH] Pyrofork: Add Story support for download_media method and Add Story.download bound method Signed-off-by: wulan17 --- compiler/docs/compiler.py | 1 + pyrogram/methods/messages/download_media.py | 8 +-- pyrogram/types/messages_and_media/story.py | 75 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index fbfca35f..acd2fd5a 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -692,6 +692,7 @@ def pyrogram_api(): story=""" Story Story.delete + Story.download Story.edit Story.edit_animation Story.edit_caption diff --git a/pyrogram/methods/messages/download_media.py b/pyrogram/methods/messages/download_media.py index 4f44ff25..ce58a42c 100644 --- a/pyrogram/methods/messages/download_media.py +++ b/pyrogram/methods/messages/download_media.py @@ -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) diff --git a/pyrogram/types/messages_and_media/story.py b/pyrogram/types/messages_and_media/story.py index 052f7b53..ea994063 100644 --- a/pyrogram/types/messages_and_media/story.py +++ b/pyrogram/types/messages_and_media/story.py @@ -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, + )