1

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.

5
  • Did you perform the import? from pyrogram.errors import FloodWait ? See here for pyrogram error handling: docs.pyrogram.org/start/errors Commented Jul 5, 2023 at 8:13
  • Yes, I import the error with from pyrogram.errors import FloodWait, followed all the steps here, still not working @Swifty Commented Jul 5, 2023 at 8:17
  • Sorry then, I have no further idea at this time. Commented Jul 5, 2023 at 8:19
  • There is no re-raising FloodWait exception in code - just logging. Feel free to search in "issues" sectrion on pyrogram github repo and apply some PR to your local installation in order to get it work :( Commented Jan 24, 2024 at 18:43
  • Notice that the traceback does not include your code at all. There's code in another thread that's triggering the error. Commented Jun 22, 2024 at 23:43

2 Answers 2

0

If we look at the code that traceback points to, specifically the first line, we can see it's pointing to pyrogram.client.Client.get_file. Line 880 is inside a giant try-catch block, 150 lines long:

try:
    await session.start()

    if dc_id != await self.storage.dc_id():
        exported_auth = await self.invoke(
            raw.functions.auth.ExportAuthorization(
                dc_id=dc_id # line 880
            )
        )
# [over 100 lines abridged]
except pyrogram.StopTransmission:
    raise
except Exception as e:
    log.exception(e)
finally:
    await session.stop()

that catches, logs, and otherwise ignores any exception other than pyrogram.StopTransmission.

You're not going to be able to catch this exception. Pyrogram is already catching it. If you wanted to do something other than log it, well, Pyrogram has made that decision for you.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

def is_file_empty(file_path):
    if os.path.exists(file_path):
        return os.path.getsize(file_path) == 0
    else:
        return True

flag = True
while flag:
    file_path = app.download_media(file_id)
    if file_path and not is_file_empty(file_path):  
        flag = False
        # solve success situation
    else:
        time.sleep(10)
        # sleep for 10 seconds and retry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.