-
-
Save dominicgs/4029402 to your computer and use it in GitHub Desktop.
Say election results (with Linux support)
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
#!/usr/bin/env python | |
import time | |
import json | |
import urllib2 | |
import os | |
platform = os.uname()[0] | |
if platform == 'Darwin': | |
import subprocess | |
def speak(phrase): | |
subprocess.call(['say', phrase]) | |
if platform == 'Linux': | |
# Use python espeak module | |
from espeak import espeak | |
def speak(phrase): | |
espeak.synth(phrase) | |
else: | |
raise Exception('Unsupported OS (%s)' % platform) | |
def check_results(): | |
page = urllib2.urlopen('http://elections.nytimes.com/2012/results/president/big-board.json') | |
data = json.loads(page.read()) | |
new_results = (str(data['tally']['dem_electoral_won']), str(data['tally']['gop_electoral_won'])) | |
print "Latest data: {0}".format(new_results) | |
if new_results != RESULTS: | |
for candidate, result in zip(['Obama', 'Romney'], new_results): | |
speak('%s %s' % (candidate, result)) | |
if int(result) >= 270: | |
speak('%s has won' % candidate) | |
return None | |
time.sleep(0.5) | |
return new_results | |
if __name__ == '__main__': | |
RESULTS = ('', '') | |
while True: | |
try: | |
RESULTS = check_results() | |
except: | |
print "Error retrieving results - retry in 60 seconds" | |
if RESULTS == None: | |
break | |
# wait 2 minutes | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment