Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created November 3, 2010 17:23
Show Gist options
  • Save rbranson/661391 to your computer and use it in GitHub Desktop.
Save rbranson/661391 to your computer and use it in GitHub Desktop.
Reads ABC's XML files for 2010 election totals and dumps them out.
#!/usr/bin/env ruby
#
# Read's ABC's XML files for 2010 election totals and dumps them out.
#
# http://a.abcnews.com/xml/elections/2010/dynamic/dynamicgeneral/prod/elections/national/HouseMap.xml
# http://a.abcnews.com/xml/elections/2010/dynamic/dynamicgeneral/prod/elections/national/SenateMap.xml
#
# Copyright (c) 2010 Rick Branson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
require "rubygems"
require "nokogiri"
parties = {}
xml = Nokogiri::XML(File.read(ARGV[0]))
xml.xpath("//State").each do |state|
puts "=== #{state['name']} (#{state.xpath('PrecRepPct').text}) ==="
state.xpath("*/Candidates/Cand | Candidates/Cand").each do |cand|
candidate = {
:name => cand["name"].to_s,
:party => cand["party"].to_s,
:tab => cand["tab"].to_s
}
puts " #{candidate[:name]} (#{candidate[:party]}): #{candidate[:tab]}"
parties[candidate[:party]] ||= 0
parties[candidate[:party]] += candidate[:tab].gsub(/[^\d]+/, "").to_i
end
end
parties.sort_by { |p, t| t }.reverse.each do |party, tab|
puts "Party: #{party}, Total: #{tab}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment