Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Created February 4, 2011 17:39
Show Gist options
  • Save donpdonp/811430 to your computer and use it in GitHub Desktop.
Save donpdonp/811430 to your computer and use it in GitHub Desktop.
quick streaming twitter keyword search
#!/usr/bin/env ruby
# filename: twitter-stream
#
#$ sudo gem install twitter-stream json
#$ ./twitter-stream superbowl
#@ZZZZThomas If you tell me your fave #SuperBowl recipe here, you could win free books
#@ZZle_Model smh all these #steelerhaters... They must not want the steelers to win a
require 'rubygems'
require 'json'
require 'twitter/json_stream'
keyword = ARGV[0]
puts "watching #{keyword}"
EventMachine::run {
stream = Twitter::JSONStream.connect(
:path => "/1/statuses/filter.json?track=#{keyword}",
:auth => 'username:password'
)
stream.each_item do |item|
jitem = JSON.parse(item)
puts "@#{jitem["user"]["screen_name"]} #{jitem["text"]}"
end
stream.on_error do |message|
# No need to worry here. It might be an issue with Twitter.
# Log message for future reference. JSONStream will try to reconnect after a timeout.
puts "error #{message}"
end
stream.on_max_reconnects do |timeout, retries|
puts "max. reconnects"
# Something is wrong on your side. Send yourself an email.
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment