Write to the database using the tweetstream daemon

I am trying to write all tweets that match a keyword in my database. I installed the following in tracker.rb :

 require 'rubygems' require 'tweetstream' TweetStream::Daemon.new('Bill Gates','money','Twitter Tracker').track('ladygaga') do |status| Tweet.new(:content => status.text) end 

But nothing happens. What am I doing wrong here?

Thank you in advance

Update: I put everything in a .rake file called twitter.rake and ran the daemon using $ rake scrap :

 task :scrap => :environment do desc "Run Twitter Scraper" TweetStream::Client.new('TWITTER_USER','TWITTER_PASS').track('ladygaga') do |status| Tweet.create(:user_id => status.user.id, :user_screen_name => status.user.screen_name, :user_profile_image_url => status.user.profile_image_url, :status_text => status.text, :status_id => status.id) puts "[#{status.user.screen_name}] #{status.text}" end end 
+4
source share
3 answers

What do you call Daemon?

You need to provide the command (start / stop ..)

For instance:

 rails runner "TweetStream::Daemon.new('tracker').track('ladygaga') { |status| do_something }" start 

This will start work in the background.

+1
source

Your first approach was the best, you need to run "deamon" from the command line, but since you want custom rails and activerecord you need to load the rails environment into the script.

You need to do something like this:

 #!/usr/bin/env ruby # encoding: utf-8 ENV["RAILS_ENV"] ||= "development" root = File.expand_path(File.join(File.dirname(__FILE__), '..')) require File.join(root, "config", "environment") require 'tweetstream' p "Initializing daemon..." TweetStream.configure do |config| config.consumer_key = 'your-consumer_key' config.consumer_secret = 'your-consumer_secret' config.oauth_token = 'your-oauth_token' config.oauth_token_secret = 'your-oauth_token_secret' config.auth_method = :oauth end terms = ['ladygaga'] daemon = TweetStream::Daemon.new('tracker', :log_output => true, :backtrace => true, ) daemon.on_inited do ActiveRecord::Base.connection.reconnect! p "Listening..." end daemon.on_error do |message| puts "on_error: #{message}" end daemon.on_reconnect do |timeout, retries| puts "on_reconnect: #{timeout}, #{retries}" end daemon.on_limit do |discarded_count| puts "on_limit: #{skip_count}" end daemon.track(terms) do |status| # put here your model.create code! # Tweet.create!( :uid => status.id, ... ) end 

To run the script just type:

 ruby scrip-name.rb run 
+1
source

I guess this is part of the big rails application. If so, problem 1 is that Tweet.new will not be stored in the database if it is a standard activerecord object. Try Tweet.create Secondly, I'm not sure if the script will know about Tweet if it activates the record without delaying the rails application, possibly including the environment.rb file.

Sort of:

 ENV["RAILS_ENV"] ||= "production" require File.dirname(__FILE__) + "/../../config/application" Rails.application.require_environment! 

If this does not work, you can only try to enable active recording. Here's a question and answer that describes this:

How to use ActiveRecord in a ruby ​​script outside of Rails?

0
source

All Articles