Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

workaround for Twitter api rate limiting

Tags:

ruby

twitter

I've collected a bunch of users and put them in a variable 'users'. I'm looping through them and trying to follow them with my new twitter account. However, after about 15, I'm getting stopped by Twitter for exceeding rate limit. I want to run this again but without the users that i've already followed. How do I remove 'i' from the array of 'users' after they've been followed, or somehow return a new array out of this with the users I've yet to follow? I'm aware of methods like pop and unshift etc, but I'm not sure where 'i' is coming from within the 'users' array. I'm a perpetual newbie, so please include as much detail as possible

Not, users is actually a 'cursor' and not an array, therefore, it has no length method

>> users.each do |i|
?> myuseraccount.twitter.follow(i)
>> end

Twitter::Error::TooManyRequests: Rate limit exceeded

like image 657
BrainLikeADullPencil Avatar asked Dec 26 '22 16:12

BrainLikeADullPencil


2 Answers

A simple hack would could make use of a call to sleep(n):

>> users.each do |i|
?>   myuseraccount.twitter.follow(i)
?>   sleep(3)
>> end

Increment the sleep count until twitter-api stops throwing errors.

A proper solution to this problem is achieved via rate-limiting.

A possible ruby solution for method call rate limiting would be glutton_ratelimit.

Edit - And, as Kyle has pointed out, there is a documented solution to this problem.

Below is an enhanced version of that solution:

def rate_limited_follow (account, user)
  num_attempts = 0
  begin
    num_attempts += 1
    account.twitter.follow(user)
  rescue Twitter::Error::TooManyRequests => error
    if num_attempts % 3 == 0
      sleep(15*60) # minutes * 60 seconds
      retry
    else
      retry
    end
  end
end

>> users.each do |i|
?>   rate_limited_follow(myuseraccount, i)
>> end
like image 191
Adam Eberlin Avatar answered Jan 12 '23 15:01

Adam Eberlin


There are a number of solutions, but the easiest in your case is probably shift:

while users.length > 0 do
  myuseraccount.twitter.follow(users.first)
  users.shift
end

This will remove each user from the array as they are processed.

like image 25
Mark Reed Avatar answered Jan 12 '23 17:01

Mark Reed