-
Notifications
You must be signed in to change notification settings - Fork 0
/
jefferson-co.py
36 lines (27 loc) · 947 Bytes
/
jefferson-co.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
import requests
import re
import sys
import json
from lxml import etree
url = 'https://www.jccal.org/elections/'
response = requests.get(url)
tree = etree.fromstring(re.subn(r'<(script).*?<\/\1>(?s)', '', response.text)[0])
pre = tree.xpath('//PRE/text()')
lines = pre[0].split('\n\n')
total = {}
for l in lines[2:]:
vote = l.split('\n')
vote_name = vote[0].strip()
total[vote_name] = {}
for i, line in enumerate(vote):
if i > 1 and ('(VOTE FOR) 1' not in line):
count = re.split('(\. +){2,}', line.strip())
if count[0]:
candidate = count[0].strip()
tallies = [f for f in re.split(' ', count[-1]) if f]
if len(tallies) == 1:
tallies = [0, 0]
total[vote_name].update(
{candidate: {'count': tallies[0], 'percentage': tallies[1]}}
)
json.dump(total, sys.stdout, indent=4)