From e27e782e50baf3cb38b9b9d59e6f30b0c9b879cd Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Wed, 18 Oct 2023 15:08:10 +0300 Subject: [PATCH] New high-lvl methods for stories --- pyrogram/methods/stories/__init__.py | 4 + pyrogram/methods/stories/can_send_story.py | 54 +++++++++++ .../methods/stories/get_pinned_stories.py | 91 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 pyrogram/methods/stories/can_send_story.py create mode 100644 pyrogram/methods/stories/get_pinned_stories.py diff --git a/pyrogram/methods/stories/__init__.py b/pyrogram/methods/stories/__init__.py index aed2a56e..dfd531e8 100644 --- a/pyrogram/methods/stories/__init__.py +++ b/pyrogram/methods/stories/__init__.py @@ -17,11 +17,13 @@ # along with Pyrogram. If not, see . from .apply_boost import ApplyBoost +from .can_send_story import CanSendStory from .delete_stories import DeleteStories from .edit_story import EditStory from .export_story_link import ExportStoryLink from .get_all_stories import GetAllStories from .get_peer_stories import GetPeerStories +from .get_pinned_stories import GetPinnedStories from .get_stories import GetStories from .get_stories_archive import GetStoriesArchive from .hide_stories import HideStories @@ -32,11 +34,13 @@ from .send_story import SendStory class Stories( ApplyBoost, + CanSendStory, DeleteStories, EditStory, ExportStoryLink, GetAllStories, GetPeerStories, + GetPinnedStories, GetStories, GetStoriesArchive, HideStories, diff --git a/pyrogram/methods/stories/can_send_story.py b/pyrogram/methods/stories/can_send_story.py new file mode 100644 index 00000000..61d1abe1 --- /dev/null +++ b/pyrogram/methods/stories/can_send_story.py @@ -0,0 +1,54 @@ +# 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 . + +from typing import Union, Iterable + +import pyrogram +from pyrogram import raw +from pyrogram import types + + +class CanSendStory: + async def can_send_story( + self: "pyrogram.Client", + chat_id: Union[int, str], + ) -> bool: + """Can send story + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + Returns: + ``str``: On success, a bool is returned. + + Example: + .. code-block:: python + + # Check if you can send story to chat id + app.can_send_story(chat_id) + """ + r = await self.invoke( + raw.functions.stories.CanSendStory( + peer=await self.resolve_peer(chat_id), + ) + ) + + return r diff --git a/pyrogram/methods/stories/get_pinned_stories.py b/pyrogram/methods/stories/get_pinned_stories.py new file mode 100644 index 00000000..ff428cea --- /dev/null +++ b/pyrogram/methods/stories/get_pinned_stories.py @@ -0,0 +1,91 @@ +# 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 . + +from typing import AsyncGenerator, Union, Optional + +import pyrogram +from pyrogram import raw +from pyrogram import types + + +class GetPinnedStories: + async def get_pinned_stories( + self: "pyrogram.Client", + chat_id: Union[int, str], + offset_id: int = 0, + limit: int = 0, + ) -> Optional[AsyncGenerator["types.Story", None]]: + """Get pinned stories stories. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + offset_id (``int``, *optional*): + Offset event identifier from which to start returning results. + By default, no offset is applied and events will be returned starting from the latest. + + limit (``int``, *optional*): + Maximum amount of events to be returned. + By default, all events will be returned. + + Yields: + :obj:`~pyrogram.types.Story` objects. + + Example: + .. code-block:: python + + # Get all pinned story + async for story in app.get_pinned_stories(): + print(story) + """ + current = 0 + total = abs(limit) or (1 << 31) + limit = min(100, total) + + while True: + peer = await self.resolve_peer(chat_id) + r = await self.invoke( + raw.functions.stories.GetPinnedStories( + peer=peer, + offset_id=offset_id, + limit=limit + ) + ) + + if not r.stories: + return + + last = r.stories[-1] + offset_id = last.id + + for story in r.stories: + yield await types.Story._parse( + self, + story, + {i.id: i for i in r.users}, + {i.id: i for i in r.chats}, + peer + ) + + current += 1 + + if current >= total: + return