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 |
2 comments:
or you could just do it the easy way ;P
----
require 'tweetstream'
TweetStream::Client.new('username','password').track('awesome') do |status|
puts "#{status.text}"
end
----
http://github.com/intridea/tweetstream
I could have, I saw the library of course. I wanted to write it by hand without any additional magic. It was a purely academic exercise :)
Post a Comment