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))
def get_type(peer_id: int) -> str:
def get_peer_type(peer_id: int) -> str:
if peer_id < 0:
if MIN_CHAT_ID <= peer_id:
return "chat"

View File

@ -162,7 +162,7 @@ class Chat(Object):
username=user.username,
first_name=user.first_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,
client=client
)
@ -175,7 +175,7 @@ class Chat(Object):
id=peer_id,
type="group",
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)),
members_count=getattr(chat, "participants_count", None),
client=client
@ -194,7 +194,7 @@ class Chat(Object):
is_scam=getattr(channel, "scam", None),
title=channel.title,
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,
permissions=ChatPermissions._parse(getattr(channel, "default_banned_rights", None)),
members_count=getattr(channel, "participants_count", None),

View File

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

View File

@ -187,7 +187,7 @@ class User(Object, Update):
language_code=user.lang_code,
dc_id=getattr(user.photo, "dc_id", None),
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,
client=client
)