Skip to content

@bleything /results.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
pull current election results
#!/usr/bin/env ruby
require 'nokogiri'
require 'csv'
require 'open-uri'
require 'pp'
BASE_URL = "http://vote.wa.gov/results/current/"
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']
end
end
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
end
cleaned.each do |race, results|
puts race
puts "-" * 80
results.each do |name, percent|
puts "#{name}: #{percent}%"
end
puts # blank line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.