Skip to content

Instantly share code, notes, and snippets.

@dominicgs
Forked from ipmb/election_results.py
Created November 7, 2012 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominicgs/4029402 to your computer and use it in GitHub Desktop.
Save dominicgs/4029402 to your computer and use it in GitHub Desktop.
Say election results (with Linux support)
#!/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