I’m trying to download pictures from Telegram with the Pyrogram API. After some images I do get ratelimited but the code fails to catch the exception an wait the given seconds.
I keep getting these errors and it continues my for loop instead of actually waiting to download the next image. It seems to me that within download_media() there is an own try catch block that precatches the error, I am not even printing the exception?
Telegram says: [420 FLOOD_WAIT_X] - A wait of 1219 seconds is required (caused by "auth.ExportAuthorization")
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pyrogram/client.py", line 880, in get_file
dc_id=dc_id
File "/usr/local/lib/python3.7/dist-packages/pyrogram/methods/advanced/invoke.py", line 83, in invoke
else self.sleep_threshold)
File "/usr/local/lib/python3.7/dist-packages/pyrogram/session/session.py", line 389, in invoke
return await self.send(query, timeout=timeout)
File "/usr/local/lib/python3.7/dist-packages/pyrogram/session/session.py", line 357, in send
RPCError.raise_it(result, type(data))
File "/usr/local/lib/python3.7/dist-packages/pyrogram/errors/rpc_error.py", line 97, in raise_it
is_signed=is_signed)
pyrogram.errors.exceptions.flood_420.FloodWait: Telegram says: [420 FLOOD_WAIT_X] - A wait of 1219 seconds is required (caused by `"auth.ExportAuthorization")
The code is the following…
try:
downloaded_file = await app.download_media(new_message.photo, file_name=new_file_path, progress=progress)
except FloodWait as e:
print("Now waiting seconds", e.value)
await asyncio.sleep(e.value) # Wait "value" seconds before continuing
I already tried to fix it based on the how to handle flood waits given by Pyrogram (https://docs.pyrogram.org/faq/how-to-avoid-flood-waits), but the catch block is simply never called.
2
Answers
I think it is a bug from pyrogram, we just can’t catch the flood error from ‘download_media’ function. So I find another way to solve this, when show a FloodWait error, the file downloaded size usually is 0KB, so we can test whether the file size is 0KB.
Generate a
session_string
and pass it in the app and addin_memory
Bot = Client(
"Some name",
bot_token=BOT_TOKEN,
api_id=API_ID,
api_hash=API_HASH,
in_memory=True,
session_string=SESSION_STRING
)
Use the below script to Generate session-string from bot token