Skip to content

Commit

Permalink
Added two candidate preferred data
Browse files Browse the repository at this point in the history
  • Loading branch information
kennib committed Mar 12, 2016
1 parent debd60d commit 5ed4b0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 16 additions & 0 deletions queries/two_candidate_preferred.sql
@@ -0,0 +1,16 @@
SELECT votes, event.name, election.name, contest.name, party.name, candidate.name
FROM two_candidate_preferred AS fp
JOIN event
ON event.id = fp.event_id
JOIN election
ON election.event_id = fp.event_id
AND election.id = fp.election_id
JOIN contest
ON contest.event_id = fp.event_id
AND contest.election_id = fp.election_id
AND contest.id = fp.contest_id
JOIN party
ON fp.party_id = party.id
JOIN candidate
ON candidate.id = fp.candidate_id
;
13 changes: 10 additions & 3 deletions scraper.py
Expand Up @@ -72,9 +72,16 @@ def contest_data(xml, event_id, election_id):
name = contest.find('eml:ContestName', NS).text
enrolment = int(xml.find('aec:Enrolment', NS).text)

# First preference data
first_preferences = xml.find('aec:FirstPreferences', NS)
candidates = first_preferences.xpath('.//aec:Candidate', namespaces=NS)
candidates_data = [candidate_data(candidate, event_id, election_id, id) for candidate in candidates]
candidates_data = [candidate_data(candidate, event_id, election_id, id, 'first_preferences') for candidate in candidates]

# Two candidate preference data
two_candidate_preferred = xml.find('aec:TwoCandidatePreferred', NS)
if two_candidate_preferred is not None:
candidates = two_candidate_preferred.xpath('.//aec:Candidate', namespaces=NS)
candidates_data = [candidate_data(candidate, event_id, election_id, id, 'two_candidate_preferred') for candidate in candidates]

scraperwiki.sqlite.save(table_name='contest',
unique_keys=['event_id', 'election_id', 'id'],
Expand All @@ -83,7 +90,7 @@ def contest_data(xml, event_id, election_id):
return {'id': id, 'name': name, 'enrolment': enrolment, 'candidates': candidates_data}

# This function takes an lxml object and returns candidate data
def candidate_data(xml, event_id, election_id, contest_id):
def candidate_data(xml, event_id, election_id, contest_id, kind):
candidate = xml.find('eml:CandidateIdentifier', NS)
id = candidate.get('Id')
name = candidate.find('eml:CandidateName', NS).text
Expand All @@ -98,7 +105,7 @@ def candidate_data(xml, event_id, election_id, contest_id):
unique_keys=['id'],
data={'id': id, 'name': name})

scraperwiki.sqlite.save(table_name='first_preferences',
scraperwiki.sqlite.save(table_name=kind,
unique_keys=['event_id', 'election_id', 'contest_id', 'candidate_id'],
data={'event_id': event_id, 'election_id': election_id, 'contest_id': contest_id, 'candidate_id': id, 'party_id': party.get('id'),
'elected': elected, 'incumbent': incumbent, 'votes': votes})
Expand Down

0 comments on commit 5ed4b0a

Please sign in to comment.