我使用Sinatra,EventMachine,DataMapper,SQLite3和Twitter Stream API來捕獲和保存推文。當我從命令行運行應用程序時,它似乎在推文50上不斷失敗。如果我沒有保存推文,它可能會永遠運行。使用Sinatra將推文保存到數據庫時收到錯誤
以下是應用程序代碼,用於捕捉帶有'oscar'的推文,它們提供了一個非常快速的流。只需輸入您的twitter用戶名和密碼並在命令行運行即可。
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
我在奧斯卡頒獎典禮上正在運行,所以也許這個流太快了?跟蹤一個不同的術語,例如每秒1條推文,應用程序運行良好。 – Braxo 2011-02-28 04:06:01