[Eric] has put together a simple python script to scrape election results from CNN.com. It uses urllib2 to return the popular and electoral votes for each party and throws an ElectionWon exception when CNN calls the race. He’s planning on hooking this to DMX controlled RGB LED lighting that will shift to either blue or red as the night progresses. It’s a great starting point if you want to pull off something similar.
You may remember [Eric] for building the IKEA MAME table and the TRS-80 wireless terminal.
[photo: skenmy]
UPDATE: [Garrett] of macetech is putting the finishing touches on his version which uses 32 ShiftBrite modules and 2 4-digit displays controlled by a CuBLOC.
Oh, but how will I build this in less than 24 hours with Radioshack closed?
@tricky
radioshack wouldn’t help you too much with DMX lighting fixtures. you happen to have one though, ranging from a $70 chauvet PAR to a $2000 martin strip light, the DMX protocol is pretty simple as I understand it. Should be easy to fake up with a uc of some sort.
This is PERFECT for any election party. Imagine watching the election on TV and having all the walls around you shift colors like that as your guests yell at it for being blue or red when they want it red or blue.
can this be rigged with an automatic catapult that throws eggs at a poster of the losing candidate?
You can do this tomorrow if you happen to live near a Guitar Center. Go in a buy a few LED par cans. Grab some XLR cables. Buy a DMX controller. I’m using a ENTTEC DMX USB PRO, which might not be available at Guitar Center, but an alternative certainly is.
Though the DMX protocol is quite simple, it does have some tricky timing. I’d caution against trying to whip it up from scratch given the fact that it can be done on a computer quite cheaply.
I was actually just integrating the code I posted with the DMX code (from a previous project) and I’ll update the blog entry with the final code shortly.
This is a nifty post, however it would of been nice if it was posted a few days ago. I’m way too drunk to do anything slick with it now.
I’m building one of these right now. I’ve got his code running, with some modifications to send the data to a Bluetooth/serial module using rfcomm. It’ll have a red/blue bar graph and large numerical displays with the count at either end. It’s going to look like an 80’s cartoon battle….
I’m parsing the data now, but the tough part is I’m not sure what the format of the popular vote variable will be. Maybe it’s a plain number, maybe it’s thousands-separated, etc. I guess I will have to tune the code a little bit tomorrow. But the physical construction is next….
eric gradman
can you give more details on the trs80 mod
Any ideas as to how to get state by state percentages? They appear to be burried in one of the other files that the html links to.
Yay, it’s working! Okay: 32 RGB ShiftBrite modules in a row, running off a Cubloc CB405, receiving data over a Bluetooth connection from a Linux box running in a closet. That box is running a modified version of the CNN scraper. I’m using the 32 LEDs as a bar graph, and I have two red 2″ 4-digit 7 segment displays showing the actual electoral counts. Just going to tack this all to a plank tomorrow, it’s ready! Pics: http://www.flickr.com/photos/macetech/3001651751/
Grab a couple of super bright red led’s and a couple of super bright BLue LED’s
write a simple pic project to modulate them and get data via rs232. use a pc to collect the data and send the numbers to the pic.
same thing in 20 minutes and far less money spent.
will you fill a room full of light? no, but then the DMX lighting setup will run several hundred anyways.
I added a touch of code to the python scrape to convert the normalized values into hex color codes and send that over serial to my arduino.
I’d say it works well, but we’re still at 0,0 :)
but hey, no dmx, just arduino and a few red and blue leds.
what kind of numbers does this return? i want to do something with this but am not good enough at python to get it from reading the code. thanks in advanced.
it better turn green
I modified their code to make a live election ticker.
import urllib2
import re
import os
import time
def ElectionWon(winner):
while 1:print winner, ” WIN!!!!!!”
class CNN(object):
def __init__(self):
self.url = “http://www.cnn.com/ELECTION/2008/results/president/”
def get(self):
“””return the electoral balance
(dpopular, delectoral), (rpopular, relectoral)
“””
u = urllib2.urlopen(self.url)
for line in u.readlines():
res = re.search(r’var CNN_NData=(.*?);’, line)
if res is not None:
data = res.group(1)
data = data.replace(“true”, “True”)
data = data.replace(“false”, “False”)
data = eval(data)
d,r = None, None
for candidate in data[‘P’][‘candidates’]:
if candidate[‘party’] == ‘D’:
d = candidate[‘votes’], candidate[‘evotes’]
if candidate[‘winner’]:
ElectionWon(“DEMOCRATS”)
elif candidate[‘party’] == ‘R’:
r = candidate[‘votes’], candidate[‘evotes’]
if candidate[‘winner’]:
ElectionWon(“REPUBLICANS”)
if d[1] > r[1]: leader = “<<<<< d[1]: leader = “>>>>>>”
if d == r: leader = “——”
print “Democrats Popular:”,d[0],” Democrats Electoral:”,d[1],leader,” Republicans Popular:”,r[0],” Republicans Electoral:”,r[1]
return 0
if __name__==’__main__’:
cnn = CNN()
os.system(“clear”)
while 1:
cnn.get()
time.sleep(2)
os.system(“clear”)
“””DOS users should replace ‘clear’ with ‘cls'”””
I put up a howto for my election meter, It’s sitting over the fireplace right now showing the results: http://www.macetech.com/blog/node/67
Btw, if anyone is wondering what dmx is, check my link.
Better add the link :P