Skip to content

Instantly share code, notes, and snippets.

@elecnix
Created October 5, 2008 13:51
Show Gist options
  • Save elecnix/14889 to your computer and use it in GitHub Desktop.
Save elecnix/14889 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mechanize'
# Generates a CVS list: Postal Code, District ID, District Name
# Use like this:
# $~/postal_codes/A$ ruby /path/to/extract_elections_ca 2>/dev/null >> districts.csv
postal_codes = File.open("postal_codes.txt").read.split("\n")
postal_codes.each do |postcode|
postcode.sub! ' ', ''
file_name = "pages/#{postcode}"
if File.exist?(file_name)
doc = open(file_name) { |f| Hpricot(f) }
if !(doc/"#myMessages_lblMessage").empty?
$stderr.puts "#{postcode},invalid,invalid"
else
district_name = (doc/"#lblElectoralDistrict").text
links = doc.search("//td[@class='Banner']/a").select {|link| link.attributes["href"].match('ED=([0-9]+)') }
if links.empty?
$stderr.puts "#{postcode},invalid,invalid"
else
district_id = links.first.attributes["href"].match('ED=([0-9]+)')[1]
$stdout.puts "#{postcode},#{district_id},#{district_name}"
end
end
else
$stderr.puts "#{postcode},unknown,unknown"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment