Created
June 4, 2011 19:12
-
-
Save metade/1008222 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require "bundler/setup" | |
require 'ruby-processing' | |
require 'logger' | |
require 'tweetstream' | |
require 'yaml' | |
require 'ostruct' | |
class Term < OpenStruct | |
def matches(text) | |
return false if text.nil? | |
result = false | |
search_string = text.downcase | |
search.each { |s| result = true if search_string =~ /#{s}/ } | |
result | |
end | |
end | |
$terms = [ | |
Term.new( | |
:search => ['red'], | |
:colour => [0.4, 0.039, 0.08], | |
:tweets => 0), | |
Term.new( | |
:search => ['white'], | |
:colour => [1.0, 1.0, 1.0], | |
:tweets => 0), | |
Term.new( | |
:search => ['rose'], | |
:colour => [0.945, 0.369, 0.157], | |
:tweets => 0) | |
] | |
Processing::App::SKETCH_PATH = File.dirname(__FILE__) | |
class MySketch < Processing::App | |
def setup | |
frame_rate 1 | |
color_mode RGB, 1.0 | |
font = loadFont("Eureka-90.vlw"); | |
textFont(font, 24); | |
textAlign(CENTER) | |
@padding = 10 | |
@padding2 = @padding * 2 | |
@term_width = width / $terms.size | |
@bar_height = @height-24-@padding2 | |
end | |
def draw | |
background(0.8) | |
max = round_up($terms.map { |t| t.tweets }.max) | |
$terms.each_with_index do |term, i| | |
h = (term.tweets / max.to_f) * @bar_height | |
x = (i * @term_width) + @padding | |
y = @padding + (@bar_height - h) | |
fill(*term.colour) | |
rect(x, y, @term_width-@padding2, h) | |
text(term.search.join, (i * @term_width) + (@term_width/2), height-@padding); | |
end | |
end | |
protected | |
def round_up(number) | |
return 0 if number == 0 | |
divisor = 10**Math.log10(number).floor | |
i = number / divisor | |
remainder = number % divisor | |
if remainder == 0 | |
i * divisor | |
else | |
(i + 1) * divisor | |
end | |
end | |
end | |
MySketch.new :title => "Most Tweeted Wine", :width => 286, :height => 286 | |
if __FILE__ == $0 | |
Thread.abort_on_exception = true | |
mutex = Mutex.new | |
logger = Logger.new("log/dubstep-#{Time.now.strftime('%Y%m%d-%H%M%S')}.log") | |
hashtag = 'wine' | |
config = YAML::load(File.open('config/twitter.yml')) | |
TweetStream::Client.new(config['username'],config['password']).track(hashtag) do |status| | |
p status.text | |
logger.info(status.inspect) | |
$terms.each do |term| | |
if term.matches(status.text) | |
mutex.synchronize do | |
term.tweets += 1 | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment