0
votes

I'm using Tweepy to write a function that will return all the followers for a large Twitter account and write them to a file. I've been reading about the Twitter rate limit but it still isn't quite making sense. The documentation says "15 calls every 15 minutes, and 180 calls every 15 minutes." However, when I run my code without the sleep function I manage to get around 280 names before twitter cuts me off. So how many calls am I actually making here? My code is as follows:

import tweepy
import time

auth = tweepy.OAuthHandler("...", "...")
auth.set_access_token("...", "...")
api = tweepy.API(auth)

f = open('output.txt', 'w')
timecount = 0
for user in tweepy.Cursor(api.followers, screen_name="NAME").items():
    timecount = timecount + 1
    if timecount == 200:    
        print "HOLD ON A SECOND!!!"
        #print api.rate_limit_status()
        time.sleep(60*15)
        timecount = 0   
    data = user.screen_name 
    print user.screen_name  
    print >> f, data   
f.close()

Right now it's waiting 15 minutes between every 280 names it gets which seems to be working. Obviously, I want this to run as efficiently as possible. Can anyone help me understand how many calls I'm making how long I should be waiting?

2

2 Answers

3
votes

The Maths is pretty simple in this case, You are actually making 14 requests before twitter cuts you off, But you are able to fetch 280 names because tweepy.Cursor(api.followers, screen_name="NAME") is a single request which returns 20 values at a time, this means that you fetch 20 values for on a single request, And as you mentioned that you were able to fetch 280 names, which is not surprising as 280/20 = 14 So actually you made only 14 requests and you are just iterating over the 280 values to print out the names, etc. Kindly refer to the documentation for further details.

0
votes

I would add this in comments but I cannot due reputation and stuffs. I had the same issue a while ago. You can actually fetch up to 5000 usernames per request meaning that you can get 75000 followers every 15 min. Just increase the count to 5000. The call uses followers/list call. Here is the link.