同时要求红宝石宝石朋友的Twitter速率限制打
我有麻烦打印出来的人,我在Twitter上跟随列表。这code供职于250,但现在失败,我以下320人。
I am having trouble printing out a list of people I am following on twitter. This code worked at 250, but fails now that I am following 320 people.
故障说明:的code请求超过Twitter的速率限制。在code睡用于重置极限所需的时间,然后再次尝试。
Failure Description: The code request exceeds twitter's rate limit. The code sleeps for the time required for the limit to reset, and then tries again.
我认为这是书面的方式,它只是不断重试同整个拒收的要求,而不是拿起它离开的地方。
I think the way it's written, it just keeps retrying the same entire rejectable request, rather than picking up where it left off.
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
谢谢!
以下code将返回用户名的数组。绝大多数code被写的文章:http://workstuff.tumblr.com/post/4556238101/a-short-ruby-script-to-pull-your-twitter-followers-who
The following code will return an array of usernames. The vast majority of the code was written by the author of: 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
使用以下两行第二收集您的Twitter好友
friends = get_cursor_results('friends', 'users', 'twitterusernamehere')
screen_names = friends.collect{|x| x.screen_name}