|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import Union, Iterable |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import raw, types |
| 23 | + |
| 24 | + |
| 25 | +class GetStories: |
| 26 | + async def get_stories( |
| 27 | + self: "pyrogram.Client", |
| 28 | + story_sender_chat_id: Union[int, str], |
| 29 | + story_ids: Union[int, Iterable[int]], |
| 30 | + ) -> "types.Story": |
| 31 | + """Get one or more stories from a chat by using stories identifiers. |
| 32 | +
|
| 33 | + .. include:: /_includes/usable-by/users.rst |
| 34 | +
|
| 35 | + Parameters: |
| 36 | + story_sender_chat_id (``int`` | ``str``): |
| 37 | + Identifier of the chat that posted the story. |
| 38 | +
|
| 39 | + story_ids (``int`` | Iterable of ``int``, *optional*): |
| 40 | + Pass a single story identifier or an iterable of story ids (as integers) to get the content of the |
| 41 | + story themselves. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + :obj:`~pyrogram.types.Story` | List of :obj:`~pyrogram.types.Story`: In case *story_ids* was not |
| 45 | + a list, a single story is returned, otherwise a list of stories is returned. |
| 46 | +
|
| 47 | + Example: |
| 48 | + .. code-block:: python |
| 49 | +
|
| 50 | + # Get stories by id |
| 51 | + stories = await app.get_stories( |
| 52 | + story_sender_chat_id, |
| 53 | + [1, 2, 3] |
| 54 | + ) |
| 55 | +
|
| 56 | + for story in stories: |
| 57 | + print(story) |
| 58 | + """ |
| 59 | + is_iterable = not isinstance(story_ids, int) |
| 60 | + ids = list(story_ids) if is_iterable else [story_ids] |
| 61 | + |
| 62 | + peer = await self.resolve_peer(story_sender_chat_id) |
| 63 | + r = await self.invoke( |
| 64 | + raw.functions.stories.GetStoriesByID( |
| 65 | + peer=peer, |
| 66 | + id=ids |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + stories = [] |
| 71 | + |
| 72 | + users = {i.id: i for i in r.users} |
| 73 | + chats = {i.id: i for i in r.chats} |
| 74 | + |
| 75 | + for story in r.stories: |
| 76 | + stories.append( |
| 77 | + await types.Story._parse( |
| 78 | + self, |
| 79 | + users, |
| 80 | + chats, |
| 81 | + None, None, None, |
| 82 | + # TODO |
| 83 | + story, |
| 84 | + None, # |
| 85 | + # TODO |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + return types.List(stories) if is_iterable else stories[0] if stories else None |
0 commit comments