Created
June 14, 2016 21:01
-
-
Save zmbush/987281a1d4680c301ef72761b877a218 to your computer and use it in GitHub Desktop.
Fetch california election results
This file contains hidden or 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
while true; do | |
curl http://api.sos.ca.gov/api/president/party/democratic?format=csv | tail -n +5 > results-$(date +%m-%d_%H%M) | |
python process.py | |
sleep 1h | |
done |
This file contains hidden or 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 glob | |
import csv | |
import re | |
def main(): | |
print "Time,Votes for Hillary,Votes for Bernie,Total,% Hillary,% Bernie" | |
for fname in sorted(glob.glob("results-*")): | |
with open(fname, 'rb') as csvfile: | |
res = re.search("results-(..)-(..)_(..)(..)", fname) | |
month = res.group(1) | |
day = res.group(2) | |
hour = res.group(3) | |
minute = res.group(4) | |
reader = csv.DictReader(csvfile) | |
print "2016/%s/%s %s:%s:00," % (month, day, hour, minute), | |
total = 0 | |
candidateVotes = {} | |
for row in reader: | |
votes = int(row['Votes']) | |
total += votes | |
candidateVotes[row['CandidateName']] = votes | |
print "%d, %d, %d, %.02f%%, %.02f%%" % ( | |
candidateVotes["Hillary Clinton"], | |
candidateVotes["Bernie Sanders"], | |
total, | |
float(candidateVotes["Hillary Clinton"]) / total * 100.0, | |
float(candidateVotes["Bernie Sanders"]) / total * 100.0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment