Clean-up download_profile_photo and add missing cases

This commit is contained in:
Lonami Exo 2018-05-06 11:41:42 +02:00
parent d6935355ae
commit ee1e4e18f6

View File

@ -1874,59 +1874,55 @@ class TelegramClient(TelegramBareClient):
``None`` if no photo was provided, or if it was Empty. On success ``None`` if no photo was provided, or if it was Empty. On success
the file path is returned since it may differ from the one given. the file path is returned since it may differ from the one given.
""" """
photo = entity # hex(crc32(x.encode('ascii'))) for x in
possible_names = [] # ('User', 'Chat', 'UserFull', 'ChatFull')
try: ENTITIES = (0x2da17977, 0xc5af5d94, 0x1f4661b9, 0xd49a2697)
is_entity = entity.SUBCLASS_OF_ID in ( # ('InputPeer', 'InputUser', 'InputChannel')
0x2da17977, 0xc5af5d94, 0x1f4661b9, 0xd49a2697 INPUTS = (0xc91c90b6, 0xe669bf46, 0x40f202fd)
) if not isinstance(entity, TLObject) or entity.SUBCLASS_OF_ID in INPUTS:
except AttributeError:
return None # Not even a TLObject as attribute access failed
if is_entity:
# Maybe it is an user or a chat? Or their full versions?
#
# The hexadecimal numbers above are simply:
# hex(crc32(x.encode('ascii'))) for x in
# ('User', 'Chat', 'UserFull', 'ChatFull')
entity = self.get_entity(entity) entity = self.get_entity(entity)
possible_names = []
if entity.SUBCLASS_OF_ID not in ENTITIES:
photo = entity
else:
if not hasattr(entity, 'photo'): if not hasattr(entity, 'photo'):
# Special case: may be a ChatFull with photo:Photo # Special case: may be a ChatFull with photo:Photo
# This is different from a normal UserProfilePhoto and Chat # This is different from a normal UserProfilePhoto and Chat
if hasattr(entity, 'chat_photo'): if not hasattr(entity, 'chat_photo'):
return self._download_photo(
entity.chat_photo, file,
date=None, progress_callback=None
)
else:
# Give up
return None return None
return self._download_photo(entity.chat_photo, file,
date=None, progress_callback=None)
for attr in ('username', 'first_name', 'title'): for attr in ('username', 'first_name', 'title'):
possible_names.append(getattr(entity, attr, None)) possible_names.append(getattr(entity, attr, None))
photo = entity.photo photo = entity.photo
if not isinstance(photo, UserProfilePhoto) and \ if isinstance(photo, (UserProfilePhoto, ChatPhoto)):
not isinstance(photo, ChatPhoto): loc = photo.photo_big if download_big else photo.photo_small
return None else:
try:
loc = utils.get_input_location(photo)
except TypeError:
return None
photo_location = photo.photo_big if download_big else photo.photo_small
file = self._get_proper_filename( file = self._get_proper_filename(
file, 'profile_photo', '.jpg', file, 'profile_photo', '.jpg',
possible_names=possible_names possible_names=possible_names
) )
# Download the media with the largest size input file location
try: try:
self.download_file( self.download_file(
InputFileLocation( InputFileLocation(
volume_id=photo_location.volume_id, volume_id=loc.volume_id,
local_id=photo_location.local_id, local_id=loc.local_id,
secret=photo_location.secret secret=loc.secret
), ),
file file
) )
return file
except LocationInvalidError: except LocationInvalidError:
# See issue #500, Android app fails as of v4.6.0 (1155). # See issue #500, Android app fails as of v4.6.0 (1155).
# The fix seems to be using the full channel chat photo. # The fix seems to be using the full channel chat photo.
@ -1940,7 +1936,6 @@ class TelegramClient(TelegramBareClient):
else: else:
# Until there's a report for chats, no need to. # Until there's a report for chats, no need to.
return None return None
return file
def download_media(self, message, file=None, progress_callback=None): def download_media(self, message, file=None, progress_callback=None):
""" """