Skip to content

Commit 3947927

Browse files
committed
Add get_stories to get story information from identifiers (#47)
1 parent eecadbc commit 3947927

File tree

10 files changed

+170
-5
lines changed

10 files changed

+170
-5
lines changed

compiler/docs/compiler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ def get_title_list(s: str) -> list:
359359
get_message_effects
360360
get_stickers
361361
""",
362+
stories="""
363+
Stories
364+
get_stories
365+
""",
362366
users="""
363367
Users
364368
get_me

compiler/docs/template/methods.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ Stickers
151151

152152
{stickers}
153153

154+
Stories
155+
--------
156+
157+
.. autosummary::
158+
:nosignatures:
159+
160+
{stories}
161+
162+
.. toctree::
163+
:hidden:
164+
165+
{stories}
166+
154167
Password
155168
--------
156169

docs/source/releases/changes-in-this-fork.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
1414
| Scheme layer used: 183 |
1515
+------------------------+
1616

17+
- Added :meth:`~pyrogram.Client.get_stories`.
18+
- Added :obj:`~pyrogram.filters.thread` and :obj:`~pyrogram.filters.self_destruct`.
1719
- Added the field ``can_send_paid_media`` to the class :obj:`~pyrogram.types.Chat`.
1820
- Added support for launching Web Apps via ``t.me`` link in the class :obj:`~pyrogram.types.MenuButtonWebApp`.
1921
- `View new and changed raw API methods <https://telegramplayground.github.io/TG-APIs/TL/diff/tdesktop.html?from=181&to=183>`__.

pyrogram/dispatcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ async def story_parser(update, users, chats):
200200
users,
201201
chats,
202202
None, None,
203-
update
203+
update,
204+
None, None
204205
),
205206
StoryHandler
206207
)

pyrogram/methods/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from .password import Password
2929
from .phone import Phone
3030
from .stickers import Stickers
31+
from .stories import Stories
3132
from .users import Users
3233
from .utilities import Utilities
3334
from .business import TelegramBusiness
@@ -46,6 +47,7 @@ class Methods(
4647
Password,
4748
Phone,
4849
Stickers,
50+
Stories,
4951
TelegramBusiness,
5052
Users,
5153
Utilities,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
20+
from .get_stories import GetStories
21+
22+
class Stories(
23+
GetStories,
24+
):
25+
pass
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

pyrogram/types/input_message_content/external_reply_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ async def _parse(
291291
dice = types.Dice._parse(client, media)
292292
media_type = enums.MessageMediaType.DICE
293293
elif isinstance(media, raw.types.MessageMediaStory):
294-
story = await types.Story._parse(client, users, chats, media, None, None)
294+
story = await types.Story._parse(client, users, chats, media, None, None, None, None)
295295
media_type = enums.MessageMediaType.STORY
296296
elif isinstance(media, raw.types.MessageMediaGiveaway):
297297
giveaway = types.Giveaway._parse(client, chats, media)

pyrogram/types/messages_and_media/message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ async def _parse(
11211121
dice = types.Dice._parse(client, media)
11221122
media_type = enums.MessageMediaType.DICE
11231123
elif isinstance(media, raw.types.MessageMediaStory):
1124-
story = await types.Story._parse(client, users, chats, media, None, None)
1124+
story = await types.Story._parse(client, users, chats, media, None, None, None, None)
11251125
media_type = enums.MessageMediaType.STORY
11261126
elif isinstance(media, raw.types.MessageMediaGiveaway):
11271127
giveaway = types.Giveaway._parse(client, chats, media)
@@ -1267,7 +1267,7 @@ async def _parse(
12671267
)
12681268

12691269
if isinstance(message.reply_to, raw.types.MessageReplyStoryHeader):
1270-
parsed_message.reply_to_story = await types.Story._parse(client, users, chats, None, message.reply_to, None)
1270+
parsed_message.reply_to_story = await types.Story._parse(client, users, chats, None, message.reply_to, None, None, None)
12711271

12721272
if replies:
12731273
try:

pyrogram/types/messages_and_media/story.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ async def _parse(
206206
chats: dict,
207207
story_media: "raw.types.MessageMediaStory",
208208
reply_story: "raw.types.MessageReplyStoryHeader",
209-
story_update: "raw.types.UpdateStory"
209+
story_update: "raw.types.UpdateStory",
210+
story_item: "raw.types.StoryItem",
211+
peer: "raw.base.peer"
210212
) -> "Story":
211213
story_id = None
212214
chat = None
@@ -310,6 +312,33 @@ async def _parse(
310312
deleted
311313
) = Story._parse_story_item(client, story_update.story)
312314

315+
if peer:
316+
raw_peer_id = utils.get_raw_peer_id(peer)
317+
if isinstance(peer, raw.types.PeerUser):
318+
chat = types.Chat._parse_chat(client, users.get(raw_peer_id))
319+
else:
320+
chat = types.Chat._parse_chat(client, chats.get(raw_peer_id))
321+
322+
if story_item:
323+
story_id = getattr(story_item, "id", None)
324+
(
325+
date,
326+
expire_date,
327+
media,
328+
has_protected_content,
329+
photo,
330+
video,
331+
edited,
332+
pinned,
333+
caption,
334+
caption_entities,
335+
views,
336+
forwards,
337+
reactions,
338+
skipped,
339+
deleted
340+
) = Story._parse_story_item(client, story_item)
341+
313342
return Story(
314343
client=client,
315344
_raw=rawupdate,

0 commit comments

Comments
 (0)