Skip to content

Instantly share code, notes, and snippets.

@flakd
Created July 2, 2013 12:19
Show Gist options
  • Select an option

  • Save flakd/5908802 to your computer and use it in GitHub Desktop.

Select an option

Save flakd/5908802 to your computer and use it in GitHub Desktop.
Getting a list of your Twitter friends in Ruby/Rails while avoiding the Twitter rate limit error
First create the following definition.
def get_cursor_results(action, items, *args)
result = []
next_cursor = -1
until next_cursor == 0
begin
t = @client.send(action, args[0], args[1], {:cursor => next_cursor})
result = result + t.send(items)
next_cursor = t.next_cursor
rescue Twitter::Error::TooManyRequests => error
puts "Rate limit error, sleeping for #{error.rate_limit.reset_in} seconds...".color(:yellow)
sleep error.rate_limit.reset_in
retry
end
end
return result
end
Second gather your twitter friends using the following two lines
friends = get_cursor_results('friends', 'users', 'twitterusernamehere')
screen_names = friends.collect{|x| x.screen_name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment