Tweetstream

Hi, I want to use the TweetStream gem http://github.com/intridea/tweetstream in my Rails app

I can’t seem to figure out how to do it, because it opens a connection and then won’t move on.

I got it to work in a Ruby script with

---------- start code ---------- require ‘rubygems’ require ‘tweetstream’

user = ‘blinkdev’ password = secret keyword = ‘#ROCKPIC

Thread.new do TweetStream::Client.new(user,password).track(keyword) do |status| puts “[#{status.created_at}-#{status.user.screen_name}] #{status.text}” end end

loop do sleep 10 puts “other stuff happening” end

---------- end code ----------

Which gives the output: other stuff happening other stuff happening [Mon Mar 01 20:30:59 +0000 2010-blinkdev] test #ROCKPIC 7 other stuff happening

So I tried to replicate that in my Rails app in the file config/initializers/twitter_stream.rb

---------- start code ---------- require ‘rubygems’ require ‘tweetstream’

Thread.new do logger.info “started tweetstream”

TweetStream::Client.new(‘blinkdev’,secret).track(‘#ROCKPIC’) do |status| message = “[#{status.created_at}-#{status.user.screen_name}] #{status.text}” logger.info message logger.info Rockpic.new( :message => message ).save! end

end ---------- end code ----------

But it didn’t work. I couldn’t get it to log anything either. So I went and tried it in the console:

---------- start code ---------- $script/console Loading development environment (Rails 2.3.3)

require ‘rubygems’ => require ‘tweetstream’ =>

?> Thread.new do ?> logger.info “started tweetstream”

?> TweetStream::Client.new(‘blinkdev’,secret).track(‘#ROCKPIC’) do |status| ?> message = “[#{status.created_at}-#{status.user.screen_name}] #{status.text}”

[logger.info](http://logger.info) message
[logger.info](http://logger.info) Rockpic.new( :message => message ).save!

end

?> end => #<Thread:0x24ba60c dead> ---------- end code ----------

It doesn’t seem to do anything. I tried playing with daemons, but I can’t figure out how to use them either. If someone can show me how to use daemons, I’ll give that a try (I have the daemons gem installed http://rubygems.org/gems/daemons) and it looks like TweetStream has their own version with Daemons, http://rdoc.info/rdoc/intridea/tweetstream/blob/e5ecb9ef125ebce038d24f816973bbb4644445a8/TweetStream/Daemon.html but I can’t seem to get it to do anything:

---------- start code ---------- ARGV << ‘start’ require ‘rubygems’ require ‘tweetstream’

user = ‘blinkdev’ password = secret keyword = ‘#ROCKPIC

TweetStream::Daemon.new(user,password, ‘tracker’).track(keyword) do |status| puts “[#{status.created_at}-#{status.user.screen_name}] #{status.text}” end ---------- end code ----------

It runs, but it doesn’t respond to anything, and I think I need it to run in the Rails environment, because I want to host it on Heroku. Anyway, I’m not sure where to go with it, I just want my Rails app to have a stream to twitter to watch for a given hash tag, and a particular user’s tweets.

Any help would be appreciated.

Based on their example, I got this to work: ---------- start code ---------- require ‘rubygems’ require ‘tweetstream’ require ‘ruby-growl’

username = ‘blinkdev’ password = secret tracks = [‘#ROCKPIC’] puts “Starting a GrowlTweet to track: #{tracks.inspect}”

TweetStream::Daemon.new(username,password).track(*tracks) do |status| g = Growl.new ‘localhost’, ‘growltweet’, [‘tweet’] g.notify ‘tweet’, status.user.screen_name, status.text end ---------- end code ----------

And run it from the command line like $ruby test.rb start $ruby test.rb stop

So I want to pull that into my rails app, I wrote this file to load the environment, and save the statuses ---------- start code ----------

file: RAILS_ROOT/jobs/twitter_stream.rb

ENV[‘RAILS_ENV’] = ARGV.shift require File.expand_path(File.dirname(FILE) + “/…/config/environment”)

require ‘rubygems’ require ‘tweetstream’

username = ‘blinkdev’ password = secret tracks = [‘#ROCKPIC’]

TweetStream::Daemon.new(username,password).track(*tracks) do |status| Rockpic.new( :message => “[#{status.created_at}-#{status.user.screen_name}] #{status.text}” ).save! end ---------- end code ----------

and then to load it, I created

---------- start code ----------

file: RAILS_ROOT/config/initializers/twitter_stream.rb

puts ‘pre’ system “#{%x(which ruby).chomp} #{File.join(RAILS_ROOT,‘jobs’,‘twitter_stream.rb’)} #{ENV[‘RAILS_ENV’]} restart” puts ‘post’ ---------- end code ----------

But it never finishes that command, when I start my server it just keeps writing ‘pre’ over and over, implying to me that it is recursively calling itself. ie the initializer starts the daemon, which requires the environment, which runs the initializer, which starts the daemon, etc.

I figure if I can get it to realize that it is already running, then I should be good, but I’m not very experienced with this, and don’t know how to check for that. Api doesn’t look like it offers a way, but looking at the code shows that it uses Daemons underneath of it. Not sure how to do it, still, though. Any help is appreciated.

Hi Josh,

If your overall goal is to run it as a background task on Heroku, I think you want this: http://docs.heroku.com/background-jobs.

I don't think that there's any other way to run background jobs on Heroku.

Regards Ritchie

Thanks, Ritchie, I figured the TweetStream should be fine since it runs on Daemons, which is listed in heroku’s installed gems at http://installed-gems.heroku.com

But I’ll read through that and see if a background task makes more sense, I appreciate it.