diff --git a/.gitignore b/.gitignore index f737e858..aef0b91f 100755 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ telethon/tl/all_tlobjects.py usermedia/ api/settings +# Quick tests should live in this file +example.py + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 63252de2..d49b5855 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -145,21 +145,13 @@ class TelegramClient(TelegramBareClient): self._cached_clients.clear() - def reconnect(self, new_dc=None): - """Disconnects and connects again (effectively reconnecting). - - If 'new_dc' is not None, the current authorization key is - removed, the DC used is switched, and a new connection is made. - - *args will be ignored. - """ - super(TelegramClient, self).reconnect(new_dc=new_dc) - # endregion - # region Working with different Data Centers + # region Working with different connections - def _get_exported_client(self, dc_id, init_connection=False): + def _get_exported_client(self, dc_id, + init_connection=False, + bypass_cache=False): """Gets a cached exported TelegramBareClient for the desired DC. If it's the first time retrieving the TelegramBareClient, the @@ -168,12 +160,16 @@ class TelegramClient(TelegramBareClient): If after using the sender a ConnectionResetError is raised, this method should be called again with init_connection=True - in order to perform the reconnection.""" + in order to perform the reconnection. + + If bypass_cache is True, a new client will be exported and + it will not be cached. + """ # Thanks badoualy/kotlogram on /telegram/api/DefaultTelegramClient.kt # for clearly showing how to export the authorization! ^^ client = self._cached_clients.get(dc_id) - if client: + if client and not bypass_cache: if init_connection: client.reconnect() return client @@ -191,10 +187,37 @@ class TelegramClient(TelegramBareClient): client = TelegramBareClient(session, self.api_id, self.api_hash) client.connect(exported_auth=export_auth) - # Don't go through this expensive process every time. - self._cached_clients[dc_id] = client + if not bypass_cache: + # Don't go through this expensive process every time. + self._cached_clients[dc_id] = client return client + def create_new_connection(self, on_dc=None): + """Creates a new connection which can be used in parallel + with the original TelegramClient. A TelegramBareClient + will be returned already connected, and the caller is + responsible to disconnect it. + + If 'on_dc' is None, the new client will run on the same + data center as the current client (most common case). + + If the client is meant to be used on a different data + center, the data center ID should be specified instead. + + Note that TelegramBareClients will not handle automatic + reconnection (i.e. switching to another data center to + download media), and InvalidDCError will be raised in + such case. + """ + if on_dc is None: + client = TelegramBareClient(self.session, self.api_id, self.api_hash, + proxy=self.proxy) + client.connect() + else: + client = self._get_exported_client(on_dc, bypass_cache=True) + + return client + # endregion # region Telegram requests functions @@ -476,6 +499,8 @@ class TelegramClient(TelegramBareClient): # endregion + # region Uploading files + def send_photo_file(self, input_file, entity, caption=''): """Sends a previously uploaded input_file (which should be a photo) to the given entity (or input peer)"""