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

x-rate-limit #252

Open
muhmad-f-k opened this issue Nov 10, 2024 · 4 comments
Open

x-rate-limit #252

muhmad-f-k opened this issue Nov 10, 2024 · 4 comments

Comments

@muhmad-f-k
Copy link

Hello,

Is it possible to retrieve these fields? I’ve checked, but I can’t find any information on how to access these values each time tweets are retrieved. If it’s possible, could they be included? This would greatly help in managing the rate limit.

Fields:

x-rate-limit-limit: 50
x-rate-limit-remaining: 48
x-rate-limit-reset: 1731259969

@rainacao
Copy link

I'm able to access the headers and x-rate-limit-reset field through the exceptions class (looked through code in twikit/twikit/errors.py), but at this point x-rate-limit-remaining would always be 0. Maybe this code snippet could help somewhat? It would definitely be helpful to be able to see the x-rate-limit-remaining variable though

from twikit.errors import TooManyRequests

try:
    more_tweets = await tweets.next()
except TooManyRequests as e:
    print(f"Request limit reached.")
    if e.headers:
         print(f"Header information: {e.headers}")
    if e.rate_limit_reset:
         print(f"Rate limit reset variable: {e.rate_limit_reset}")

> Request limit reached.
> Header information: {...'x-rate-limit-limit': '50', 'x-rate-limit-reset': '1731020128', 'x-rate-limit-remaining': '0',...}
> Rate limit reset variable: 1731020128

@muhmad-f-k
Copy link
Author

muhmad-f-k commented Nov 12, 2024

Hi @rainacao , Thank you -
yes only when hit rate limit you can get thos values wich then late - will be nice to get thos value and avoid rate limit

here is the class

class TooManyRequests(TwitterException):
"""
Exception raised for 429 Too Many Requests errors.
"""
def init(self, *args, headers: dict | None = None) -> None:
super().init(*args, headers=headers)
if headers is not None and 'x-rate-limit-reset' in headers:
self.rate_limit_reset = int(headers.get('x-rate-limit-reset'))
else:
self.rate_limit_reset = None

@ne0YT
Copy link

ne0YT commented Nov 15, 2024

is there a rule of thumb when that limit is reached? like if I only check for fresh tweets of one user once a minute and most of the time I'll receive nothing a sthis person didn't tweet, is it gonna still hit the limit because of the requests?

@muhmad-f-k
Copy link
Author

is there a rule of thumb when that limit is reached? like if I only check for fresh tweets of one user once a minute and most of the time I'll receive nothing a sthis person didn't tweet, is it gonna still hit the limit because of the requests?

Yes- A request always count if you get repons or not!:)

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

No branches or pull requests

3 participants