forked from belowtheline/site_2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc.py
76 lines (61 loc) · 2.4 KB
/
abc.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
#!/usr/bin/env python
import json
import os.path
import sys
from bs4 import BeautifulSoup
import requests
soup = BeautifulSoup(requests.get('http://www.abc.net.au/news/federal-election-2013/guide/candindex/').text)
parties = {}
for tbody in soup.findAll('tbody'):
for row in tbody.findAll('tr'):
last, first = [str(x) for x in row.find('td').text.split(', ', 1)]
party = row.find('td', attrs={'class': 'party'})
parties[party.text] = party.find('span')['title']
party = party.text.lower()
candidate = row.find('td', attrs={'class': 'electorate'}).text
if first.endswith("(Sitting MP)"):
sitting = True
first = first[:-13]
else:
sitting = False
if candidate.startswith("Senate"):
start = candidate.find('(') + 1
end = candidate.find(')')
candidate = 'state/' + candidate[start:end].lower()
else:
end = candidate.find('(') - 1
candidate = str(candidate[:end]).lower().translate(None, " -'")
candidate = 'division/' + candidate
if first == 'Ian' and last == 'McDonald':
last = "Macdonald"
filename = '{}-{}.json'.format(last.lower().translate(None, " -'"),
first.lower().translate(None, " -'"))
if sitting or os.path.exists('data/people/' + filename):
data = json.loads(open('data/people/' + filename).read())
data['candidate'] = candidate
else:
data = {
'first_name': first,
'last_name': last,
'candidate': candidate,
}
if party not in ('ind', '-'):
data['party'] = party
# print filename
# print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
open('data/people/' + filename, 'w').write(json.dumps(data, sort_keys=True,
indent=4, separators=(',', ': ')))
print open('data/people/' + filename).read()
print ""
for code, name in parties.items():
if code == '-':
continue
data = {
'name': name,
'code': code.upper(),
}
filename = '{}.json'.format(code.lower())
open('data/parties/' + filename, 'w').write(json.dumps(data, sort_keys=True,
indent=4, separators=(',', ': ')))
print open('data/parties/' + filename).read()
print ""