From b3c4123ae60abd96758366aa21d96fa2451435b5 Mon Sep 17 00:00:00 2001 From: Aliwoto Date: Thu, 26 Dec 2024 21:24:52 +0330 Subject: [PATCH 1/2] Add add_to_gifs method to client and its bound method to animation. Signed-off-by: Aliwoto --- pyrogram/methods/messages/add_to_gifs.py | 60 +++++++++++++++++++ .../types/messages_and_media/animation.py | 23 +++++++ 2 files changed, 83 insertions(+) create mode 100644 pyrogram/methods/messages/add_to_gifs.py diff --git a/pyrogram/methods/messages/add_to_gifs.py b/pyrogram/methods/messages/add_to_gifs.py new file mode 100644 index 0000000000..a4b520136c --- /dev/null +++ b/pyrogram/methods/messages/add_to_gifs.py @@ -0,0 +1,60 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw +from pyrogram.file_id import FileId + +class AddToGifs(): + async def add_to_gifs( + self: "pyrogram.Client", + file_id: str, + unsave: bool = False + ) -> bool: + """Add a GIF to the list of saved GIFs. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + file_id (``str``): + Unique identifier for the GIF. + + unsave (``bool``, optional): + Whether to remove the GIF from the list of saved GIFs. Defaults to ``False``. + + Returns: + ``bool``: True on success. + + Example: + .. code-block:: python + + await app.add_to_gifs(message.animation.file_id) + + """ + decoded_file_id = FileId.decode(file_id) + + return await self.invoke( + raw.functions.messages.SaveGif( + id=raw.types.InputDocument( + id=decoded_file_id.media_id, + file_reference=decoded_file_id.file_reference, + access_hash=decoded_file_id.access_hash, + ), + unsave=unsave + ) + ) \ No newline at end of file diff --git a/pyrogram/types/messages_and_media/animation.py b/pyrogram/types/messages_and_media/animation.py index 9bd5e34255..d9a69264a3 100644 --- a/pyrogram/types/messages_and_media/animation.py +++ b/pyrogram/types/messages_and_media/animation.py @@ -89,6 +89,29 @@ def __init__( self.height = height self.duration = duration self.thumbs = thumbs + + async def add_to_gifs( + self, + unsave: bool = False + ) -> bool: + """Bound method *add_to_gifs* of :obj:`~pyrogram.types.Message`. + + .. include:: /_includes/usable-by/users.rst + + Use as a shortcut for: + + .. code-block:: python + + await app.add_to_gifs(message.animation.file_id) + + Parameters: + unsave (``bool``, optional): + Whether to remove the GIF from the list of saved GIFs. Defaults to ``False``. + + Returns: + ``bool``: True on success. + """ + return await self._client.add_to_gifs(self.file_id, unsave) @staticmethod def _parse( From 15439730f91eaaa4b7ea1f9373259edf99e75741 Mon Sep 17 00:00:00 2001 From: Aliwoto Date: Sat, 28 Dec 2024 21:17:12 +0330 Subject: [PATCH 2/2] Add add_to_gifs method to docs compiler. Minor fixes to compiler.py file. Signed-off-by: Aliwoto --- compiler/api/compiler.py | 11 +++++------ compiler/docs/compiler.py | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index c151476c55..32361320bd 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -129,7 +129,7 @@ def get_type_hint(type: str) -> str: return f"Optional[{type}] = None" if is_flag else type else: ns, name = type.split(".") if "." in type else ("", type) - type = f'"raw.base.' + ".".join([ns, name]).strip(".") + '"' + type = '"raw.base.' + ".".join([ns, name]).strip(".") + '"' return f'{type}{" = None" if is_flag else ""}' @@ -394,7 +394,6 @@ def start(format: bool = False): for i, arg in enumerate(sorted_args): arg_name, arg_type = arg is_optional = FLAGS_RE.match(arg_type) - flag_number = is_optional.group(1) if is_optional else -1 arg_type = arg_type.split("?")[-1] arg_docs = combinator_docs.get(c.qualname, None) @@ -408,7 +407,7 @@ def start(format: bool = False): "{} ({}{}):\n {}\n".format( arg_name, get_docstring_arg_type(arg_type), - ", *optional*".format(flag_number) if is_optional else "", + ", *optional*" if is_optional else "", arg_docs ) ) @@ -429,11 +428,11 @@ def start(format: bool = False): if function_docs: docstring += function_docs["desc"] + "\n" else: - docstring += f"Telegram API function." + docstring += "Telegram API function." docstring += f"\n\n Details:\n - Layer: ``{layer}``\n - ID: ``{c.id[2:].upper()}``\n\n" - docstring += f" Parameters:\n " + \ - (f"\n ".join(docstring_args) if docstring_args else "No parameters required.\n") + docstring += " Parameters:\n " + \ + ("\n ".join(docstring_args) if docstring_args else "No parameters required.\n") if c.section == "functions": docstring += "\n Returns:\n " + get_docstring_arg_type(c.qualtype) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 6ad04f166f..f387bc1c56 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -208,6 +208,7 @@ def get_title_list(s: str) -> list: delete_chat_history send_paid_media send_paid_reaction + add_to_gifs """, chats=""" Chats @@ -845,6 +846,10 @@ def get_title_list(s: str) -> list: StarGift StarGift.show StarGift.hide + """, + animation=""" + Animation + Animation.add_to_gifs """ )