Created
February 9, 2011 02:52
-
-
Save seejohnrun/817796 to your computer and use it in GitHub Desktop.
Twitter Streaming Search Growl Notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'twitterstream' | |
module MassStream | |
CREDS = [ | |
{:username => 'xxx', :password => 'xxx'}, | |
] | |
def self.stream(action, items, batch_size, &block) | |
# break all of the tracks into groups (really pretty) | |
groups = []; grp = nil | |
items.each_with_index do |item, idx| | |
groups << (grp = []) if idx % batch_size == 0 | |
grp << item | |
end | |
# track each group all at once, but subscribe them all to the same listener | |
groups.map do |group_items| | |
Thread.start do | |
creds = CREDS.pop | |
puts "starting a group of #{group_items.size} (:#{action}) using user: #{creds[:username]}" | |
TwitterStream.new(:username => creds[:username], :password => creds[:password]).send(action, group_items, &block) | |
end | |
end | |
end | |
def self.track(*items, &block) | |
stream(:track, items, 5000) do |status| | |
mitems = items.select do |i| | |
text = status['text'].downcase | |
i.split(/\s/).all? { |w| text.include?(w.downcase.strip) } | |
end | |
block.call(mitems, status) | |
end | |
end | |
def self.follow(*items, &block) | |
stream(:follow, items, 400) do |status| | |
block.call(status['user']['id'], status['text']) | |
end | |
end | |
end | |
raise "Please enter something to track!" if ARGV.empty? | |
Kernel.fork do | |
threads = MassStream.track(*ARGV) do |what, status| | |
# what << "\e[31mNO MATCH\e[0m" if what.empty? | |
unless what.empty? | |
title = what.join(', ') | |
text = "@#{status['user']['screen_name']} -> " + status['text'] | |
`growlnotify -t \"#{title}\" -m \"#{text.gsub('"', '\"')}\"` | |
end | |
end | |
threads.each { |t| t.join } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment