【发布时间】:2014-07-15 05:50:49
【问题描述】:
我找到了一个脚本,可以用来创建一个推特用户关注的所有人的列表(在本例中为“@kingjames”)。曾经有一个私有方法 next_cursor 在 Twitter API 中以某种方式公开,但现在它真的是私有的,因为我无法调用它,如果我尝试它会引发错误。
删除下面脚本中对next_cursor 的所有引用会消除错误消息,但脚本无法开始添加用户。由于停止时的速率限制,该脚本必须暂停 15 分钟,因此它一遍又一遍地添加相同的用户。
您能否建议一种方法来更改脚本以使其从中断处继续,从而不会一遍又一遍地添加相同的用户?
#!/usr/bin/env ruby
require 'rubygems'
require 'twitter'
def fetch_all_friends(twitter_username, max_attempts = 100)
# in theory, one failed attempt will occur every 15 minutes, so this could be long-running
# with a long list of friends
num_attempts = 0
client = client = Twitter::REST::Client.new do |config|
config.consumer_key = "8nwjpoIsqag..."
config.consumer_secret = "Wj20rZEfPsyHd0KnW..."
config.access_token = "363090951-n5NdXfp5wWCkNU5eY..."
config.access_token_secret = "7eydU2nQHMsSVB8W76Z2PKH1P...."
end
myfile = File.new("#{twitter_username}_friends_list.txt", "w")
running_count = 0
cursor = -1
while (cursor != 0) do
begin
num_attempts += 1
# 200 is max, see https://dev.twitter.com/docs/api/1.1/get/friends/list
friends = client.friends(twitter_username, {:cursor => cursor, :count => 200} )
# friends = client.friends(twitter_username ).take(200) //seems like another way to get 200 users
friends.each do |f|
running_count += 1
myfile.puts "\"#{running_count}\",\"#{f.name.gsub('"','\"')}\",\"#{f.screen_name}\",\"#{f.id}\""
end
puts "#{running_count} done"
# cursor = friends.next_cursor
# break if cursor == 0
rescue Twitter::Error::TooManyRequests => error
if num_attempts <= max_attempts
# cursor = friends.next_cursor if friends && friends.next_cursor
puts "#{running_count} done from rescue block..."
puts "Hit rate limit, sleeping for #{error.rate_limit.reset_in}..."
sleep error.rate_limit.reset_in
retry
else
raise
end
end
end
end
fetch_all_friends("kingjames")
【问题讨论】:
-
我刚刚写了一篇博文,介绍如何使用 Yelp api here 做到这一点。让我看看它是否也适用于 Twitter
-
@anthony 好的,谢谢,请尽可能回复
-
诀窍是不要达到极限。通过故意在循环之间休眠来限制您的代码,或者找到一种方法来分解查询并在达到预定限制时暂停。爆破请求并达到限制并不是一个好的网络公民,所以学习如何让你的代码表现得更好。