2014-02-09 45 views
0

我需要推送推文並將其存儲在mongodb中進行處理。我已經安裝了紅寶石,mongo和tweetstream寶石。使用紅寶石提取推文

我運行下面的代碼來提取推文,並將它存儲在「tweet」數據庫的名爲「users」的集合中。這裏是rawks.rb

require "tweetstream" 
require "mongo" 
require "time" 
db = Mongo::Connection.new("localhost", 27017).db("tweet") 
tweets = db.collection("users") 
TweetStream::Daemon.new("username","password","scrapedaemon").on_error do |message| 
# Log your error message somewhere 
end.filter({"locations" => "-12.72216796875, 49.76707407366789, 1.977539, 61.068917"}) do |status| 
# Do things when nothing's wrong 
data = {"created_at" => Time.parse(status.created_at), "text" => status.text, "geo" =>  status.geo, "coordinates" => status.coordinates, "id" => status.id, "id_str" => status.id_str} 
tweets.insert({"data" => data}); 
end 

當我運行這個文件,我得到以下錯誤的程序: 從rawks.rb:8:在 '新' rawks.rb:8:'

在文件daemon.rb,40:在 '初始化' 錯誤的參數數目(3 2)的參數錯誤

這裏是daemon.rb文件

require 'daemons' 

# A daemonized TweetStream client that will allow you to 
# create backgroundable scripts for application specific 
# processes. For instance, if you create a script called 
# <tt>tracker.rb</tt> and fill it with this: 
# 
#  require 'rubygems' 
#  require 'tweetstream' 
# 
#  TweetStream.configure do |config| 
#  config.consumer_key = 'abcdefghijklmnopqrstuvwxyz' 
#  config.consumer_secret = '' 
#  config.oauth_token = 'abcdefghijklmnopqrstuvwxyz' 
#  config.oauth_token_secret = '' 
#  config.auth_method = :oauth 
#  end 
# 
#  TweetStream::Daemon.new('tracker').track('intridea') do |status| 
#  # do something here 
#  end 
# 
# And then you call this from the shell: 
# 
#  ruby tracker.rb start 
# 
# A daemon process will spawn that will automatically 
# run the code in the passed block whenever a new tweet 
# matching your search term ('intridea' in this case) 
# is posted. 
# 
class TweetStream::Daemon < TweetStream::Client 
DEFAULT_NAME = 'tweetstream'.freeze 
DEFAULT_OPTIONS = {:multiple => true} 

attr_accessor :app_name, :daemon_options 

# The daemon has an optional process name for use when querying 
# running processes. You can also pass daemon options. 
def initialize(name = DEFAULT_NAME, options = DEFAULT_OPTIONS) 
@app_name = name 
@daemon_options = options 
super({}) 
end 

def start(path, query_parameters = {}, &block) #:nodoc: 
Daemons.run_proc(@app_name, @daemon_options) do 
super(path, query_parameters, &block) 
end 
end 
end 

回答

0

你做

TweetStream::Daemon.new("username","password","scrapedaemon") 

其中有三個參數,可以而且應該只有兩個,第二個選項是一個哈希:

initialize(name = DEFAULT_NAME, options = DEFAULT_OPTIONS) 

(似乎有針對不同的文檔中,一個在紅寶石文檔。 org顯示您嘗試的用法,但您使用的來源看起來更像此處所述:http://rdoc.info/github/intridea/tweetstream/TweetStream/Daemon