skip to Main Content

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


  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
    
    Login or Signup to reply.
  2. Generate a session_string and pass it in the app and add in_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

    async def main():
        # Create a new Client instance
        async with Client(":memory:", api_id=API_ID, api_hash=API_HASH) as app:
            # Generate the session string
            session_string = await app.export_session_string()
            print("Your session string is:", session_string)
    
    if __name__ == "__main__":
        import asyncio
        asyncio.run(main())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search