#!/usr/bin/ruby require 'date' require 'rexml/document' require 'bushrating' require 'SVG/Graph/TimeSeries' include REXML class BushOut def initialize stream STDERR.puts "Parsing document" t = Time.now doc = Document.new(stream) t = Time.now - t STDERR.puts "Parsed #{stream.length/1000}kb stream in #{t} seconds. Checking polls." @polls = XPath.match( doc, '/popularity/poll' ) end def dateToFraction( date ) y,d = date.ord y + (d.to_f/365) end def getJD( date ) date.gsub!(/\*/, "") case date when %r{^(\d+)/(\d+)-(\d+)/(\d+)$} y = $4.to_i y += 2000 if y<2000 return Date.new( y, $1.to_i, ($2.to_i+$3.to_i)/2 ) when %r{^(\d+)/(\d+)-(\d+)/(\d+)/(\d+)$} y = $5.to_i y += 2000 if y<2000 d1 = Date.new( y, $1.to_i, $2.to_i ) d2 = Date.new( y, $3.to_i, $4.to_i ) return (d1 + ((d2-d1)/2)) when %r{^(\d+)/(\d+)/(\d+)-(\d+)/(\d+)/(\d+)$} y1 = $3.to_i y1 += 2000 if y1<2000 y2 = $6.to_i y2 += 2000 if y2<2000 d1 = Date.new( y1, $1.to_i, $2.to_i ) d2 = Date.new( y2, $4.to_i, $5.to_i ) return (d1 + ((d2-d1)/2)) when %r{^(\d+)/(\d+)/(\d+)$} y = $3.to_i y += 2000 if y<2000 return Date.new( y, $1.to_i, $2.to_i ) when %r{^(\d+)/(\d+)$} y = $2.to_i y += 2000 if y<2000 return Date.new( y, $1.to_i, 15 ) end end def process STDERR.puts "Processing..." curdate = Time.now.strftime("+%Y/%j") graph = SVG::Graph::TimeSeries.new( :width => 640, :height => 640, :graph_title => "BushMeter, #{curdate}", :show_graph_title => true, :no_css => true, :key => true, :key_position => :bottom, :scale_x_integers => true, :scale_y_integers => true, :scale_y_divisions => 10, :show_data_labels => false, :show_x_guidelines => false, :show_x_title => true, :x_title => "Time", :show_y_title => true, :y_title => "%", :y_title_text_direction => :bt, #:stagger_x_labels => true, :x_label_format => "%m/%Y", :timescale_divisions => "6 months", :add_popups => false, :popup_format => "%m/%d/%y", :show_data_points => false, :show_data_values => false, #:area_fill => true, :min_y_value => 0 ) @polls.each do |poll| source = poll.elements["source"].text source = source.length > 80 ? source[0,80]+"..." : source data = [] XPath.match(poll, 'instance').each do |instance| date = instance.elements['date'].text approval = instance.elements['approve'].text if approval.to_i > 0 d = getJD( date ) if d data << d.to_s data << approval.to_i end end end STDERR.puts "Got #{data.length} data points" graph.add_data( :data => data, :title => source ) end STDERR.puts "Burning..." rv = graph.burn graph.width=300 graph.height=200 graph.key=false graph.show_x_labels=false graph.show_x_title=false graph.show_y_labels=false graph.show_y_title=false graph.show_graph_title = false File.open("thumb.svg", "w") {|f| f.puts graph.burn} return rv end end if $0 == __FILE__ unless false and File.exists? "temp.txt" bs = BushSucker.new( true ) data = bs.get_data File.open( "temp.txt", "w" ) {|fout| fout.print data} else data = IO.read( "temp.txt" ) end bo = BushOut.new( data ) puts bo.process end