Created
May 9, 2014 17:32
-
-
Save tony612/953171a3b0886704902a to your computer and use it in GitHub Desktop.
Rate limit exceeded based on Twitter wiki
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
| 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 |
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
| 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