From 1ff5826c26c0da5cd9dcccc1874dbd5ed03f56eb Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 14 Mar 2018 21:01:00 +0100 Subject: [PATCH] Call .get_dialogs only once on entity not found --- telethon/telegram_client.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 02bf918e..c3f8ba72 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -192,6 +192,9 @@ class TelegramClient(TelegramBareClient): # Sometimes we need to know who we are, cache the self peer self._self_input_peer = None + # Don't call .get_dialogs() every time a .get_entity() fails + self._called_get_dialogs = False + # endregion # region Telegram requests functions @@ -2401,16 +2404,18 @@ class TelegramClient(TelegramBareClient): # Add the mark to the peers if the user passed a Peer (not an int), # or said ID is negative. If it's negative it's been marked already. # Look in the dialogs with the hope to find it. - mark = not isinstance(peer, int) or peer < 0 - target_id = utils.get_peer_id(peer) - if mark: - for dialog in self.iter_dialogs(): - if utils.get_peer_id(dialog.entity) == target_id: - return utils.get_input_peer(dialog.entity) - else: - for dialog in self.iter_dialogs(): - if dialog.entity.id == target_id: - return utils.get_input_peer(dialog.entity) + if not self._called_get_dialogs: + self._called_get_dialogs = True + mark = not isinstance(peer, int) or peer < 0 + target_id = utils.get_peer_id(peer) + if mark: + for dialog in self.get_dialogs(100): + if utils.get_peer_id(dialog.entity) == target_id: + return utils.get_input_peer(dialog.entity) + else: + for dialog in self.get_dialogs(100): + if dialog.entity.id == target_id: + return utils.get_input_peer(dialog.entity) raise TypeError( 'Could not find the input entity corresponding to "{}". '