0

运行以下 ruby​​ 脚本时,我不断收到以下错误。如果有人能帮我解决这个问题,将不胜感激。我已经删除了所有敏感数据,例如 API 密钥。

代码:

#!/usr/bin/env ruby
require "tweetstream"
require "mongo"
require "time"

TweetStream.configure do |config|
  config.consumer_key       = 'KEY'
  config.consumer_secret    = 'SECRET'
  config.oauth_token        = 'TOKEN'
  config.oauth_token_secret = 'TOKEN_SECRET'
  config.auth_method        = :oauth
end

db = Mongo::Connection.new("ds045037.mongolab.com", 45037).db("tweets")
auth = db.authenticate("DB_USERNAME", "DB_PASSWORD")
tweets = db.collection("tweetdata")

TweetStream::Daemon.new("TWITTER_USERNAME", "TWITTER_PASSWORD").track("TERM") 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

启动脚本的命令:

ruby tweetscrape.rb

红宝石版本:

ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]

ruby -c tweetscrape.rb 产生:

Syntax OK

错误信息:

/usr/local/rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:184:in `[]=': can't convert Symbol into Integer (TypeError)
    from /usr/local/rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:184:in `run_proc'
    from /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/daemon.rb:48:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:131:in `filter'
    from /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:98:in `track'
    from tweetscrape.rb:19:in `<main>'

编辑:我现在使用以下内容没有错误,但没有任何内容输入到 mongodb:

#!/usr/bin/env ruby
require "tweetstream"
require "mongo"
require "time"

TweetStream.configure do |config|
  config.consumer_key       = 'gfdsgfdsgfdsgfdsgfdsgfds'
  config.consumer_secret    = 'gfsdgfdsgfdsgfdsgfsdgfd'
  config.oauth_token        = 'gfdgfdsgfsdgfdsgfsdgf'
  config.oauth_token_secret = 'hsgfsdgfsdgfsdgfds'
  config.auth_method        = :oauth
end

db = Mongo::Connection.new("ds045037.mongolab.com", 45037).db("tweets")
auth = db.authenticate("gfsdgfdsgfsd", "gfdsgfdsgfdsgfsd")
tweets = db.collection("tweetdata")

TweetStream::Client.new.track('TERM') do |status|
  puts status.text
  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

推文通过puts显示在屏幕上......

4

1 回答 1

0

您在使用 Daemon 类时遇到的初始错误是因为您没有将正确的参数传递给构造函数。构造函数接受一个字符串和一个散列。

从那开始,插入失败,因为:

  1. 解析 status.datetime 会引发异常(它已经是一个 Time 对象)。
  2. 如果没有坐标,status.coordinate 会抛出异常。

以下代码适用于我(注意:我添加了咆哮,以便您可以看到推文):

#!/usr/bin/env ruby
require "tweetstream"
require "mongo"
require "time"
require 'growl'

DESIRED = %w{created_at text geo coordinates id id_str}
host= ENV["MONGO_HOST"]  || 'localhost'
port = ENV["MONGO_PORT"]  || 27017
username = ENV["MONGO_USERNAME"]
password = ENV["MONGO_PASSWORD"]

term = ARGV[1] || 'TERM'

begin
  TweetStream.configure do |config|
    config.consumer_key       = ENV["TWEET_CONSUMER_KEY"]
    config.consumer_secret    = ENV["TWEET_CONSUMER_SECRET"]
    config.oauth_token        = ENV["TWEET_OAUTH_TOKEN"]
    config.oauth_token_secret = ENV["TWEET_OAUTH_TOKEN_SECRET"]
    config.auth_method        = :oauth
  end

  db = Mongo::Connection.new(host, port).db("tweets")
  db.authenticate(username, password)
  tweets = db.collection("tweetdata")

  puts "about to start tracking term #{term}"
  TweetStream::Daemon.new('tracker').track(term) do |status|
    Growl.notify status.text, :title => status.user.screen_name

    #
    # filter out nil values
    # filter out all keys not in the desired array
    #
    data = status.attrs.select{|k,v| !v.nil? && DESIRED.include?(k.to_s)}
    tweets.insert({"data" => data});
  end

rescue Mongo::ConnectionFailure
  puts "Connection Error :  #{$!}"
rescue Mongo::AuthenticationError
  puts "Auth Error :  #{$!}"
rescue Mongo::MongoDBError
  puts "Unexpected Error :  #{$!}"
end

您需要使用以下正确值设置您的环境:

export MONGO_USERNAME="..."
export MONGO_PASSWORD="..."
export TWEET_CONSUMER_KEY="..."
export TWEET_CONSUMER_SECRET="..."
export TWEET_OAUTH_TOKEN="..."
export TWEET_OAUTH_TOKEN_SECRET="..."

然后你可以用类似的东西启动守护进程(在这种情况下,我们将搜索 yankees):

ruby tweetscrape.rb start yankees
于 2013-05-20T12:51:25.070 回答