我正在尝试捕获特定标签的twitter流,以便将其作为另一个http流传递我编写的代码(我是Ruby新手)如下:
require 'tweetstream'
require 'sinatra'
set :server, 'webrick'
connections = []
TweetStream.configure do |config|
config.consumer_key = 'XXX'
config.consumer_secret = 'XXX'
config.oauth_token = 'XXX'
config.oauth_token_secret = 'XXX'
config.auth_method = :oauth
end
get '/hi' do
"Hello World!"
end
get '/evented', provides: 'text/event-stream' do
stream :keep_open do |out|
connections.push out
out.callback { connections.delete( out ) }
end
end
TweetStream::Client.new.track('#worldcup') do |status|
connections.each do |out|
out << "#{status.text}"
end
end
当我运行这个程序时,我得到:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/client.rb:25: warning: `*' interpreted as argument prefix
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/connection.rb:268: warning: assigned but unused variable - e
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:15: warning: method redefined; discarding old initialize
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:25: warning: previous definition of initialize was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:30: warning: method redefined; discarding old extract
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:49: warning: previous definition of extract was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:52: warning: method redefined; discarding old flush
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:100: warning: previous definition of flush was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/naught-1.0.0/lib/naught/null_class_builder/commands/predicates_return.rb:38: warning: method redefined; discarding old respond_to?
C:/Ruby193/lib/ruby/gems/1.9.1/gems/naught-1.0.0/lib/naught/null_class_builder.rb:105: warning: previous definition of respond_to? was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/pidfile.rb:39: warning: (...) interpreted as grouped expression
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/daemonize.rb:59: warning: assigned but unused variable - sess_id
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/daemonize.rb:102: warning: assigned but unused variable - pid
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/application.rb:337: warning: mismatched indentations at 'end' with 'def' at 326
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/application_group.rb:80: warning: assigned but unused variable - pid
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
奇怪的是,如果我使用sinatra并删除“TweetStream::Client.new.track”部分,一切都会正常工作在控制台中使用tweetstream输出也是如此但是,如果我同时使用它们,我就无法与浏览器建立连接。
最佳答案
如上所述,服务器应设置为thin
。
由于事件的性质,我不确定上面的random_tweet_about
函数如何工作。
我可以通过在一个TweetStream::Client.new
块中包装EM.schedule
来让tweetstream与sinatra一起工作。也许竞争的事件循环要求它在另一个线程中。
require 'sinatra'
require 'tweetstream'
set :server, 'thin'
twitter = YAML.load_file('twitter.yml')['twitter']
TweetStream.configure do |config|
config.consumer_key = twitter['CONSUMER_KEY']
config.consumer_secret = twitter['CONSUMER_SECRET']
config.oauth_token = twitter['ACCESS_TOKEN']
config.oauth_token_secret = twitter['ACCESS_SECRET']
config.auth_method = :oauth
end
connections = []
get '/tweets', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
out.callback { connections.delete(out) }
end
end
EM.schedule do
TweetStream::Client.new.track('#yolo') do |status|
connections.each do |out|
out << status.text
end
end
end