-
Notifications
You must be signed in to change notification settings - Fork 0
/
apfeed.py
398 lines (304 loc) · 11.7 KB
/
apfeed.py
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
398
import boto3
import csv
import hashlib
import os
from bs4 import BeautifulSoup
from datetime import datetime
from dateutil import parser as parsedate
from pytz import timezone
from time import mktime
from random import randint
# create and load a fips_lookup dict
fips_lookup = {}
with open('counties_FIPS.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
fips_lookup[row['County_Name']] = row['FIPS']
class ElectionResults(object):
_soup = None
_last_updated = None
_md5hash = None
_other_hash = None
_races = None
def __init__(self, xml):
self.xml = xml
@property
def file_name(self):
return 'apfeed{dt:%Y-%m-%d_%H-%M-%S}.xml'.format(dt=self.last_updated)
@property
def soup(self):
if not self._soup:
self._soup = BeautifulSoup(self.xml, 'xml')
return self._soup
@property
def last_updated(self):
if not self._last_updated:
central = timezone('US/Central')
self._last_updated = parsedate.parse(
self.soup.find('ElectionResults')['LastUpdated']
)
self._last_updated = central.localize(self._last_updated)
return self._last_updated
@property
def md5hash(self):
if not self._md5hash:
self._md5hash = hashlib.md5()
self._md5hash.update(str(self.soup.find_all('ElectionInfo')))
return self._md5hash.digest()
@property
def other_hash(self):
if not self._other_hash:
self._other_hash = hash(str(self.soup.find('ElectionInfo')))
return self._other_hash
@property
def races(self):
if not self._races:
self.parse_races()
return self._races
def parse_races(self):
self._races = []
for election in self.soup.find_all('ElectionInfo'):
for race_type in election.find_all('TypeRace'):
type_tag = race_type.find('Type')
type_name = type_tag.text.strip().replace(' ', '_').lower()
for race in race_type.find_all('Race'):
if type_name == 'ballot_issues':
new_results = BallotIssueResults(race, type_name)
elif type_name in [
'state_senate', 'state_house', 'us_representative'
]:
new_results = LegislativeRaceResults(race, type_name)
else:
new_results = CandidateRaceResults(race, type_name)
new_results.calculate_totals()
self._races.append(new_results)
return self._races
def cache_xml(self):
os.path.exists('.cache/') or os.makedirs('.cache/')
with open(os.path.join('.cache', self.file_name), 'wb') as f:
f.write(self.xml)
def save_to_dynamodb(self):
# create a boto3 session (should load your stored credentials from env)
session = boto3.Session()
# create a client for interacting with dynamodb
dynamodb = session.resource('dynamodb')
# get the election_results dynamodb table
table = dynamodb.Table(os.environ['DYNAMO_DB_RESULTS_TABLE'])
items_to_save = {}
for race in self.races:
try:
items_to_save[race.type]['races'].append(race.data_dict)
except KeyError:
items_to_save[race.type] = {
'last_updated': int(mktime(self.last_updated.timetuple())),
'races': [race.data_dict],
}
for race_type, data in items_to_save.iteritems():
data['race_type'] = race_type
table.put_item(Item=data)
def upload_xml_to_s3(self):
# create a boto3 session (should load your stored credentials from env)
session = boto3.Session()
# create a client for interacting with s3
s3 = session.client('s3')
cached_file_name = os.path.join('.cache/', self.file_name)
if not os.path.exists(cached_file_name):
self.cache_xml
# upload the xml results to s3
s3.upload_file(
cached_file_name,
os.environ['S3_BUCKET_NAME'],
self.file_name,
)
class RaceResults(object):
_counties = None
_reporting_precincts = None
_total_precincts = None
def __init__(
self,
soup,
type_name,
fake=False,
):
self.soup = soup
self.type = type_name
self.fake = fake
self.title = self.soup.find('RaceTitle').text.strip()
@property
def data_dict(self):
if not self._counties:
self.parse_counties()
data_dict = {}
for k, v in self.__dict__.iteritems():
if k not in ['soup', 'fake']:
data_dict[(k.replace('_', '', 1))] = v
return data_dict
@property
def counties(self):
if not self._counties:
self.parse_counties()
return self._counties
@property
def reporting_precincts(self):
if not self._reporting_precincts:
self.calculate_totals()
return self._reporting_precincts
@property
def total_precincts(self):
if not self._total_precincts:
self.calculate_totals()
return self._total_precincts
def calculate_totals(self):
self._reporting_precincts = 0
self._total_precincts = 0
for county in self.counties:
self._reporting_precincts += county['reporting_precincts']
self._total_precincts += county['total_precincts']
return dict(
reporting_precincts = self._reporting_precincts,
total_precincts = self._total_precincts,
)
def parse_counties(self):
self._counties = []
for county in self.soup.find_all('Counties'):
results_tag = county.find('CountyResults')
county_output = {
'name': county.find('CountyName').text.strip(),
'reporting_precincts': int(
results.find('ReportingPrecincts').text.strip()
),
'total_precincts': int(
results.find('TotalPrecincts').text.strip()
),
}
# look up the fips by county name and add the k/v to output
county_output['fips'] = fips_lookup[
county_output['name']
]
self._counties.append(county_output)
return self._counties
class CandidateRaceResults(RaceResults):
_candidates = None
def __init__(self, *args, **kwargs):
super(CandidateRaceResults, self).__init__(*args, **kwargs)
@property
def candidates(self):
if not self._candidates:
self.parse_candidates()
return self._candidates
def calculate_totals(self):
super(CandidateRaceResults, self).calculate_totals()
self.parse_candidates()
def parse_candidates(self):
cand_dict = {}
for county in self.counties:
for candidate in county['candidates']:
try:
cand_dict[candidate['id']]
except KeyError:
cand_dict[candidate['id']] = candidate.copy()
else:
cand_dict[candidate['id']]['votes'] += candidate['votes']
self._candidates = [v for v in cand_dict.itervalues()]
def parse_counties(self):
self._counties = []
for county in self.soup.find_all('Counties'):
results = county.find('CountyResults')
county_output = {
'name': county.find('CountyName').text.strip(),
'reporting_precincts': int(
results.find('ReportingPrecincts').text.strip()
),
'total_precincts': int(
results.find('TotalPrecincts').text.strip()
),
'candidates': []
}
# look up the fips by county name and add the k/v to output
county_output['fips'] = fips_lookup[
county_output['name']
]
# loop over the <Party> tags inside the <CountyResults> tag
for party in results.find_all('Party'):
# find the <Candidate> tag
candidate = party.find('Candidate')
candidate_output = {
'party': party.find('PartyName').text.strip(),
'id': party.find('CandidateID').text.strip(),
'name': candidate.find('LastName').text.strip(),
}
if self.fake:
candidate_output['votes'] = randint(100,1000)
else:
candidate_output['votes'] = int(candidate.find('YesVotes').text)
# append candidate dict to candidates list of county_output
county_output['candidates'].append(candidate_output)
self._counties.append(county_output)
return self._counties
class LegislativeRaceResults(CandidateRaceResults):
_district = None
def __init__(self, *args, **kwargs):
super(CandidateRaceResults, self).__init__(*args, **kwargs)
@property
def district(self):
if not self._district:
self._district = int(self.title.split('- District')[1].strip())
return self._district
class BallotIssueResults(RaceResults):
_yes_votes = None
_no_votes = None
def __init__(self, *args, **kwargs):
super(BallotIssueResults, self).__init__(*args, **kwargs)
@property
def yes_votes(self):
if not self._yes_votes:
self.calculate_totals()
return self._yes_votes
@property
def no_votes(self):
if not self._no_votes:
self.calculate_totals()
return self._no_votes
def calculate_totals(self):
super(BallotIssueResults, self).calculate_totals()
self._yes_votes = 0
self._no_votes = 0
for county in self.counties:
self._yes_votes += county['yes_votes']
self._no_votes += county['no_votes']
def parse_counties(self):
self._counties = []
for county in self.soup.find_all('Counties'):
results = county.find('CountyResults')
county_output = {
'name': county.find('CountyName').text.strip(),
'reporting_precincts': int(
results.find('ReportingPrecincts').text.strip()
),
'total_precincts': int(
results.find('TotalPrecincts').text.strip()
),
}
# look up the fips by county name and add the k/v to output
county_output['fips'] = fips_lookup[
county_output['name']
]
if self.fake:
county_output['yes_votes'] = randint(100,1000)
county_output['no_votes'] = randint(100,1000)
else:
county_output['yes_votes'] = int(
results.find('Party').find('Candidate').find('YesVotes').text
)
county_output['no_votes'] = int(
results.find('Party').find('Candidate').find('NoVotes').text
)
self._counties.append(county_output)
return self._counties
def get_latest_results():
import requests
url = 'http://enrarchives.sos.mo.gov/APFeed/Apfeed.asmx/GetElectionResults?'
payload = {'AccessKey': os.environ['APFEED_LIVE_KEY']}
# make a get request for results from the APfeed
response = requests.get(url, params=payload)
return ElectionResults(response.content)