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,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
peer
r.stories.peer
)

View File

@ -69,16 +69,15 @@ class GetStories:
stories = []
for i in r.updates:
if isinstance(i, raw.types.stories.PeerStories):
stories = [
await types.Story._parse(
self,
story,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
peer
) for story in r.stories
]
for story in r.stories:
stories.append(
await types.Story._parse(
self,
story,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
peer
)
)
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"]
) -> "Story":
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):
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.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
video = None
from_user = None
@ -208,8 +192,40 @@ class Story(Object, Update):
video = types.Video._parse(client, doc, video_attributes, None)
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))
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:
if isinstance(priv, raw.types.PrivacyValueAllowAll):

View File

@ -19,6 +19,7 @@
import pyrogram
from pyrogram import raw, types
from pyrogram.errors import PeerIdInvalid
from typing import Union
from ..object import Object
from ..update import Update
@ -51,19 +52,51 @@ class StoryDeleted(Object, Update):
self.from_user = from_user
self.sender_chat = sender_chat
@staticmethod
async def _parse(
client: "pyrogram.Client",
stories: raw.base.StoryItem,
users: dict,
chats: dict,
peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"]
) -> "StoryDeleted":
from_user = None
sender_chat = None
if isinstance(peer, raw.types.PeerChannel):
sender_chat = await client.get_chat(peer.channel_id)
elif isinstance(peer, raw.types.InputPeerSelf):
from_user = client.me
else:
from_user = await client.get_users(peer.user_id)
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))
sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None
return StoryDeleted(
id=stories.id,

View File

@ -20,6 +20,7 @@ import pyrogram
from datetime import datetime
from pyrogram import raw, types, utils
from pyrogram.errors import PeerIdInvalid
from typing import Union
from ..object import Object
from ..update import Update
@ -67,19 +68,51 @@ class StorySkipped(Object, Update):
self.expire_date = expire_date
self.close_friends = close_friends
@staticmethod
async def _parse(
client: "pyrogram.Client",
stories: raw.base.StoryItem,
users: dict,
chats: dict,
peer: Union["raw.types.PeerChannel", "raw.types.PeerUser"]
) -> "StorySkipped":
from_user = None
sender_chat = None
if isinstance(peer, raw.types.PeerChannel):
sender_chat = await client.get_chat(peer.channel_id)
elif isinstance(peer, raw.types.InputPeerSelf):
from_user = client.me
else:
from_user = await client.get_users(peer.user_id)
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))
sender_chat = types.Chat._parse_channel_chat(client, chats[peer_id]) if not from_user else None
return StorySkipped(
id=stories.id,