NotLost
NotLost

Reputation: 101

Dealing with Twitter Rate Limit

I have a seemingly simple problem for which I am struggling to find a solution. I have a list of ~3,000 tweet ID's for which I wish to get the number of retweets, likes and the number of followers of the user.

To do this I have written the following code:

def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
    tweets.append(api.statuses_lookup(each, map=true))

However, this will exceed Twitter's rate limits. How can I introduce a 15 minute wait time when I have reached the rate limit?

Upvotes: 1

Views: 588

Answers (2)

slightly_toasted
slightly_toasted

Reputation: 335

def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
    # try to get get # of retweets
    try:
        tweets.append(api.statuses_lookup(each, map=true))
    # If it fails, wait 15 mins and 1 sec (just to be safe) and try again
    except: 
        sleep(901)
        tweets.append(api.statuses_lookup(each, map=true))

Upvotes: 2

Wes Doyle
Wes Doyle

Reputation: 2287

The tweepy API has a wait_on_rate_limit parameter which is set to False by default.

Another example for handling rate limit using cursors is provided in the tweepy docs Code Snippets.

Upvotes: 4

Related Questions