-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kentucky.py
45 lines (36 loc) · 1.8 KB
/
Kentucky.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
import requests # External depdendencies -- run pip install -r requirements.txt
from slugify import slugify
# from pyquery import PyQuery as pq
import os
import json
import datetime
import configuration # Local file, configuration.py, with settings
import clarityparser # Local file, clarityparser.py
countyname = "Kentucky" # Critical to have this!
rawtime = datetime.datetime.now()
snapshotsdir = configuration.snapshotsdir
filename = configuration.filename
timestamp = datetime.datetime.strftime(rawtime, "%Y%m%d-%H%M%S")
filepath = snapshotsdir + slugify(countyname) + "/" + timestamp + "/"
pbcbaseurl = "https://results.enr.clarityelections.com/KY/"
zipsuffix = "/reports/summary.zip"
pbcjson = json.loads(requests.get(pbcbaseurl + "elections.json").content)
electionid = pbcjson[0]['EID']
html = requests.get(pbcbaseurl + electionid).content
# print(pq(html))
# <html><head>
# <script src="./214792/js/version.js" type="text/javascript"/>
# <script type="text/javascript">TemplateRedirect("summary.html","./214792", "", "");</script>
# </head></html>
#
# You know what would make this more impressively hard to maintain? Let's take Javascript embedded into HTML
# and parse it with a string function. Take the stuff between the first and second slashes ...
magicnumber = str(html).split("/")[1] # Take the newest election
pbczipurl = pbcbaseurl + electionid + "/" + magicnumber + zipsuffix
print("Saving " + pbczipurl + " to " + filepath)
os.makedirs(filepath, exist_ok=True)
zipfilename = filepath + filename
with open(zipfilename, "wb") as f:
f.write(requests.get(pbczipurl).content)
print("Zip file retrieved for " + countyname + ". Beginning to parse.")
clarityparser.bring_clarity(rawtime, countyname)