Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2012 05:38
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bleything/4029741 to your computer and use it in GitHub Desktop.
pull current election results
#!/usr/bin/env ruby
require 'nokogiri'
require 'csv'
require 'open-uri'
require 'pp'
SHIT_I_CARE_ABOUT = /74|502|1185|Governor|Attorney General|Congressional District 1/
Nokogiri::HTML(open BASE_URL + "Export.html").css("a").each do |a|
if a['href'] =~ /AllState.*csv$/
@results_url = BASE_URL + a['href']
csv = open(@results_url).read.gsub(/"/, '')
results = CSV.parse(csv, :headers => true)
cleaned = {}
results.each do |res|
next unless res['Race'] =~ SHIT_I_CARE_ABOUT
race = res['Race']
candidate = res['Candidate']
percentage = res['PercentageOfTotalVotes']
cleaned[race] ||= {}
cleaned[race][candidate] = percentage
cleaned.each do |race, results|
puts race
puts "-" * 80
results.each do |name, percent|
puts "#{name}: #{percent}%"
puts # blank line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment