forked from openaustralia/openaustralia-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postcodes.rb
executable file
·53 lines (38 loc) · 1.51 KB
/
postcodes.rb
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
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
# Load the postcode data directly into the database
$:.unshift "#{File.dirname(__FILE__)}/lib"
# in `require': no such file to load -- mysql (LoadError)
require 'rubygems'
require 'active_support/core_ext'
require 'csv'
require 'mysql'
require 'configuration'
require 'people'
# mysql required
#require 'i18n'
conf = Configuration.new
def quote_string(s)
s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end
divisions = CSV.readlines("data/postcodes.csv")
# Remove the first two elements
divisions.shift
divisions.shift
puts "Reading members data..."
people = PeopleCSVReader.read_members
all_members = people.all_periods_in_house(House.representatives)
all_members.each do |mem|
puts mem.division
end
# First check that all the constituencies are valid
constituencies = divisions.map { |row| row[1] }.uniq
constituencies.each do |constituency|
# throw "Constituency #{constituency} not found" unless all_members.any? {|m| m.division == constituency}
puts "Constituency #{constituency} not found" unless all_members.any? {|m| m.division == constituency}
puts "***Constituency #{constituency} found" if all_members.any? {|m| m.division == constituency}
end
db = Mysql.real_connect(conf.database_host, conf.database_user, conf.database_password, conf.database_name)
# Clear out the old data
db.query("DELETE FROM postcode_lookup")
#values = divisions.map {|row| "('#{row[0]}', '#{quote_string(row[1])}')" }.join(',')
#db.query("INSERT INTO postcode_lookup (postcode, name) VALUES #{values}")