diff --git a/pyrogram/client.py b/pyrogram/client.py index 688b1a5e..ba5cfb49 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -165,7 +165,7 @@ class Client(Methods, Scaffold): Set a sleep threshold for flood wait exceptions happening globally in this client instance, below which any request that raises a flood wait will be automatically invoked again after sleeping for the required amount of time. Flood wait exceptions requiring higher waiting times will be raised. - Defaults to 60 (seconds). + Defaults to 10 seconds. hide_password (``bool``, *optional*): Pass True to hide the password when typing it during the login. diff --git a/pyrogram/methods/advanced/send.py b/pyrogram/methods/advanced/send.py index fa6b9f44..a438a8be 100644 --- a/pyrogram/methods/advanced/send.py +++ b/pyrogram/methods/advanced/send.py @@ -27,7 +27,13 @@ log = logging.getLogger(__name__) class Send(Scaffold): - async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): + async def send( + self, + data: TLObject, + retries: int = Session.MAX_RETRIES, + timeout: float = Session.WAIT_TIMEOUT, + sleep_threshold: float = None + ): """Send raw Telegram queries. This method makes it possible to manually call every single Telegram API method in a low-level manner. @@ -50,6 +56,9 @@ class Send(Scaffold): timeout (``float``): Timeout in seconds. + sleep_threshold (``float``): + Sleep threshold in seconds. + Returns: ``RawType``: The raw type response generated by the query. @@ -65,7 +74,7 @@ class Send(Scaffold): if self.takeout_id: data = raw.functions.InvokeWithTakeout(takeout_id=self.takeout_id, query=data) - r = await self.session.send(data, retries, timeout, self.sleep_threshold) + r = await self.session.send(data, retries, timeout, sleep_threshold or self.sleep_threshold) await self.fetch_peers(getattr(r, "users", [])) await self.fetch_peers(getattr(r, "chats", [])) diff --git a/pyrogram/methods/chats/get_chat_members.py b/pyrogram/methods/chats/get_chat_members.py index e5a25f41..d3e87c7d 100644 --- a/pyrogram/methods/chats/get_chat_members.py +++ b/pyrogram/methods/chats/get_chat_members.py @@ -141,7 +141,8 @@ class GetChatMembers(Scaffold): offset=offset, limit=limit, hash=0 - ) + ), + sleep_threshold=60 ) members = r.participants diff --git a/pyrogram/methods/chats/get_dialogs.py b/pyrogram/methods/chats/get_dialogs.py index 8549fb13..01bc0fd0 100644 --- a/pyrogram/methods/chats/get_dialogs.py +++ b/pyrogram/methods/chats/get_dialogs.py @@ -66,7 +66,10 @@ class GetDialogs(Scaffold): """ if pinned_only: - r = await self.send(raw.functions.messages.GetPinnedDialogs(folder_id=0)) + r = await self.send( + raw.functions.messages.GetPinnedDialogs(folder_id=0), + sleep_threshold=60 + ) else: r = await self.send( raw.functions.messages.GetDialogs( @@ -76,7 +79,8 @@ class GetDialogs(Scaffold): limit=limit, hash=0, exclude_pinned=True - ) + ), + sleep_threshold=60 ) users = {i.id: i for i in r.users} diff --git a/pyrogram/methods/messages/get_history.py b/pyrogram/methods/messages/get_history.py index 00c9f435..130c00bb 100644 --- a/pyrogram/methods/messages/get_history.py +++ b/pyrogram/methods/messages/get_history.py @@ -20,7 +20,7 @@ import logging from typing import Union, List from pyrogram import raw -# from pyrogram import types +from pyrogram import types from pyrogram import utils from pyrogram.scaffold import Scaffold @@ -95,7 +95,8 @@ class GetHistory(Scaffold): max_id=0, min_id=0, hash=0 - ) + ), + sleep_threshold=60 ) ) diff --git a/pyrogram/methods/messages/get_messages.py b/pyrogram/methods/messages/get_messages.py index d99bcb2f..3503f16a 100644 --- a/pyrogram/methods/messages/get_messages.py +++ b/pyrogram/methods/messages/get_messages.py @@ -111,7 +111,7 @@ class GetMessages(Scaffold): else: rpc = raw.functions.messages.GetMessages(id=ids) - r = await self.send(rpc) + r = await self.send(rpc, sleep_threshold=-1) messages = await utils.parse_messages(self, r, replies=replies) diff --git a/pyrogram/methods/messages/search_global.py b/pyrogram/methods/messages/search_global.py index 60c0c752..17f6d445 100644 --- a/pyrogram/methods/messages/search_global.py +++ b/pyrogram/methods/messages/search_global.py @@ -74,7 +74,8 @@ class SearchGlobal(Scaffold): offset_peer=offset_peer, offset_id=offset_id, limit=limit - ) + ), + sleep_threshold=60 ), replies=0 ) diff --git a/pyrogram/methods/messages/search_messages.py b/pyrogram/methods/messages/search_messages.py index 44132dce..8744fe81 100644 --- a/pyrogram/methods/messages/search_messages.py +++ b/pyrogram/methods/messages/search_messages.py @@ -80,7 +80,8 @@ async def get_chunk( else None ), hash=0 - ) + ), + sleep_threshold=60 ) return await utils.parse_messages(client, r) diff --git a/pyrogram/methods/messages/send_media_group.py b/pyrogram/methods/messages/send_media_group.py index 6c9bd04d..e35984b8 100644 --- a/pyrogram/methods/messages/send_media_group.py +++ b/pyrogram/methods/messages/send_media_group.py @@ -176,7 +176,8 @@ class SendMediaGroup(Scaffold): multi_media=multi_media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id - ) + ), + sleep_threshold=60 ) return await utils.parse_messages( diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index b25b2166..bca1dc8f 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -18,12 +18,11 @@ import asyncio import logging -import time +import os from concurrent.futures.thread import ThreadPoolExecutor from datetime import datetime, timedelta from hashlib import sha1 from io import BytesIO -import os import pyrogram from pyrogram import __copyright__, __license__, __version__ @@ -32,7 +31,7 @@ from pyrogram.connection import Connection from pyrogram.crypto import mtproto from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait from pyrogram.raw.all import layer -from pyrogram.raw.core import TLObject, MsgContainer, Int, Long, FutureSalt, FutureSalts +from pyrogram.raw.core import TLObject, MsgContainer, Int, FutureSalt, FutureSalts from .internals import MsgId, MsgFactory log = logging.getLogger(__name__) @@ -48,7 +47,7 @@ class Session: INITIAL_SALT = 0x616e67656c696361 START_TIMEOUT = 1 WAIT_TIMEOUT = 15 - SLEEP_THRESHOLD = 60 + SLEEP_THRESHOLD = 10 MAX_RETRIES = 5 ACKS_THRESHOLD = 8 PING_INTERVAL = 5 @@ -443,7 +442,7 @@ class Session: except FloodWait as e: amount = e.x - if amount > sleep_threshold: + if amount > sleep_threshold > 0: raise log.warning(f'[{self.client.session_name}] Sleeping for {amount}s (required by "{query}")')