/
load.rake
397 lines (313 loc) · 11.3 KB
/
load.rake
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
task :loading_environment => :environment do
require 'csv'
require 'sunlight'
Sunlight::Base.api_key = config[:sunlight_api_key]
end
namespace :load do
desc "Loads titles from titles.csv"
task :titles => :loading_environment do
start = Time.now
debug = ENV['debug'].present?
# blow away and start from scratch
Title.delete_all
i = 0
CSV.foreach("data/csv/titles.csv") do |row|
i += 1
next if i == 1 # header row
title_name_original = row[0].strip
title_name = row[1].blank? ? title_name_original : row[1].strip
if title = Title.where(:name => title_name).first
title.original_names << title_name_original
puts "Updated title: #{title_name} with original title #{title_name_original}" if debug
else
title = Title.new :name => title_name
title.original_names = [title_name_original]
puts "New title: #{title_name} with original title #{title_name_original}" if debug
end
title.save!
end
puts "Loaded #{Title.count} titles."
puts "\nFinished in #{Time.now - start} seconds."
end
desc "Loads offices from offices.csv"
task :offices => :loading_environment do
start = Time.now
offline = ENV['offline'].present?
# don't empty out offices; we don't keep past committees anywhere, so they must be preserved
committees = offline ? {} : committee_cache
i = 0
CSV.foreach("data/csv/offices.csv") do |row|
i += 1
next if row == 1 # header row
office_from_row row, i, committees
end
unless offline
Sunlight::Legislator.all_where(:all_legislators => true).each do |legislator|
office_from_legislator legislator
end
end
puts "Loaded #{Office.count} offices."
puts "\t#{Office.members.count} members"
puts "\t#{Office.committees.count} committees"
puts "\t#{Office.others.count} other offices"
puts "\nFinished in #{Time.now - start} seconds."
end
desc "Loads staffers from staffers.csv"
task :staffers => :loading_environment do
start = Time.now
Staffer.delete_all
i = 0
CSV.foreach("data/csv/staffers.csv") do |row|
i += 1
next if i == 1 # header row
staffer_from_row row, i
end
puts "Loaded #{Staffer.count} staffers."
puts "\nFinished in #{Time.now - start} seconds."
end
desc "Loads database (from scratch) from staffers.csv and titles.csv"
task :positions => :loading_environment do
start = Time.now
debug = ENV['debug'].present?
limit = ENV['limit'].present? ? ENV['limit'].to_i : nil
Position.delete_all
i = 0
CSV.foreach("data/csv/positions.csv") do |row|
i += 1
next if i == 1 # header row
# for debugging usage
return if limit and i > limit
staffer_name_original = strip row[0]
if staffer_name_original.blank?
puts "WARNING: No staffer name given, skipping row #{i}"
next
end
title_original = strip row[1]
quarter = strip row[2]
bioguide_id = strip row[3]
office_name_original = strip row[4]
# also strip off title addendums, we're ignoring these and collapsing them programmatically
["(OTHER COMPENSATION)", "(OVERTIME)"].each do |addendum|
title_original.sub! addendum, ''
end
title_original.strip!
unless staffer = Staffer.where(:original_names => staffer_name_original).first
puts "Couldn't locate staffer by name #{staffer_name_original} in row #{i}, skipping"
next
end
unless title = Title.where(:original_names => title_original).first
puts "Couldn't locate title by name #{title_original} in row #{i}, skipping"
next
end
office = nil
if bioguide_id.present?
unless office = Office.where("member.bioguide_id" => bioguide_id).first
puts "Couldn't locate legislator by bioguide_id #{bioguide_id} in row #{i}, skipping"
next
end
else
unless office = Office.where(:original_names => office_name_original).first
puts "Couldn't locate office by name #{office_name_original} in row #{i}, skipping"
next
end
end
position = Position.where(
:quarter => quarter,
"title.name" => title['name'],
"staffer.slug" => staffer['slug'],
"office.slug" => office['slug']
).first
if position.nil?
position = Position.new(
:quarter => quarter,
:title => title.attributes,
:staffer => staffer.attributes,
:office => office.attributes,
:original_title => title_original
)
position.save!
end
puts "[#{i}][#{quarter}] #{staffer.name} works as #{title.name} for #{office.name}" if debug
end
puts "\nLoaded in #{Position.count} staffer positions."
puts "\nFinished in #{Time.now - start} seconds."
end
desc "Load in quarters from positions"
task :quarters => :loading_environment do
Quarter.delete_all
Position.all.distinct(:quarter).each do |quarter|
Quarter.create! :name => quarter
end
puts "\nLoaded in #{Quarter.count} quarters: #{Quarter.all.map(&:name).join ', '}"
end
desc "Run all loading tasks in sequence"
task :all => [:loading_environment, "load:titles", "load:offices", "load:staffers", "load:positions", "load:quarters"] do
end
end
# format name from Sunlight API
def titled_name(legislator)
"#{legislator.title}. #{legislator.nickname.present? ? legislator.nickname : legislator.firstname} #{legislator.lastname} #{legislator.name_suffix}".strip
end
# split congress_office out into room and building
def split_office(congress_office)
words = congress_office.split ' '
[words[0], words[1]]
end
def strip(str)
str.present? ? str.strip : nil
end
# office from a row in offices.csv
def office_from_row(row, i, committees)
office_name_original = strip row[0]
office_name = strip(row[1]) || office_name_original
committee_id = strip row[2]
phone = strip row[3]
building = strip row[4]
room = strip row[5]
debug = ENV['debug'].present?
if committee_id.present?
office = Office.where("committee.id" => committee_id).first
# there may be multiple spellings of a given committee that cause it to show up in duplicate rows in committees.csv
if office
if !office.original_names.include?(office_name_original)
office.original_names << office_name_original
puts "Updated committee office: #{office.name} with new original name #{office_name_original}" if debug
else
puts "Found old committee office with this name, not touching" if debug
end
# in case remote committee data changed
committee = committees[committee_id]
if committee
office.attributes = {
:name => committee.name,
:committee => {
:id => committee_id,
:name => committee.name,
:chamber => committee.chamber
}
}
office.save!
end
else
committee = committees[committee_id]
if committee
office = Office.new :name => committee.name
office.attributes = {
:original_names => [office_name_original],
:office_type => "committee",
:phone => phone,
:room => room,
:building => building,
:chamber => 'house',
:committee => {
:id => committee_id,
:name => committee.name,
:chamber => committee.chamber
}
}
else
puts "BAD OR OLD COMMITTEE_ID: #{committee_id}, row #{i}"
return
end
puts "New committee office: #{office.name} with original name #{office_name_original}" if debug
end
office.save!
else
office = Office.where(:name => office_name).first
if office
if !office.original_names.include?(office_name_original)
office.original_names << office_name_original
puts "Updated other office: #{office.name} with new original name #{office_name_original}" if debug
end
else
office = Office.new :name => office_name
office.attributes = {
:original_names => [office_name_original],
:office_type => "other",
:phone => phone,
:room => room,
:building => building,
:chamber => 'house'
}
puts "New other office: #{office.name} with original name #{office_name_original}" if debug
end
office.save!
end
end
def office_from_legislator(legislator)
phone = legislator.phone
room, building = split_office legislator.congress_office
chamber = legislator.title == 'Sen' ? 'senate' : 'house'
unless office = Office.where("member.bioguide_id" => legislator.bioguide_id).first
office = Office.new :name => titled_name(legislator)
# puts "[#{legislator.bioguide_id}] not found, making new record"
# else
# puts "[#{legislator.bioguide_id}] found, updating existing record"
end
office.attributes = {
:original_names => [],
:office_type => "member",
:phone => phone,
:room => room,
:building => building,
:chamber => chamber,
:member => {
:bioguide_id => legislator.bioguide_id,
:firstname => legislator.firstname,
:lastname => legislator.lastname,
:nickname => legislator.nickname,
:party => legislator.party,
:name_suffix => legislator.name_suffix,
:title => legislator.title,
:chamber => chamber,
:congress_office => legislator.congress_office,
:phone => legislator.phone,
:state => legislator.state,
:district => legislator.district,
:in_office => legislator.in_office
}
}
# puts "[#{legislator.bioguide_id}] New or updated member office: #{office.name}"
office.save!
end
def staffer_from_row(row, i)
debug = ENV['debug'].present?
staffer_name_original = strip row[0]
staffer_name = strip(row[1]) || staffer_name_original
if staffer_name_original.blank?
puts "WARNING: no staffer original name provided for row #{i}, skipping"
return
end
if staffer = Staffer.where(:name => staffer_name).first
staffer.original_names << staffer_name_original
puts "[#{i}] Updated staffer: #{staffer_name} with original name #{staffer_name_original}" if debug
else
# standardize fields
last_name, first_name = staffer_name.split /,\s?/
if last_name
last_name = last_name.split(/\s+/).map {|n| n.capitalize}.join " "
end
if first_name
first_name = first_name.split(/\s+/).map {|n| n.capitalize}.join " "
end
staffer = Staffer.new
staffer.attributes = {
:name => [first_name, last_name].join(" "),
:original_names => [staffer_name_original],
:first_name => first_name,
:last_name => last_name,
}
puts "[#{i}] New staffer: #{staffer_name} with original name #{staffer_name_original}" if debug
end
staffer.save!
end
def committee_cache
senate = Sunlight::Committee.all_for_chamber 'Senate'
house = Sunlight::Committee.all_for_chamber 'House'
joint = Sunlight::Committee.all_for_chamber 'Joint'
cache = {}
(senate + house + joint).each do |comm|
cache[comm.id] = comm
end
cache
end