Skip to content

Instantly share code, notes, and snippets.

@tony612
Created May 9, 2014 17:32
Show Gist options
  • Select an option

  • Save tony612/953171a3b0886704902a to your computer and use it in GitHub Desktop.

Select an option

Save tony612/953171a3b0886704902a to your computer and use it in GitHub Desktop.
Rate limit exceeded based on Twitter wiki
def find_friend_ids(client, uid)
uid = uid.to_i
following_ids = Thread.new { client.friend_ids(uid) }
follower_ids = Thread.new { client.follower_ids(uid) }
TwitterHelper.handle_rate_limit { following_ids.value.to_a } &
TwitterHelper.handle_rate_limit { follower_ids.value.to_a }
end
class TwitterHelper
class << self
MAX_ATTEMPTS = 3
def handle_rate_limit
num_attempts = 0
begin
num_attempts += 1
yield if block_given?
rescue Twitter::Error::TooManyRequests => error
if num_attempts <= MAX_ATTEMPTS
p num_attempts, error.rate_limit.reset_in
sleep error.rate_limit.reset_in + 1
retry
else
raise
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment