You'll need the json gem installed. Once you have that you can run the script like...
./twitter_stream.rb <username> <password>
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 'net/http' | |
require 'json' | |
http = Net::HTTP.new('stream.twitter.com', 80) | |
get = Net::HTTP::Get.new("/1/statuses/filter.json?track=awesome") | |
get.basic_auth ARGV[0], ARGV[1] | |
http.request(get) do |response| | |
text = "" | |
response.read_body do |chunk| | |
chunk.each_byte do |c| | |
if c == 10 | |
if text.length > 1 | |
begin | |
json = JSON.parse(text) | |
puts "#{json['user']['screen_name']}: #{json['text']}" | |
rescue Exception => e | |
puts "Unable to parse JSON:: #{e}" | |
end | |
text = "" | |
end | |
text = "" | |
else | |
text += c.chr | |
end | |
end | |
end | |
end |