使用twitter gem连接到twitter流API。
当我在sublime文本2中运行控制台中的代码时,一切正常,并从api获取结果。但是,当我尝试从终端运行脚本时,我收到此错误:
/Users/username/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/streaming/connection.rb:16:in `initialize': Can't assign requested address - connect(2) for "199.16.156.217" port (Errno::EADDRNOTAVAIL)
我只使用twitter gem的github页面中的示例代码。 https://github.com/sferik/twitter
client = Twitter::Streaming::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
client.sample do |object|
puts object.text if object.is_a?(Twitter::Tweet)
end
有谁知道为什么我会收到此错误,以及我如何解决此问题?
答案 0 :(得分:0)
这是twitter gem的一个已知问题,使用GitHub的更新版本解决了这个问题。 https://github.com/sferik/twitter/issues/709
答案 1 :(得分:0)
require 'twitter'
while true
config = {
:consumer_key => CONSUMER_KEY,
:consumer_secret => COMSUMER_SECRET,
:access_token => ACCESS_TOKEN,
:access_token_secret => ACCESS_TOKEN_SECRET,
}
sClient = Twitter::Streaming::Client.new(config)
topics = ['edelweiss', 'rose']
sClient.filter(:track => topics.join(',')) do |tweet|
if tweet.is_a?(Twitter::Tweet)
puts "#{tweet.user.screen_name}: #{tweet.text}"
end
end
end
运行代码
$ ruby lasswi.rb
Suphatra_Rfc: RT @GGiftfyy: Rose Gold ในมือนั้นอิจเเรงงงง เครื่องเก่าโยนมาทางนี้ก็ได้นะเพ่~ น้องพร้อมเสมอ #งานซูมต้องมา #อยากได้อ่ะอยากได้ http://t.…
CBullsfans: Jimmy Butler Reportedly Doesn't Respect Derrick Rose's Work Ethic http://t.co/3Ikjvvjuth #Bulls #NBA
sobinasalvez: RT @iPhoneTeam: Rose gold everything http://t.co/1DLhXokknu
magicearth_: RT @magazine_wmw: Rose-ringed parakeets in flight on their way to roost in an urban cemetery in London, England.
Photograph: Sam Hobson htt…
demoo2012: Rose, use this pic
@Razana96 http://t.co/uAwS9JdHyl
EndearingImages: New artwork for sale! - "Grace" - http://t.co/ugpIaxABqg @fineartamerica http://t.co/jg0e3eDNll
LoveKnitting: Great rose workshop with @NickyKnits at #TheKnittingandStitchingShow such a lovely lady! @twistedthread http://t.co/rV0Bsjg63t
camarillonican4: Brand New Sealed - Apple iPhone 6S Plus - 64GB - Rose Gold - UNLOCKED http://t.co/ygFPoN7pLO http://t.co/mqXKJmmrNz
Dekho00: RT @PAPIGFUNK: Giveaway ENDING on Sunday! Enter Now- iPhone 6S Plus - Rose Gold - Unboxing + Giveaway! https://t.co/02ONZ6D8IS #iPhone6SPlu…
souravmishra1: RT @RHIndia: "Only in art will the lion lie down with the lamb, and the rose grow without thorn." - Martin Amis #RandomAmis http://t.co/MT…
exol_lzw0112: RT @DOThFanclub: [Preview] 151009 ONE K Concert (cr.Like a star, Lovely Rose, Chibimori)
อันนยอง~~ http://t.co/w8vvE40dEK
BruhninhaD: Livro: Hugo & Rose da Editora Agir
Será o correto deixar a realidade para viver um sonho?http://t.co/OfxkKrzBog #books #book #livros #blog