Created
February 18, 2010 18:30
-
-
Save tscho/307916 to your computer and use it in GitHub Desktop.
Parses the @WeatherCalgary twitter feed and uses the google chart API to create a png temperature graph
This file contains hidden or 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/local/bin/ruby | |
require 'rubygems' | |
require 'uri' | |
require 'net/http' | |
require 'json' #3rd party | |
require 'time' | |
def weird_parse(weird) | |
slices = weird.split /[\s]/ | |
day = slices[0] | |
hour = slices[2] | |
ind = Time::RFC2822_DAY_NAME.index day | |
ind = -1 * (7 - ind) if ind > Time.now.wday | |
puts ind | |
time = Time.new | |
time = Time.parse(hour) | |
puts (Time.now.wday - ind) * 24 * 60 * 60 | |
time = time - (Time.now.wday - ind) * 24 * 60 * 60 | |
puts "Parsed [ #{weird} ] into [ #{time.to_s} ]" | |
time | |
end | |
class DataPoint | |
include Comparable | |
attr_accessor :temp, :time, :time_txt | |
def initialize(time_txt, temp, wind, humidity) | |
@time = weird_parse time_txt | |
@time_txt = time_txt | |
@temp = temp | |
@wind = wind | |
@humidity = humidity | |
end | |
def <=>(other) | |
if @time < other.time | |
-1 | |
elsif other.time < @time | |
1 | |
else | |
0 | |
end | |
end | |
def eql?(other) | |
@time == other.time | |
end | |
def hash | |
@time.hash | |
end | |
end | |
class Numeric | |
def roundup(nearest=10) | |
self % nearest == 0 ? self : self + nearest - (self % nearest) | |
end | |
def rounddown(nearest=10) | |
self % nearest == 0 ? self : self - (self % nearest) | |
end | |
end | |
class Array | |
def every_nth(nth=2) | |
elems = [] | |
self.each_with_index do |x, i| | |
elems.push(x) if i % nth == 0 | |
end | |
return elems | |
end | |
end | |
uri = URI.parse('http://twitter.com/statuses/user_timeline/WeatherCalgary.json?count=100') | |
r = Net::HTTP.get_response(uri.host, uri.path + "?" + uri.query) | |
puts "#{r.code}\n" | |
json = JSON.parse r.body | |
puts "received #{json.length} tweets" | |
data = [] | |
json.each do |s| | |
slices = [] | |
slices.concat /\[([^\]]+)\] ([^|]+)/.match(s["text"]).captures | |
s["text"].scan /\|([^|#]+)/ do |m| | |
slices.concat m | |
end | |
time = Time.parse s["created_at"] | |
time_txt = slices[0] | |
condition = slices[1] | |
temp = slices[2].split("/")[0].to_i | |
wind = slices[3] | |
humidity = slices[4] | |
data.push DataPoint.new(time_txt, temp, wind, humidity) | |
end | |
data = data.uniq | |
puts "#{data.length} unique data points" | |
data = data.reverse | |
data = data.last 48 | |
temps = data.map{ |x| x.temp } | |
times = data.map { |x| x.time_txt.gsub(/[\s]/, '%20') } | |
last = times.last | |
times = times.every_nth 4 | |
times.push last | |
chart_req = "\ | |
/chart?\ | |
cht=lc\ | |
&chs=1000x200\ | |
&chtt=Weather\ | |
&chd=t:#{temps.join(',')}\ | |
&chxt=x,y\ | |
&chxl=0:|#{times.join('|')}\ | |
&chdl=Temperature\ | |
&chma=10,10,10,10,10,10\ | |
&chxtc=1,-1000|0,-200" | |
temps = temps.sort | |
min_temp = temps.first.rounddown 5 | |
max_temp = temps.last.roundup 5 | |
temp_spread = max_temp - min_temp | |
chart_req += "\ | |
&chxr=1,#{min_temp},#{max_temp}\ | |
&chds=#{min_temp},#{max_temp}" | |
puts chart_req | |
f = File.open "Temps.png", "wb" | |
f.write(Net::HTTP.get_response("chart.apis.google.com", chart_req).body) | |
f.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment