sinatra和tweetstream不会一起工作

时间:2014-07-07 23:16:24

标签: ruby sinatra tweetstream

我正在尝试捕获特定主题标签的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输出。但是,如果我同时使用它们,我就无法与浏览器建立连接。

2 个答案:

答案 0 :(得分:2)

tbh我无法重现此警告。我想这可能是因为我使用ruby 2.1.1p76或一些更新宝石或因为我的操作系统。我不知道。

简单地重写您的想法,而无需保持连接。只是等待来自Twitter的回复。

require 'tweetstream'
require 'sinatra'

set :server, 'webrick'

TweetStream.configure do |config|
  config.consumer_key       = 'xxxx'
  config.consumer_secret    = 'xxxx'
  config.oauth_token        = 'xxxx'
  config.oauth_token_secret = 'xxxx'
  config.auth_method        = :oauth
end

get '/:hashtag', provides: 'text/event-stream' do
  stream :keep_open do |out|
    out << random_tweet_about(params[:hashtag])
  end
end

def random_tweet_about(hashtag)
  TweetStream::Client.new.track(hashtag) do |tweet|
    return tweet.text
  end
end

答案 1 :(得分:1)

如上所述,服务器应设置为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