Question

Using the Twitter API, I get my rate_limiting information with the code I've written below:

def limit():
twitter = Twitter(auth=OAuth('....'))
g = twitter.application.rate_limit_status()
print g

This is great, but I get a big jumble like this:

{u'rate_limit_context': {u'access_token': u'....'}, u'resources': {u'account': {u'/account/verify_credentials': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/account/settings': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'blocks': {u'/blocks/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/blocks/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'users': {u'/users/contributors': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/lookup': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/search': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/suggestions/:slug/members': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/suggestions/:slug': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/show/:id': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/suggestions': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/users/profile_banner': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/users/contributees': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'friends': {u'/friends/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friends/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'saved_searches': {u'/saved_searches/show/:id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/saved_searches/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/saved_searches/destroy/:id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'lists': {u'/lists/subscriptions': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/subscribers/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/statuses': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/subscribers': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/members/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/memberships': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/lists/members': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/lists/ownerships': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'search': {u'/search/tweets': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}}, u'application': {u'/application/rate_limit_status': {u'reset': 1371673401, u'limit': 180, u'remaining': 178}}, u'trends': {u'/trends/available': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/trends/closest': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/trends/place': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'followers': {u'/followers/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/followers/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'favorites': {u'/favorites/list': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'friendships': {u'/friendships/outgoing': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/show': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/friendships/incoming': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/no_retweets/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/friendships/lookup': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'geo': {u'/geo/similar_places': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/id/:place_id': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/reverse_geocode': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/geo/search': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'direct_messages': {u'/direct_messages/show': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages/sent': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/direct_messages/sent_and_received': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}, u'statuses': {u'/statuses/retweets_of_me': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/retweeters/ids': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/mentions_timeline': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/user_timeline': {u'reset': 1371672792, u'limit': 180, u'remaining': 177}, u'/statuses/oembed': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/statuses/show/:id': {u'reset': 1371673471, u'limit': 180, u'remaining': 180}, u'/statuses/home_timeline': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/statuses/retweets/:id': {u'reset': 1371672792, u'limit': 15, u'remaining': 0}}, u'help': {u'/help/tos': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/configuration': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/privacy': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}, u'/help/languages': {u'reset': 1371673471, u'limit': 15, u'remaining': 15}}}}

I only need 1 part from it though. I just need to know the remaining limit on this:

u'/statuses/retweets/:id': {u'reset': 1371672792, u'limit': 15, u'remaining': 0}

How do I get just that number?

Was it helpful?

Solution

Without knowing the details of the Twitter API, it looks like the rate_limit_status() method is returning a dictionary of dictionaries. So you should be able to get at the data you want by looking up the right keys:

>>> g = eval("{u'rate_limit_context': {u'access_token': u'....'}, u'resources': ... ")
>>> g[u'resources'][u'statuses'][u'/statuses/retweets/:id']
{u'limit': 15, u'remaining': 0, u'reset': 1371672792}
>>> g[u'resources'][u'statuses'][u'/statuses/retweets/:id'][u'remaining']
0

OTHER TIPS

This problem has been frustrating me for the last 72 hours. First, we should clarify here - you are using the Tweepy library, correct? I had some code using the twitter package found at the top of the list in PyPI, but it doesn't appear that they made the upgrade from API 1.0 to API 1.1, nor does the #2 in popularity python-twitter appear to be ready, despite the older API sunsetting over a month ago.

The entire rate limit scheme has changed from API 1.0 to API 1.1 - previously you had 350 general purpose tokens per hour and the system reset on the hour. The prior ability to get 150 API tokens worth of search based on source IP w/o authentication is gone. Now time increments are fifteen minutes and you get 15 posts and 180 gets.

https://dev.twitter.com/docs/rate-limiting/1.1

The new API provides granularity to the level of each API access point - instead of just a bucket of 350 tokens it looks like every conceivable post type action is limited by type to 15 per each quarter hour, and get type actions are limited to 180 per quarter hour. It looks like Twitter will have a much better handle on API usage accounting. For my purposes it appears that capacity is nearly double, from 350 calls/hour to (4x180 = 720) calls/hour. Thank you, Twitter.

The access method the other responder described is ugly - we have to dig four levels deep into dictionaries for what is conceptually a dictionary of endpoint/count tuples? If that's the full structure, so be it, but there really needs to be a simple method of naming the desired endpoint and getting back an integer. That would be readable, this muddle of dictionary references is not.

You can see an example of the full json structure of the returned object you describe on the Twitter docs, here

As has already been mentioned here by andersschuller, in Python you can get to the object you want simply by calling it's keys, g[u'resources'][u'statuses'][u'/statuses/retweets/:id']

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top