2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-02 15:25:41 +00:00

Update ChatPhoto parser

This commit is contained in:
Dan
2019-09-14 19:30:07 +02:00
parent c5498c3b4e
commit 840a9d1cc7
4 changed files with 10 additions and 18 deletions

View File

@@ -215,7 +215,7 @@ def get_peer_id(peer: Union[PeerUser, PeerChat, PeerChannel]) -> int:
raise ValueError("Peer type invalid: {}".format(peer)) raise ValueError("Peer type invalid: {}".format(peer))
def get_type(peer_id: int) -> str: def get_peer_type(peer_id: int) -> str:
if peer_id < 0: if peer_id < 0:
if MIN_CHAT_ID <= peer_id: if MIN_CHAT_ID <= peer_id:
return "chat" return "chat"

View File

@@ -162,7 +162,7 @@ class Chat(Object):
username=user.username, username=user.username,
first_name=user.first_name, first_name=user.first_name,
last_name=user.last_name, last_name=user.last_name,
photo=ChatPhoto._parse(client, user.photo, peer_id), photo=ChatPhoto._parse(client, user.photo, peer_id, user.access_hash),
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None, restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
client=client client=client
) )
@@ -175,7 +175,7 @@ class Chat(Object):
id=peer_id, id=peer_id,
type="group", type="group",
title=chat.title, title=chat.title,
photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id), photo=ChatPhoto._parse(client, getattr(chat, "photo", None), peer_id, 0),
permissions=ChatPermissions._parse(getattr(chat, "default_banned_rights", None)), permissions=ChatPermissions._parse(getattr(chat, "default_banned_rights", None)),
members_count=getattr(chat, "participants_count", None), members_count=getattr(chat, "participants_count", None),
client=client client=client
@@ -194,7 +194,7 @@ class Chat(Object):
is_scam=getattr(channel, "scam", None), is_scam=getattr(channel, "scam", None),
title=channel.title, title=channel.title,
username=getattr(channel, "username", None), username=getattr(channel, "username", None),
photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id), photo=ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id, channel.access_hash),
restrictions=pyrogram.List([Restriction._parse(r) for r in restriction_reason]) or None, restrictions=pyrogram.List([Restriction._parse(r) for r in restriction_reason]) or None,
permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)), permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)),
members_count=getattr(channel, "participants_count", None), members_count=getattr(channel, "participants_count", None),

View File

@@ -20,7 +20,7 @@ from struct import pack
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.errors import PeerIdInvalid from pyrogram.client.ext import utils
from ..object import Object from ..object import Object
from ...ext.utils import encode from ...ext.utils import encode
@@ -51,7 +51,7 @@ class ChatPhoto(Object):
self.big_file_id = big_file_id self.big_file_id = big_file_id
@staticmethod @staticmethod
def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int): def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_id: int, peer_access_hash: int):
if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)): if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)):
return None return None
@@ -59,22 +59,14 @@ class ChatPhoto(Object):
loc_small = chat_photo.photo_small loc_small = chat_photo.photo_small
loc_big = chat_photo.photo_big loc_big = chat_photo.photo_big
try: peer_type = utils.get_peer_type(peer_id)
peer = client.resolve_peer(peer_id)
except PeerIdInvalid:
return None
if isinstance(peer, types.InputPeerUser): if peer_type == "user":
peer_id = peer.user_id
peer_access_hash = peer.access_hash
x = 0 x = 0
elif isinstance(peer, types.InputPeerChat): elif peer_type == "chat":
peer_id = -peer.chat_id
peer_access_hash = 0
x = -1 x = -1
else: else:
peer_id += 1000727379968 peer_id += 1000727379968
peer_access_hash = peer.access_hash
x = -234 x = -234
return ChatPhoto( return ChatPhoto(

View File

@@ -187,7 +187,7 @@ class User(Object, Update):
language_code=user.lang_code, language_code=user.lang_code,
dc_id=getattr(user.photo, "dc_id", None), dc_id=getattr(user.photo, "dc_id", None),
phone_number=user.phone, phone_number=user.phone,
photo=ChatPhoto._parse(client, user.photo, user.id), photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash),
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None, restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
client=client client=client
) )