0

我无法打印出我在 Twitter 上关注的人的列表。这段代码在 250 处有效,但现在我关注了 320 个人,所以失败了。

失败描述:代码请求超过了twitter的速率限制。代码在限制重置所需的时间内休眠,然后重试。

我认为它的编写方式只是不断重试相同的整个可拒绝请求,而不是从中断的地方继续。

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

谢谢!

4

2 回答 2

3

以下代码将返回一个用户名数组。绝大多数代码由以下作者编写: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}
于 2013-05-18T15:40:18.150 回答
2

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

于 2013-05-18T04:21:23.393 回答