2014-02-09 42 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