Created
April 17, 2012 17:31
-
-
Save anonymous/2407659 to your computer and use it in GitHub Desktop.
election results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import lxml | |
from lxml import etree | |
from decimal import * | |
from jinja2 import Template | |
import codecs | |
r = requests.get('http://www.azsos.gov/ftp/dbelec/detail.xml') | |
xml_source = r.text | |
root = etree.fromstring(xml_source) | |
timestamp = root.findall('Timestamp')[0].text | |
contests_xml = root.findall('Contest') | |
contests = [] | |
for contest_xml in contests_xml: | |
choices_xml = contest_xml.findall('Choice') | |
choices = [] | |
vote_count = 0 | |
for choice_xml in choices_xml: | |
choice = { | |
'name': choice_xml.attrib['text'], | |
'votes': Decimal(choice_xml.attrib['totalVotes']), | |
} | |
vote_count += choice['votes'] | |
choices.append(choice) | |
for choice in choices: | |
choice['pc_votes'] = '%0.1f' % (choice['votes'] / vote_count * 100) | |
contest = { | |
'key': contest_xml.attrib['key'], | |
'name': contest_xml.attrib['text'], | |
'timestamp': timestamp, | |
'choices': choices, | |
} | |
contests.append(contest) | |
template_file = codecs.open('templates/graph.html','r', 'utf-8') | |
template_string = template_file.read() | |
template = Template(template_string) | |
template_file.close() | |
for contest in contests: | |
output_file = codecs.open('/Users/evan.wyloge/Dropbox/Public/election_results/%s.html' % contest['key'], 'w', 'utf-8') | |
output_file.write(template.render(contest=contest)) | |
output_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment