Incredibly improve downloading media on different DCs*

* This involves raising the InvalidDCError on .invoke()
when the message is FILE_MIGRATE_X, but users can always
catch this error and use the .invoke_on_dc() function
This commit is contained in:
Lonami Exo 2017-06-09 10:50:40 +02:00
parent efd1c1ba64
commit 40f4c2533a

View File

@ -252,21 +252,17 @@ class TelegramClient(TelegramBareClient):
return result return result
except InvalidDCError as e: except InvalidDCError as e:
if (e.message.startswith('PHONE_MIGRATE_') or if not e.message.startswith('FILE_MIGRATE_'):
e.message.startswith('USER_MIGRATE_')): # Only reconnect unless we're trying to download media,
# this is, on login (user migrate, phone migrate, etc.)
self._logger.info('DC error when invoking request, ' self._logger.info('DC error when invoking request, '
'attempting to reconnect at DC {}' 'attempting to reconnect at DC {}'
.format(e.new_dc)) .format(e.new_dc))
self.reconnect(new_dc=e.new_dc) self.reconnect(new_dc=e.new_dc)
return self.invoke(request, timeout=timeout) return self.invoke(request, timeout=timeout)
else: else:
self._logger.info('DC error when invoking request, ' raise
'attempting to send it on DC {}'
.format(e.new_dc))
return self.invoke_on_dc(request, e.new_dc, timeout=timeout)
finally: finally:
self._lock.release() self._lock.release()
@ -725,6 +721,35 @@ class TelegramClient(TelegramBareClient):
return file_path return file_path
def download_file(self,
input_location,
file_path,
part_size_kb=None,
file_size=None,
progress_callback=None,
on_dc=None):
if on_dc is None:
try:
super(TelegramClient, self).download_file(
input_location,
file_path,
part_size_kb=part_size_kb,
file_size=file_size,
progress_callback=progress_callback
)
except InvalidDCError as e:
on_dc = e.new_dc
if on_dc is not None:
client = self._get_exported_client(on_dc)
client.download_file(
input_location,
file_path,
part_size_kb=part_size_kb,
file_size=file_size,
progress_callback=progress_callback
)
# endregion # endregion
# endregion # endregion