Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FloodWait Exception handling #1306

Open
3 tasks done
ChuckNorrison opened this issue Jul 20, 2023 · 8 comments
Open
3 tasks done

FloodWait Exception handling #1306

ChuckNorrison opened this issue Jul 20, 2023 · 8 comments

Comments

@ChuckNorrison
Copy link

Checklist

  • I am sure the error is coming from Pyrogram's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U https://github.com/pyrogram/pyrogram/archive/master.zip and reproduced the issue using the latest development version

Description

For a download script i try to respect the telegram rate limits as documented here. This does not work cause my FloodWait exception is never reached.

Steps to reproduce

Call download_media in a loop for more than 200 files to download. Check if the exception get catched with a log and sleep the amount of time from FloodWait value.

Code example

import asyncio
from pyrogram import Client
from pyrogram.errors import FloodWait

[...]

try:
    await app.download_media(message, os.path.join(path,""), progress=progress)
except FloodWait as ex_flood:
    logger.warning("Wait %s seconds to download more media...", ex_flood.value)
    await asyncio.sleep(ex_flood.value+1)

Logs

Telegram says: [420 FLOOD_WAIT_X] - A wait of 1076 seconds is required (caused by "auth.ExportAuthorization")
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pyrogram/client.py", line 861, in get_file
    exported_auth = await self.invoke(
  File "/usr/local/lib/python3.10/site-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/usr/local/lib/python3.10/site-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.10/site-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/usr/local/lib/python3.10/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.flood_420.FloodWait: Telegram says: [420 FLOOD_WAIT_X] - A wait of 1076 seconds is required (caused by "auth.ExportAuthorization")
@qfewzz
Copy link

qfewzz commented Jul 30, 2023

I found a solution after wasting many days. lets say you have 1000 files to download. sort all the files by dc_id, then start downloading files with the new order. you will not get errors related to Authorization. The reason is let's say first file's dc_id is 4, second file is 1, third file is again 4 and so on. By starting from first file, to download the next file, every time you need to Export and import Authentication data from one server to another. I hope you understand what I said.

@ChuckNorrison
Copy link
Author

In my function i walk through get_chat_history and this is sorted by date. Than i call download_media if the message media is MessageMediaType.PHOTO.

@jgardiner68
Copy link

I do not see Pyrogram raise an error for any of the messages Telegram may send during a download_media call including Telegram internal errors, FloodWait and so on.

Is this raising an error supported for this call?

@KurimuzonAkuma
Copy link

In my function i walk through get_chat_history and this is sorted by date. Than i call download_media if the message media is MessageMediaType.PHOTO.

Better to use search_messages with photo filter.

Also you can try this PR: #1313

@jgardiner68
Copy link

I am trying to catch the exception using VS Code:
I have "justMyCode": false in my launch.json and I have enabled debugging breakpoints for Raised Exceptions, Uncaught Exceptions and User Uncaught Exceptions so the debugger should stop when there is an issue but I get FloodWait and Internal Telegram errors without the debugger stopping. Any ideas?

@nemec
Copy link

nemec commented Sep 3, 2023

The reason this error handling doesn't work is because the exception is trapped and logged within download_media

https://github.com/pyrogram/pyrogram/blob/master/pyrogram/client.py#L1020-L1021

            except Exception as e:
                log.exception(e)

The code should either re-throw the exception or return some details so that we can act on it.

@ChuckNorrison
Copy link
Author

ChuckNorrison commented Apr 8, 2024

This was fixed in #1313, thanks @KurimuzonAkuma

Works in this fork:
https://github.com/KurimuzonAkuma/pyrogram/releases/tag/v2.1.16

@jgardiner68
Copy link

Thanks for the update. Doesnt the link above need a raise e ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants