public
Last active

pull current election results

  • Download Gist
results.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.