python-twitter-tools / twitter

Python Twitter API
http://mike.verdone.ca/twitter/
MIT License
3.17k stars 710 forks source link

How to get X-Rate-Limit-Reset after hitting the API limit? #264

Closed surfarcher closed 9 years ago

surfarcher commented 9 years ago

Because write limits aren't returned in /application/rate_limit_status we are forced to hit the current limit (it's variable in a lot of cases) and then check the X headers.

For example add enough tweeps (generally somewhere near 30) to a list with... t.lists.members.create(list_id=listId, user_id=userId) and you'll get a 104 "You aren't allowed to add members to this list".

If you wrap the attempt in a try and grab it in an except you should be able to get the X-Rate-Limit-Reset to tell you when you can try again... try: t.lists.members.create(list_id=listId, user_id=userId) except (TwitterHTTPError) as ex: pprint(t.response.rate_limit_reset) pprint(t.response.headers.get('X-Rate-Limit-Reset', "0")) pprint(ex.e.headers.get('X-Rate-Limit-Reset', "0"))

Well there are three of the many things I have tried with no luck.

Any thoughts anyone?

surfarcher commented 9 years ago

And now I am write limited.

sigh

RouxRC commented 9 years ago

Hi, it's a bit hidden, but you can access it through TwitterHTTPError's e.headers, for instance:

while True:                                                                          
    try:                                                                             
        assert(type(t.direct_messages()) == list)                                    
    except TwitterHTTPError as e:                                                    
        print("Rate-limit reached until " + e.e.headers.get("x-rate-limit-reset"))   
        break