Fix story parsing

This commit is contained in:
KurimuzonAkuma 2023-10-23 11:23:49 +03:00
parent e8ee3810d5
commit bd5ea1407c
5 changed files with 124 additions and 43 deletions

View File

@ -65,5 +65,5 @@ class GetPeerStories:
story, story,
{i.id: i for i in r.users}, {i.id: i for i in r.users},
{i.id: i for i in r.chats}, {i.id: i for i in r.chats},
peer r.stories.peer
) )

View File

@ -69,16 +69,15 @@ class GetStories:
stories = [] stories = []
for i in r.updates: for story in r.stories:
if isinstance(i, raw.types.stories.PeerStories): stories.append(
stories = [ await types.Story._parse(
await types.Story._parse( self,
self, story,
story, {i.id: i for i in r.users},
{i.id: i for i in r.users}, {i.id: i for i in r.chats},
{i.id: i for i in r.chats}, peer
peer )
) for story in r.stories )
]
return types.List(stories) if is_iterable else stories[0] return types.List(stories) if is_iterable else stories[0]

View File

@ -163,29 +163,13 @@ class Story(Object, Update):
peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"]
) -> "Story": ) -> "Story":
if isinstance(stories, raw.types.StoryItemSkipped): if isinstance(stories, raw.types.StoryItemSkipped):
return await types.StorySkipped._parse(client, stories, peer) return await types.StorySkipped._parse(client, stories, users, chats, peer)
if isinstance(stories, raw.types.StoryItemDeleted): if isinstance(stories, raw.types.StoryItemDeleted):
return await types.StoryDeleted._parse(client, stories, peer) return await types.StoryDeleted._parse(client, stories, users, chats, peer)
entities = [types.MessageEntity._parse(client, entity, {}) for entity in stories.entities] entities = [types.MessageEntity._parse(client, entity, {}) for entity in stories.entities]
entities = types.List(filter(lambda x: x is not None, entities)) or None entities = types.List(filter(lambda x: x is not None, entities)) or None
peer_id = utils.get_raw_peer_id(peer)
if isinstance(peer, raw.types.PeerUser) and peer_id not in users:
try:
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
await client.resolve_peer(peer_id)
]
)
)
except PeerIdInvalid:
pass
else:
users.update({i.id: i for i in r})
photo = None photo = None
video = None video = None
from_user = None from_user = None
@ -208,8 +192,40 @@ class Story(Object, Update):
video = types.Video._parse(client, doc, video_attributes, None) video = types.Video._parse(client, doc, video_attributes, None)
media_type = enums.MessageMediaType.VIDEO media_type = enums.MessageMediaType.VIDEO
peer_id = None
if hasattr(peer, "user_id"):
peer_id = peer.user_id
elif hasattr(peer, "chat_id"):
peer_id = peer.chat_id
elif hasattr(peer, "channel_id"):
peer_id = peer.channel_id
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users:
try:
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
await client.resolve_peer(peer_id)
]
)
)
except PeerIdInvalid:
pass
else:
users.update({i.id: i for i in r})
if isinstance(peer, raw.types.InputPeerSelf):
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
raw.types.InputPeerSelf()
]
)
)
peer_id = r[0].id
users.update({i.id: i for i in r})
from_user = types.User._parse(client, users.get(peer_id, None)) from_user = types.User._parse(client, users.get(peer_id, None))
sender_chat = types.Chat._parse_channel_chat(client, chats.get(peer_id, None)) if not from_user else None sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None
for priv in stories.privacy: for priv in stories.privacy:
if isinstance(priv, raw.types.PrivacyValueAllowAll): if isinstance(priv, raw.types.PrivacyValueAllowAll):

View File

@ -19,6 +19,7 @@
import pyrogram import pyrogram
from pyrogram import raw, types from pyrogram import raw, types
from pyrogram.errors import PeerIdInvalid
from typing import Union from typing import Union
from ..object import Object from ..object import Object
from ..update import Update from ..update import Update
@ -51,19 +52,51 @@ class StoryDeleted(Object, Update):
self.from_user = from_user self.from_user = from_user
self.sender_chat = sender_chat self.sender_chat = sender_chat
@staticmethod
async def _parse( async def _parse(
client: "pyrogram.Client", client: "pyrogram.Client",
stories: raw.base.StoryItem, stories: raw.base.StoryItem,
users: dict,
chats: dict,
peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"]
) -> "StoryDeleted": ) -> "StoryDeleted":
from_user = None from_user = None
sender_chat = None sender_chat = None
if isinstance(peer, raw.types.PeerChannel):
sender_chat = await client.get_chat(peer.channel_id) peer_id = None
elif isinstance(peer, raw.types.InputPeerSelf): if hasattr(peer, "user_id"):
from_user = client.me peer_id = peer.user_id
else: elif hasattr(peer, "chat_id"):
from_user = await client.get_users(peer.user_id) peer_id = peer.chat_id
elif hasattr(peer, "channel_id"):
peer_id = peer.channel_id
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users:
try:
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
await client.resolve_peer(peer_id)
]
)
)
except PeerIdInvalid:
pass
else:
users.update({i.id: i for i in r})
if isinstance(peer, raw.types.InputPeerSelf):
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
raw.types.InputPeerSelf()
]
)
)
peer_id = r[0].id
users.update({i.id: i for i in r})
from_user = types.User._parse(client, users.get(peer_id, None))
sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None
return StoryDeleted( return StoryDeleted(
id=stories.id, id=stories.id,

View File

@ -20,6 +20,7 @@ import pyrogram
from datetime import datetime from datetime import datetime
from pyrogram import raw, types, utils from pyrogram import raw, types, utils
from pyrogram.errors import PeerIdInvalid
from typing import Union from typing import Union
from ..object import Object from ..object import Object
from ..update import Update from ..update import Update
@ -67,19 +68,51 @@ class StorySkipped(Object, Update):
self.expire_date = expire_date self.expire_date = expire_date
self.close_friends = close_friends self.close_friends = close_friends
@staticmethod
async def _parse( async def _parse(
client: "pyrogram.Client", client: "pyrogram.Client",
stories: raw.base.StoryItem, stories: raw.base.StoryItem,
users: dict,
chats: dict,
peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"] peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"]
) -> "StorySkipped": ) -> "StorySkipped":
from_user = None from_user = None
sender_chat = None sender_chat = None
if isinstance(peer, raw.types.PeerChannel):
sender_chat = await client.get_chat(peer.channel_id) peer_id = None
elif isinstance(peer, raw.types.InputPeerSelf): if hasattr(peer, "user_id"):
from_user = client.me peer_id = peer.user_id
else: elif hasattr(peer, "chat_id"):
from_user = await client.get_users(peer.user_id) peer_id = peer.chat_id
elif hasattr(peer, "channel_id"):
peer_id = peer.channel_id
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser)) and peer_id not in users:
try:
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
await client.resolve_peer(peer_id)
]
)
)
except PeerIdInvalid:
pass
else:
users.update({i.id: i for i in r})
if isinstance(peer, raw.types.InputPeerSelf):
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
raw.types.InputPeerSelf()
]
)
)
peer_id = r[0].id
users.update({i.id: i for i in r})
from_user = types.User._parse(client, users.get(peer_id, None))
sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None
return StorySkipped( return StorySkipped(
id=stories.id, id=stories.id,