Ruby Tweetstream MongoDB Error -
i keep getting following error when running following ruby script. if can me fix appreciated. i've removed sensitive data such api keys.
code:
#!/usr/bin/env ruby require "tweetstream" require "mongo" require "time" tweetstream.configure |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") |status| # 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
command start script:
ruby tweetscrape.rb
ruby version:
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]
ruby -c tweetscrape.rb produces:
syntax ok
error message:
/usr/local/rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:184:in `[]=': can't convert symbol integer (typeerror) /usr/local/rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:184:in `run_proc' /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/daemon.rb:48:in `start' /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:131:in `filter' /usr/local/rvm/gems/ruby-1.9.3-p429/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:98:in `track' tweetscrape.rb:19:in `<main>'
edit: have no errors using below nothing entered in mongodb:
#!/usr/bin/env ruby require "tweetstream" require "mongo" require "time" tweetstream.configure |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') |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
tweets show on screen through puts though...
the initial error getting daemon class because you're not passing correct parameters constructor. contructor takes string , hash.
moving on , insert failed because:
- parsing status.datetime throws exception (its time object).
- status.coordinate throws exception if there's no coordinate.
the following code works me (note : added growl can see tweets):
#!/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 |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 start tracking term #{term}" tweetstream::daemon.new('tracker').track(term) |status| growl.notify status.text, :title => status.user.screen_name # # filter out nil values # filter out keys not in 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
you'll need setup environment following correct values :
export mongo_username="..." export mongo_password="..." export tweet_consumer_key="..." export tweet_consumer_secret="..." export tweet_oauth_token="..." export tweet_oauth_token_secret="..."
then can start daemon (in case we'll search yankees):
ruby tweetscrape.rb start yankees
Comments
Post a Comment