我找到了一个脚本,我可以使用它创建一个Twitter用户关注的所有人的列表(在这种情况下" @ 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")
答案 0 :(得分:0)
曾经有一个私有方法next_cursor以某种方式公开 在Twitter api,但现在它真的是私人的,你不能打电话 它会在你尝试时抛出错误。
ruby对调用私有方法没有限制。唯一的限制是,当您调用私有方法时,无法显式指定接收器。这是一个例子:
class Dog
private
def next_cursor
puts "I'm the next cursor."
end
end
friends = Dog.new
friends.next_cursor
--output:--
1.rb:10:in `<main>': private method `next_cursor' called for #<Dog:0x00000100a40fc8> (NoMethodError)
friends.instance_eval do
next_cursor #You can't explicitly specify a receiver for a private method,
#and ruby uses whatever object is self as the receiver
end
--output:--
I'm the next cursor.
如果您不了解Ruby的细节,那么该代码看起来会让人感到困惑。但Ruby还提供了一个名为send()的方法,它允许您调用任何方法:
friends.send(:next_cursor)
--output:--
I'm the next cursor.