Created
July 2, 2013 12:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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