3

I am trying to write all tweets that matches a keyword to my database. I have set up 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?

Thanks in advance

Update: I put everything in a .rake file called twitter.rake and start the demon with $ 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
Lukas Hoffmann
  • 607
  • 1
  • 8
  • 21
  • 2
    I've got exactly the same problem with Mongoid. When I use ::Client, everything works just fine. It's only when I try daemonize it I've got neither error not response. Did you solve the problem? – Mike Bevz Jun 23 '11 at 10:23
  • 1
    Hey Mike, I put everything in a `.rake` file (see update) and so you don't have to worry about loading the rails environment. – Lukas Hoffmann Jul 05 '11 at 12:02

3 Answers3

1

How are you calling the Daemon?

You need to supply a command (start/stop..)

For example:

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

This would start the job in the background

Samer Buna
  • 8,821
  • 9
  • 38
  • 55
1

Your first approach was the best one, you need to run "deamon" from the command line, but since you want to user rails and the activerecord you need to bootstrap the rails environment in to 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
lmmendes
  • 1,480
  • 15
  • 14
0

I'm assuming this is part of a larger rails application. If so, issue 1 is that Tweet.new will not persist anything to the database if it is a standard activerecord object. Try Tweet.create Secondly I'm not sure if the script will necessarily know about the Tweet if its an activerecord without also pulling in the rails app, possibly by including the environment.rb file.

Something like:

ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!

If that doesn't work you could try just including active record theres a question and answer here that describes it:

How to use ActiveRecord in a ruby script outside Rails?

Community
  • 1
  • 1
Kelend
  • 1,417
  • 2
  • 11
  • 18
  • I tried `ENV["RAILS_ENV"] ||= "development"` and `require ::File.expand_path('../config/environment', __FILE__)` for loading the environment.rb, but it doesn't seem to work. Is this the right way to load den rails environment? – Lukas Hoffmann May 31 '11 at 09:23
  • Couple of ways to do it, I don't have much experience with EventMachine or Tweetstream, but using the daemon gem I normally use you can include the environment with: ENV["RAILS_ENV"] ||= "production" require File.dirname(__FILE__) + "/../../config/application" Rails.application.require_environment! – Kelend Jun 01 '11 at 02:47