Skip to content

Instantly share code, notes, and snippets.

@cesare
Created September 4, 2010 15:09
Show Gist options
  • Save cesare/565254 to your computer and use it in GitHub Desktop.
Save cesare/565254 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8 -*-
require 'base64'
require 'pp'
require 'rubygems'
require 'rev'
require 'json'
require 'g'
$g_sticky = false
username = 'YOUR_TWITTER_SCREEN_NAME'
password = 'YOUR_TWITTER_PASSWORD'
class TwitterStream < Rev::HttpClient
attr_accessor :username, :password
def self.connect(username, password)
client = super("stream.twitter.com", 80)
client.username = username
client.password = password
client
end
def on_body_data(data)
@line ||= ""
lines = data.split("\r")
return if lines.size == 0
if lines.size == 1
@line += lines.shift
return
end
while lines.size > 1
@line += lines.shift
if @line.length > 2
json = JSON.parse(@line)
g("[#{json['user']['screen_name']}] #{json['text']}")
puts json
@line = ''
end
end
@line = lines.shift
@line = "" if @line =="\n"
end
def statuses(keyword)
request('GET', '/1/statuses/filter.json', :query => { :track => keyword }, :head => headers)
end
def headers
{ :authorization => auth_header }
end
def auth_header
"Basic #{Base64.encode64("#{username}:#{password}").chomp}"
end
end
l = Rev::Loop.default
c = TwitterStream.connect(username, password).attach(l)
c.statuses(ARGV[0])
l.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment