Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 - Tweepy - How to get rate_limit_status()?

I am working on a twitter App using Python 2.7 and the latest version of the tweepy module. One thing I cannot figure out is how to use the function rate_limit_status()

Here is my code:

import tweepy, time, sys, random, pickle
import pprint

# argfile = str(sys.argv[1])

#enter the corresponding information from your Twitter application:
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

api = tweepy.API(auth)
public_tweets = api.home_timeline()
user = api.get_user('@MyUserName')

print api.rate_limit_status()

When I print the results of the function it gives me a large dictionary that I cannot decipher. I have looked at the tweepy documentation but can't find any good examples on using rate_limit_status().

What is the next step I should be doing to troubleshoot something like this?

Is there a tool to format these large dictionaries so I can read them and try to decipher how to access the values in the dictionary?

Edit:

It turns out I didn't have a good understanding of what a Rest API is and how simply it works! I was expecting something MUCH more complicated in my head.

I actually switched to the twitter Python module twitter library instead of Tweepy and then did a lot of research on how to use the Twitter API.

Two youtube videos that REALLY helped me are:

https://www.youtube.com/watch?v=7YcW25PHnAA

and

https://www.youtube.com/watch?v=fhPb6ocUz_k

The Postman Chrome app was awesome and allowed me to easily test and visualize how my calls to the Twitter API worked and it easily formatted the resulting JSON to so I could read it.

To do quick calculations I also took the JSON from Postman and threw it into this website http://konklone.io/json/ to get a csv that I could then open in Excel and make sure everything was behaving as expected and that I was getting the right now number of results.

After all that, writing the Python code to interact with the Twitter API was easy!

Adding all this in this hopes it will help someone else in the future! If it does please let me know! :)

like image 458
timbram Avatar asked May 18 '15 21:05

timbram


People also ask

How can I get more than 100 tweets on Tweepy?

If you need more than 100 Tweets, you have to use the paginator method and specify the limit i.e. the total number of Tweets that you want. Replace limit=1000 with the maximum number of tweets you want. Replace the limit=1000 with the maximum number of tweets you want (gist).

How do I get my tweets from Tweepy?

Steps to obtain keys: – For access token, click ” Create my access token”. The page will refresh and generate access token. Tweepy is one of the library that should be installed using pip. Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface.


1 Answers

As per the Tweepy documentation

Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour. Calls to rate_limit_status do not count against the rate limit. If authentication credentials are provided, the rate limit status for the authenticating user is returned. Otherwise, the rate limit status for the requester’s IP address is returned.

So in simpler words you can say that, it returns a JSON object in which tells you about the Number of requests you have made and Number of requests remaining, the reason why it is difficult to read at first sight lies in the face that, it contains the count for every type of API call that you have made and not only the current API call you just executed.

So for example if you run the above script, then, you can see that you have made a call to api.home_timeline() Now according to the twitter Rules and Regulations you can only make 15 calls to this method in a given window session, So if you unpack the JSON object returned then you can see that, there is a lot of data but if you analyse the data then, You will find that api.home_timeline() only affects limits of relevant methods, such as when calling above methods you can check the rate limit using:

data = api.rate_limit_status()

print data['resources']['statuses']['/statuses/home_timeline']
print data['resources']['users']['/users/lookup']

However you have to do a little bit of research on the JSON returned and then you can extract the relevant data from the JSON object, as the returned JSON objects are hard to read, you can always use these types of links to make it more user readable and then analyse it.

like image 64
ZdaR Avatar answered Nov 03 '22 05:11

ZdaR