在请求拥有ruby gem的朋友时Twitter的速率限制

我无法打印出我在Twitter上关注的人员列表。 这段代码工作在250,但现在我跟踪了320人。

失败描述:代码请求超出了twitter的速率限制。 代码会在重置限制所需的时间内hibernate,然后再次尝试。

我认为它的编写方式,它只是不断重试相同的整个可抛弃请求,而不是从它停止的地方开始。

MAX_ATTEMPTS = 3 num_attempts = 0 begin num_attempts += 1 @client.friends.each do |user| puts "#{user.screen_name}" end rescue Twitter::Error::TooManyRequests => error if num_attempts <= MAX_ATTEMPTS sleep error.rate_limit.reset_in retry else raise end end 

谢谢!

以下代码将返回用户名数组。 绝大多数代码是由以下作者撰写的: http : //workstuff.tumblr.com/post/4556238101/a-short-ruby-script-to-pull-your-twitter-followers-who

首先创建以下定义。

 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 

其次,使用以下两行收集您的推特朋友

 friends = get_cursor_results('friends', 'users', 'twitterusernamehere') screen_names = friends.collect{|x| x.screen_name} 

尝试使用游标: http : //rdoc.info/gems/twitter/Twitter/API/FriendsAndFollowers#friends-instance_method (例如, https://gist.github.com/kent/451413 )