Repository navigation
Expand file tree
/
Copy pathparse-member-links.rb
More file actions
executable file
·324 lines (281 loc) · 13 KB
/
Copy pathparse-member-links.rb
File metadata and controls
executable file
·324 lines (281 loc) · 13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
require "mechanize"
require "open-uri"
require "nokogiri"
require "json"
require "optparse"
require "fileutils"
require_relative "lib/aph_mechanize_agent"
require_relative "lib/name"
require_relative "lib/people"
require_relative "lib/configuration"
class ParseMemberLinks
def initialize(args)
@args = args
@options = { load_database: true, limit: nil, output_dir: nil }
OptionParser.new do |opts|
opts.banner = "Usage: parse-member-links.rb [--no-load] [--limit=N] [--output-dir=PATH]"
opts.on("--no-load", "Skip calling mpinfoin.pl at the end") do
@options[:load_database] = false
end
opts.on("--limit=N", Integer, "Cap the number of morph.io entries processed") do |n|
@options[:limit] = n
end
opts.on("--output-dir=PATH", "Write XML output to PATH instead of conf.members_xml_path") do |path|
@options[:output_dir] = path
end
end.parse!(@args)
end
def run
conf = Configuration.new
output_dir = @options[:output_dir] || conf.members_xml_path
FileUtils.mkdir_p output_dir
# Not using caching proxy since we will be running this script once a day and we
# always want to get the new data
agent = Mechanize.new
aph_agent = AphMechanizeAgent.new_agent
puts "Reading member data..."
people = PeopleCSVReader.read_members
puts "Web pages, social media URLs and email from APH (via Morph)..."
puts "WARNING: morph_api_key is not set in configuration.yml! The api call will fail!" if conf.morph_api_key.nil? || conf.morph_api_key =~ /\AX*\z/
puts "Writing to #{output_dir}/websites.xml ..."
xml = File.open("#{output_dir}/websites.xml", "w")
x = Builder::XmlMarkup.new(target: xml, indent: 1)
x.instruct!
x.peopleinfo do
morph_result = agent.get(
"https://api.morph.io/openaustralia/aus_mp_contact_details/data.json", { query: 'select * from "data"' }, nil, "x-api-key" => conf.morph_api_key
).body
morph_entries = JSON.parse(morph_result)
morph_entries = morph_entries.first(@options[:limit]) if @options[:limit]
morph_entries.each do |person|
if person["aph_id"].nil?
warn "Skipping #{person['contact_page']}: no aph_id in morph.io data"
next
end
p = people.find_person_by_aph_id(person["aph_id"].upcase)
if p.nil?
warn "Skipping #{person['contact_page']}: no person with aph_id #{person['aph_id']} in data/people.csv"
next
end
params = { id: p.id, mp_contact_form: person["contact_page"],
aph_url: person["profile_page"] }
params[:mp_email] = person["email"] if person["email"]
params[:mp_website] = person["website"] if person["website"]
params[:mp_twitter_url] = person["twitter"] if person["twitter"]
params[:mp_facebook_url] = person["facebook"] if person["facebook"]
x.personinfo(params)
end
end
xml.close
abc_root = "https://www.abc.net.au"
puts "Writing to #{output_dir}/links-abc-election.xml ..."
xml = File.open("#{output_dir}/links-abc-election.xml", "w")
x = Builder::XmlMarkup.new(target: xml, indent: 1)
x.instruct!
x.consinfos do
puts "Election results 2007 (from the abc.net.au) ..."
# Representatives
url = "#{conf.election_web_root}/results/electorateindex.htm"
doc = Nokogiri::HTML(URI.parse(url).open)
doc.css("td.electorate").each do |td|
href = td.at("a")["href"]
href = "#{abc_root}#{href}"
name = td.at("a").inner_text
name = name.gsub("*", "").strip
x.consinfo("canonical" => name, "abc_election_results_2007" => href)
end
# Senate
url = "#{conf.election_web_root}/results/senate/"
doc = Nokogiri::HTML(URI.parse(url).open)
doc.css("a").each do |a|
next unless %r{results/senate/(\w+)\.htm}.match(a["href"])
href = abc_root + a["href"]
name = a.inner_text
x.consinfo("canonical" => name, "abc_election_results_2007" => href)
end
puts "Election results 2010 (from the abc.net.au) ..."
# Representatives
abc_2010_root = "https://www.abc.net.au/elections/federal/2010/guide"
url = "#{abc_2010_root}/electorateresults.htm"
doc = Nokogiri::HTML(URI.parse(url).open)
doc.css("td.electorate").each do |td|
href = td.at("a")["href"]
href = "#{abc_2010_root}/#{href}"
name = td.at("a").inner_text
name = name.gsub("*", "").strip
x.consinfo("canonical" => name, "abc_election_results_2010" => href)
end
# Senate
[%w[nsw NSW], %w[vic Victoria], %w[qld Queensland], %w[wa WA], %w[sa SA], %w[tas Tasmania], %w[act ACT],
%w[nt NT]].each do |name, canonical|
href = "http://www.abc.net.au/elections/federal/2010/guide/s#{name}-results.htm"
x.consinfo("canonical" => canonical, "abc_election_results_2010" => href)
end
puts "Election results 2013 (from the abc.net.au)..."
# Representatives
abc_root = "https://www.abc.net.au"
url = "#{abc_root}/news/elections/federal/2013/guide/electorates"
doc = Nokogiri::HTML(URI.parse(url).open)
doc.css("span.electorate").each do |span|
href = span.parent["href"]
href = "#{abc_root}#{href}"
name = span.inner_text
x.consinfo("canonical" => name, "abc_election_results_2013" => href)
end
# Senate
[%w[nsw NSW], %w[vic Victoria], %w[qld Queensland], %w[wa WA], %w[sa SA], %w[tas Tasmania], %w[act ACT],
%w[nt NT]].each do |name, canonical|
href = "https://www.abc.net.au/news/federal-election-2013/results/senate/#{name}/"
x.consinfo("canonical" => canonical, "abc_election_results_2013" => href)
end
puts "Election results 2016 (from the abc.net.au)..."
# Representatives
abc_root = "https://www.abc.net.au"
url = "#{abc_root}/news/elections/federal/2016/guide/electorates"
doc = agent.get(url)
doc.search(".ert-results a").each do |a|
href = doc.uri + a["href"]
name = a.at("h2").inner_text.gsub(/[^a-z]/i, "")
x.consinfo("canonical" => name, "abc_election_results_2016" => href)
end
# Senate
[%w[nsw NSW], %w[vic Victoria], %w[qld Queensland], %w[wa WA], %w[sa SA], %w[tas Tasmania], %w[act ACT],
%w[nt NT]].each do |name, canonical|
href = "https://www.abc.net.au/news/federal-election-2016/results/senate/#s#{name}"
x.consinfo("canonical" => canonical, "abc_election_results_2016" => href)
end
puts "Election results 2019 (from the abc.net.au)..."
# Representatives
abc_root = "https://www.abc.net.au"
url = "#{abc_root}/news/elections/federal/2019/results/list"
doc = agent.get(url)
# What an absolutely ridiculous selector
doc.search("article div div div div div div div h2").each do |h2|
name = h2.inner_text.strip
href = "https://www.abc.net.au/news/elections/federal/2019/guide/#{name[0..3].downcase}"
x.consinfo("canonical" => name, "abc_election_results_2019" => href)
end
# Senate
[%w[nsw NSW], %w[vic Victoria], %w[qld Queensland], %w[wa WA], %w[sa SA], %w[tas Tasmania], %w[act ACT],
%w[nt NT]].each do |_name, canonical|
# No seperate url for each state results... So...
href = "https://www.abc.net.au/news/elections/federal/2019/results/senate"
x.consinfo("canonical" => canonical, "abc_election_results_2019" => href)
end
puts "Election results 2022 (from the abc.net.au)..."
abc_root = "https://www.abc.net.au"
url = "#{abc_root}/news/elections/federal/2022/guide/electorates"
doc = agent.get(url)
doc.search("td.electorate a").each do |a|
href = doc.uri + a["href"]
name = abc_electorate_name(a)
x.consinfo("canonical" => name, "abc_election_results_2022" => href)
end
%w[NSW Victoria Queensland WA SA Tasmania ACT NT].each do |canonical|
# No seperate url for each state results... So...
href = "https://www.abc.net.au/news/elections/federal/2022/results/senate"
x.consinfo("canonical" => canonical, "abc_election_results_2022" => href)
end
puts "Election results 2025 (from the abc.net.au)..."
abc_root = "https://www.abc.net.au"
url = "#{abc_root}/news/elections/federal/2025/guide/electorates"
doc = agent.get(url)
doc.search("td.electorate a").each do |a|
href = doc.uri + a["href"]
name = abc_electorate_name(a)
x.consinfo("canonical" => name, "abc_election_results_2025" => href)
end
%w[NSW Victoria Queensland WA SA Tasmania ACT NT].each do |canonical|
# No seperate url for each state results... So...
href = "https://www.abc.net.au/news/elections/federal/2025/results/senate"
x.consinfo("canonical" => canonical, "abc_election_results_2025" => href)
end
end
xml.close
puts "Parsing Register of interests from APH..."
page = aph_agent.get("https://www.aph.gov.au/Senators_and_Members/Members/Register")
representatives_data = []
page.search("table.documents").each do |table|
table.search("tbody tr").each do |tr|
name = tr.search("td")[1].inner_text.split(",")[0..1].join(",")
url = page.uri + tr.at("td.format a")["href"]
parsed = Name.last_title_first(name)
representative = people.find_person_by_name_current_on_date(parsed, Date.today)
if representative.nil?
fatal = true
matched_by_name = people.find_people_by_name(parsed)
suggestion = if matched_by_name.empty?
"Unable to find by name, Try appending alt name: \"#{parsed.title} #{parsed.first} #{parsed.last}\" to their data/people.csv entry"
elsif matched_by_name.size == 1
last_period = matched_by_name.first.periods.last
sub_suggestion =
if last_period.nil?
"no periods at all!"
elsif last_period.to_date > Date.today ||
last_period.to_date < Date.today - (3 * 365)
"BAD DATA: last period #{last_period.from_date} #{last_period.to_why} #{last_period.to_date}"
else
fatal = false
"not current, last period #{last_period.from_date} #{last_period.to_why} #{last_period.to_date}"
end
"Found one person by name, but #{sub_suggestion} - see data/*.csv"
else
"Found too many people with that name that are current today"
end
if fatal
raise "Couldn't find #{name} (#{suggestion})"
else
warn "WARNING: Couldn't find #{name} (#{suggestion})"
next
end
end
representatives_data << { id: representative.id, aph_interests_url: url }
end
end
senate_data = []
# they changed this to JSON
response_body = Net::HTTP.get(URI.parse("https://pbs-apim-aqcdgxhvaug7f8em.z01.azurefd.net/api/queryStatements?pageSize=1000"))
data = JSON.parse(response_body)
data["statementOfRegisterableInterests"].each do |item|
name = item["name"].strip
url = "https://www.aph.gov.au/Parliamentary_Business/Committees/Senate/Senators_Interests/Senators_Interests_Register/298839#{item['cdapId']}"
last_updated = Date.parse(item["lastDateUpdated"])
senator = people.find_person_by_name(Name.last_title_first(name))
raise if senator.nil?
senate_data << { id: senator.id, aph_interests_url: url,
aph_interests_last_updated: last_updated }
end
puts "Writing to #{output_dir}/links-register-of-interests.xml ..."
xml = File.open("#{output_dir}/links-register-of-interests.xml", "w")
x = Builder::XmlMarkup.new(target: xml, indent: 1)
x.instruct!
x.peopleinfo do
representatives_data.each do |representative|
x.personinfo(id: representative[:id],
aph_interests_url: representative[:aph_interests_url])
end
senate_data.each do |senator|
x.personinfo(id: senator[:id],
aph_interests_url: senator[:aph_interests_url],
aph_interests_last_updated: senator[:aph_interests_last_updated])
end
end
xml.close
if @options[:load_database]
system("#{conf.web_root}/twfy/scripts/mpinfoin.pl links")
else
puts "No-load option has disabled the following that is normally run:"
puts " #{conf.web_root}/twfy/scripts/mpinfoin.pl links"
end
end
private
# ABC appends " (*)" to a few electorate names, which no constituency name has.
def abc_electorate_name(link)
link.inner_text.sub(/\s*\(\*\)\s*\z/, "").strip
end
end
Configuration.report_errors { ParseMemberLinks.new(ARGV).run } if File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)