Created
November 5, 2008 00:16
-
-
Save mojodna/22267 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
#!/usr/bin/env ruby | |
require 'rexml/document' | |
require 'open-uri' | |
def load_results(url = "http://d.yimg.com/b/data/us/news/xml/elections/2008a/pres.xml") | |
doc = REXML::Document.new(open(url)) | |
nodes = doc.elements.collect("//state") { |node| [node.attributes["name"].downcase, node.elements.collect("cand") { |cand| [cand.attributes["name"].downcase, cand.attributes["PopPct"].to_f, cand.attributes["PopVote"].to_i] }] } | |
votes = {} | |
nodes.each do |state, results| | |
votes[state] = {} | |
results.each do |cand, pct, vote| | |
votes[state][cand] = [pct, vote] | |
end | |
end | |
votes | |
end | |
old_votes = load_results("pres.xml") | |
while(true) do | |
# load the results feed | |
votes = load_results | |
# loop through and compare it to the old results | |
votes.each do |state, cand| | |
cand.each do |c, val| | |
# puts "c: #{c}" | |
# puts "val: #{val}" | |
# puts "old c: #{old_votes[state].inspect}" | |
# puts "old c: #{old_votes[state][c].inspect}" | |
unless old_votes[state][c] == val | |
puts "Results changed: #{state} - #{c}: #{old_votes[state][c].inspect} => #{val.inspect}" | |
end | |
end | |
end | |
# set this as old_votes | |
old_votes = votes | |
# wait 5 minutes | |
puts "-----" | |
sleep 300 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment