Skip to content

Instantly share code, notes, and snippets.

@mikiobraun
Created June 14, 2010 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikiobraun/437710 to your computer and use it in GitHub Desktop.
Save mikiobraun/437710 to your computer and use it in GitHub Desktop.
require 'time'
require 'rubygems'
require 'json'
def usage
puts <<EOS
Usage:
ruby track_stream.rb <username> <password> <keyword1> <keyword2> ...
EOS
end
if ARGV.empty?
usage
exit
end
user = ARGV.shift
pass = ARGV.shift
keywords = ARGV
open("|curl -s http://stream.twitter.com/1/statuses/filter.json?track=#{keywords.join(',')} -u#{user}:#{pass}").each_line do |line|
begin
json = JSON.parse(line)
created_at = Time.parse(json['created_at'])
name = json['user']['screen_name']
text = json['text'].gsub(/\n/, "\n" + ' ' * 32)
printf "%s %-20s %s\n", created_at.strftime('%H:%M:%S'), name, text
rescue JSON::ParserError
puts "Parse error for line #{line}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment