public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
require 'rubygems'
require 'tweetstream'
require 'pp'
require 'json'
 
a = {:name => 'Olly', :total => 0, :previous => 0, :speed => 0}
b = {:name => 'Joe', :total => 0, :previous => 0, :speed => 0}
 
start_time = Time.now.to_i
end_time = Time.now.to_i
TweetStream::Client.new(ENV['TW_USER'],ENV['TW_PASS']).track(a[:name], b[:name]) do |status|
current_time = Time.now.to_i
if current_time > start_time + 1
end_time = start_time
start_time = current_time
a[:speed] = a[:total] - a[:previous]
b[:speed] = b[:total] - b[:previous]
end_time = start_time
start_time = current_time
b[:previous] = b[:total]
a[:previous] = a[:total]
end
if (status[:text].match(/#{a[:name]}/i) || status[:text].match(/#{b[:name]}/i) ) && status[:text].match(/win/i)
a[:total] = a[:total] + 1 if status[:text].match(/#{a[:name]}/i)
b[:total] = b[:total] + 1 if status[:text].match(/#{b[:name]}/i)
a[:ratio] = a[:total].to_f / (a[:total] + b[:total]) * 100
b[:ratio] = b[:total].to_f / (a[:total] + b[:total]) * 100
result = JSON.generate([{:text => status[:text], :a => a, :b => b, :created_at => status[:created_at], :id => status[:id], :current_time => current_time }])
p result
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.