Skip to content

Instantly share code, notes, and snippets.

@spllr
Created October 1, 2010 12:59
Show Gist options
  • Save spllr/606175 to your computer and use it in GitHub Desktop.
Save spllr/606175 to your computer and use it in GitHub Desktop.
Little twitter monitor
#!/usr/bin/env ruby
# Usage:
# > twitter_monitor [search terms]
#
# By spllr
require "rubygems"
require "twitter"
require "growl"
trap("INT") {
puts
puts "Ending twitter monitor"
exit
}
if ARGV.empty?
puts "\nUsage: twitter_monitor [SEARCH_TERMS]"
exit
end
class Msg
attr_reader :data
def initialize(data)
@data = data
end
def msg_id
data['id'].to_i
end
def to_s
"* #{data['from_user']}:\n#{data['text']}"
end
end
class Stream
include Growl
def initialize(search, opts={})
@search = search
@last_tweet_id = opts[:last_tweet_id] || 0
@interval = opts[:interval] || 30
end
def run
Thread.new do
loop do
puts "search for: #{@search}"
results = Twitter::Search.new(@search).since(@last_tweet_id).each { |r| Msg.new(r) }
puts "got #{results.size} results for #{@search}"
unless results.empty?
@last_tweet_id = results.map(&:msg_id).max
results.each { |msg| notify msg, :title => @search }
end
end
sleep(@interval)
end
end
end
end
streams = ARGV.map do |query|
puts "Start monitor: ##{query}"
Stream.new('#' << query, :interval => 10).run
end
at_exit {
puts "Ending #{streams.size} streams"
streams.each(&:exit)
}
streams.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment