, , , , , , ,

After the roaring success of the Bieb-o-meter in the previous post, it was time to crank up the stakes. Our premise was simple. Read the tweets of BBC programme The Voice UK during transmission – if a judge is mentioned in the latest tweet then light up their bulb.

I got busy coding in Python Tools for Visual Studio, before porting the code to my Raspberry Pi. The initial plan was to use an RSS feed, but Twitter has dropped support, so it gave me an opportunity to look further at Beautiful Soup and the wonders of traditional web scraping.

Here’s the code…

# funtion to take care of logging
def log(judge, tweet):

       file = open("thevoice.txt", "a")
       file.write(judge + ": " + tweet + "\n\n")
   except Exception,e:
       print str(e)

# imports
import serial
from urllib2 import urlopen
from bs4 import BeautifulSoup
from time import sleep

# com port and speed
port = '/dev/ttyAMA0'
baud = 9600
# establish a serial connection
ser = serial.Serial(port=port, baudrate=baud)

# ensure bulbs off to start with

# set up our judges
WILLIAM = "@iamwill"
KYLIE = "@kylieminogue"
TOM = "@RealSirTomJones"
RICKY = "@Rickontour"

# we'll need to keep an eye on previous tweet
prevTweet = ""

# loop til we eat up all system resources and die
while True:

        # get The Voice twitter page
        page = urlopen("https://twitter.com/BBCTheVoiceUK")
        soup = BeautifulSoup(page)     
        # grab the latest tweet
        tweet = soup.find('p', attrs={'class':'js-tweet-text tweet-text'}).getText()
        print ("Error getting tweet")
    # if no new tweet available then nothin' doin'
    if tweet == prevTweet:
        prevTweet = tweet

    # inspect tweet and switch on judge's bulbs
    if WILLIAM in tweet:
        ser.write('a--D05HIGH--') # yellow bulb
        log(WILLIAM, tweet)

    if KYLIE in tweet:
        ser.write('a--D06HIGH--') # blue bulb
        log(KYLIE, tweet)
    if TOM in tweet:
        ser.write('a--D11HIGH--') # red bulb
        log(TOM, tweet)
    if RICKY in tweet:
        ser.write('a--D13HIGH--') # green bulb
        log(RICKY, tweet)

# sadly this will never happen

Firstly we define a function for logging our results (note: Arkwood will need to reintroduce encoding on file open, as exceptions are being thrown). Next we import our Python packages and set up a serial port.

There are four judges on The Voice to monitor – so, for example, we will scan tweets for any mention of the lovely Kylie using @kylieminogue. Great.

The rest of the code is concerned with looping indefinitely, reading new tweets and switching on the judges bulbs if they get a shout out on Twitter (or switching them off otherwise). will.i.am has the yellow bulb, Kylie gets blue, Tom red, Ricky green. The bulbs are attached to the XinoRF, an Arduino-based development board supplied with the RasWIK Wireless Inventor Kit – what this means is that whilst the Raspberry Pi is busy in my spare bedroom polling for tweets, the bulbs can be placed in my living room waiting to be wirelessly triggered.

Here’s the set up:


How exciting. Arkwood and I sat down on the sofa as The Voice began at 7pm prompt on our TV set. My Belgian friend was so ecstatic he was stretching his Jonas Brothers T-shirt over his knees. I was calm, sipping my pint of Morgan’s Spiced and ginger beer. Then Kylie gave a nod to her three judge buddies on Twitter – will.i.am, Tom and Ricky – and their three respective bulbs switched on. Arkwood was so beside himself that he kissed me. I told him not to do it again (by way of punching him in the gonads).

The next tweet had no mention of the judges, so all bulbs were switched off. Then someone mentioned Kylie, and the tiny pop goddess’ blue light flickered:


Hooray! Oh, and here’s the Twitter account of BBC programme The Voice which we are scraping:


It was indeed a wonderous occasion. Arkwood exclaimed, ‘Let’s do the same for Eurovision!’ I told him not to be a damn fool. ‘We’d need around forty bulbs you… ignoramus.’

Still, we were rather pleased with ourselves. I threw beef Hula Hoops into the air and caught them with my open mouth. Arkwood drew his T-shirt over his stomach and fingered his belly button. ‘If you fancy the Jonas Brothers,’ I winked, ‘cover your belly.’