我正在使用Sinatra,EventMachine,DataMapper,SQLite3和Twitter Stream API来捕获和保存推文。当我从命令行运行应用程序时,它似乎在推文50中不断失败。如果我不保存推文,它似乎可以永远运行。
下面是使用'oscar'捕获推文的应用程序代码,它提供了一个非常快速的流。只需输入您的推特用户名和密码,然后在命令行运行。
require 'rubygems'
require 'sinatra'
require 'em-http'
require 'json'
require 'dm-core'
require 'dm-migrations'
USERNAME = '<your twitter username>'
PASSWORD = '<your secret password>'
STREAMING_URL = 'http://stream.twitter.com/1/statuses/filter.json'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/development.db")
class Tweet
include DataMapper::Resource
property :id, Serial
property :tweet_id, String
property :username, String
property :avatar_url, String
property :text, Text
end
DataMapper.auto_upgrade!
get '/' do
@tweets = Tweet.all
erb :index
end
def rip_tweet(line)
@count += 1
tweet = Tweet.new :tweet_id => line['id'],
:username => line['user']['screen_name'],
:avatar_url => line['user']['profile_image_url'],
:text => line['text']
if tweet.save
puts @count
else
puts "F"
end
end
EM.schedule do
@count = 0
http = EM::HttpRequest.new(STREAMING_URL).get({
:head => {
'Authorization' => [ USERNAME, PASSWORD]
},
:query => {
'track' => 'oscars'
}
})
buffer = ""
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
rip_tweet JSON.parse(line)
end
end
end
helpers do
alias_method :h, :escape_html
end
答案 0 :(得分:0)
我不确定您是否可以在同一过程中安全地混合EM和Sinatra。您可能希望尝试将Sinatra查看器和EventMachine下载器拆分为单独的程序和进程。