Skip to content

Instantly share code, notes, and snippets.

@leereilly
Forked from tenderlove/test.rb
Created September 19, 2011 00:41
Show Gist options
  • Save leereilly/1225769 to your computer and use it in GitHub Desktop.
Save leereilly/1225769 to your computer and use it in GitHub Desktop.
require 'thread'
require 'json'
require 'net/http'
require 'uri'
require 'betabrite'
require 'psych'
require 'usb'
class StreamClient
def initialize user, pass
@user = user
@pass = pass
end
def listen &block
uri = URI.parse("https://stream.twitter.com/1/statuses/filter.json")
producer = Thread.new do
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Post.new(uri.path)
request.basic_auth @user, @pass
request.set_form_data({:track=>"gogaruco"}, "&")
http.request(request) do |response|
response.read_body do |segment|
begin
block.call JSON.load(segment)
rescue JSON::ParserError
end
end
end
end
end
end
end
queue = Queue.new
StreamClient.new(*Psych.load_file(ARGV[0])).listen { |thing|
queue << thing
}
trap(:INT) do
queue.clear
end
Thread.new {
colors = [ 'FF0000', '00FF00', '0000FF', 'FFFF00', 'FF00FF', '00FFFF',
'FFFFFF',
]
while data = queue.pop
BetaBrite::USB.new { |sign|
sign.stringfile('0') do
print string("[@#{data['user']['screen_name']}]").rgb('0000FF')
end
sign.stringfile('1') do
color = colors.sort_by { rand }.first
print string("#{data['text']}").rgb color
end
}.write!
sleep 3
end
}.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment