Skip to content

Instantly share code, notes, and snippets.

@mojodna

mojodna/gist:22267

Created Nov 5, 2008
Embed
What would you like to do?
#!/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
You can’t perform that action at this time.