From e546ae2f85632695e771c109c7cabb15350fbb8e Mon Sep 17 00:00:00 2001 From: penn5 Date: Fri, 6 Aug 2021 07:13:34 +0100 Subject: [PATCH] Allow per-request flood sleep threshold selection (#3123) --- telethon/client/telegrambaseclient.py | 5 +++++ telethon/client/users.py | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index ee40c796..e8192d73 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -837,6 +837,11 @@ class TelegramBaseClient(abc.ABC): executed sequentially on the server. They run in arbitrary order by default. + flood_sleep_threshold (`int` | `None`, optional): + The flood sleep threshold to use for this request. This overrides + the default value stored in + `client.flood_sleep_threshold ` + Returns: The result of the request (often a `TLObject`) or a list of results if more than one request was given. diff --git a/telethon/client/users.py b/telethon/client/users.py index 646914b2..49408896 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -26,10 +26,12 @@ def _fmt_flood(delay, request, *, early=False, td=datetime.timedelta): class UserMethods: - async def __call__(self: 'TelegramClient', request, ordered=False): + async def __call__(self: 'TelegramClient', request, ordered=False, flood_sleep_threshold=None): return await self._call(self._sender, request, ordered=ordered) - async def _call(self: 'TelegramClient', sender, request, ordered=False): + async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sleep_threshold=None): + if flood_sleep_threshold is None: + flood_sleep_threshold = self.flood_sleep_threshold requests = (request if utils.is_list_like(request) else (request,)) for r in requests: if not isinstance(r, TLRequest): @@ -42,7 +44,7 @@ class UserMethods: diff = round(due - time.time()) if diff <= 3: # Flood waits below 3 seconds are "ignored" self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None) - elif diff <= self.flood_sleep_threshold: + elif diff <= flood_sleep_threshold: self._log[__name__].info(*_fmt_flood(diff, r, early=True)) await asyncio.sleep(diff) self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None)