Projects Raspberry Pi

Raspberry Pi + Unicorn Hat = UK Election Seat Tracker

||||| 0 Like |||||

On Thursday 7th May 2015, the UK vote for who they want running the country next. Pete Taylor over at http://kimondo.co.uk/ devised a very cool “Swingometer” to track candidates that have signed up for “Concern World-Wide”.

Well, I loved the thought of this – but decided to write some code that would track the number of seats won on the night.

I am currently using http://www.electionforecast.co.uk/ as I can scrape their page for the information I need.

Here is a quick video of it in action:

Long story short – here is some code (it’s messy and a little buggy – I will be doing some code clean up later). It could be a lot better with procedures – but .. maybe I will get to that later).

from BeautifulSoup import BeautifulSoup
import urllib2
import requests
import unicornhat as UH
import time
import sys

UH.brightness(0.05)

url = 'http://www.electionforecast.co.uk/index.html'
page=urllib2.urlopen(url)
soup = BeautifulSoup(page)

items = soup.findAll('strong')

cons = int(items[0].string)
cons = cons / 10
i = 1	
x = 0
while  i < cons :
   if i < 9 :
      UH.set_pixel(x, 0, 4, 117, 201)
      i = i + 1
      x = x + 1
      UH.show()
   elif i >= 9 and i < 17:
      if x == 8 : 
         x = 0
      UH.set_pixel(x, 1, 4, 117,201)
      i = i + 1
      x = x + 1
      UH.show()
   elif i >= 17 and i < 25 : 
      if x == 8 : 
         x = 0
      UH.set_pixel(x, 2, 4, 117,201)
      UH.show()
      x = x + 1
      i = i + 1
   elif i >= 25 and i < 33 :
      if x == 8 : 
         x = 0
      UH.set_pixel(x, 3, 4, 117,201)
      UH.show()
      x = x + 1
      i = i + 1
   elif i >= 33 and i < 39  :
      if x == 8 : x = 0
      UH.set_pixel(x, 4, 4, 117,201)
      UH.show()
      x = x + 1
      i = i + 1

time.sleep(1)
#print items[1].string

lab = int(items[1].string)
lab = lab / 10

lab = lab + i
ii = i


while ii < lab :
   if ii < 9 :
      UH.set_pixel(x, 0, 244, 0, 0)
      ii = ii + 1
      x = x + 1
      UH.show()
   elif ii >= 9 and ii < 17:
      if x == 8 :
         x = 0
      UH.set_pixel(x, 1, 244,0,0)
      ii = ii + 1
      x = x + 1
      UH.show()
   elif ii >= 17 and ii < 25 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 2, 244,0,0)
      UH.show()
      x = x + 1
      ii = ii + 1
   elif ii >= 25 and ii < 33 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 3, 244,0,0)
      UH.show()
      x = x + 1
      ii = ii + 1
   elif ii >= 33 and ii < 41  :
      if x == 8 : x = 0
      UH.set_pixel(x, 4, 244,0,0)
      UH.show()
      x = x + 1
      ii = ii + 1
   elif ii >= 41 and ii < 49  :
      if x == 8 : x = 0
      UH.set_pixel(x, 5, 244,0,0)
      UH.show()
      x = x + 1
      ii = ii + 1
   elif ii >= 49 and ii < 58  :
      if x == 8 : x = 0
      UH.set_pixel(x, 6, 244,0,0)
      UH.show()
      x = x + 1
      ii = ii + 1

#print items[2].string

snp = int(items[2].string)
snp = snp / 10

snp = snp + ii
iii = ii


while iii < snp :
   if iii < 9 :
      UH.set_pixel(x, 0, 244, 244, 0)
      iii = iii + 1
      x = x + 1
      UH.show()
   elif iii >= 9 and iii < 17:
      if x == 8 :
         x = 0
      UH.set_pixel(x, 1, 244,244,0)
      iii = iii + 1
      x = x + 1
      UH.show()
   elif iii >= 17 and iii < 25 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 2, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1
   elif iii >= 25 and iii < 33 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 3, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1
   elif iii >= 33 and iii < 41  :
      if x == 8 : x = 0
      UH.set_pixel(x, 4, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1
   elif iii >= 41 and iii < 49  :
      if x == 8 : x = 0
      UH.set_pixel(x, 5, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1
   elif iii >= 49 and iii < 57  :
      if x == 8 : x = 0
      UH.set_pixel(x, 6, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1
   elif iii >= 57 and iii < 66  :
      if x == 8 : x = 0
      print x
      print iii
      UH.set_pixel(x, 7, 244,244,0)
      UH.show()
      x = x + 1
      iii = iii + 1

#print items[3].string

lib = int(items[3].string)
lib = lib / 10

lib =  lib + iii
iiii = iii


while iiii < lib :
   if iiii < 9 :
      UH.set_pixel(x, 0, 244, 152, 0)
      iiii = iiii + 1
      x = x + 1
      UH.show()
   elif iiii >= 9 and iiii < 17:
      if x == 8 :
         x = 0
      UH.set_pixel(x, 1, 244, 152, 0)
      iiii = iiii + 1
      x = x + 1
      UH.show()
   elif iiii >= 17 and iiii < 25 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 2, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1
   elif iiii >= 25 and iiii < 33 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 3, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1
   elif iiii >= 33 and iiii < 41  :
      if x == 8 : x = 0
      UH.set_pixel(x, 4, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1
   elif iiii >= 41 and iiii < 49  :
      if x == 8 : x = 0
      UH.set_pixel(x, 5, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1
   elif iiii >= 49 and iiii < 58  :
      if x == 8 : x = 0
      UH.set_pixel(x, 6, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1
   elif iiii >= 58 and iiii < 67  :
      if x == 8 : x = 0
      UH.set_pixel(x, 7, 244, 152, 0)
      UH.show()
      x = x + 1
      iiii = iiii + 1

#print items[4].string
#print items[5].string
#print items[6].string
#print items[7].string
#print items[8].string
#print items[9].string

oth = int(items[3].string) + int(items[4].string) + int(items[5].string) + int(items[6].string) + int(items[7].string) + int(items[8].string) + int(items[9].string)
oth = lib / 10

oth =  oth + iiii
iiiii = iiii


while iiiii < oth :
   if iiiii < 9 :
      UH.set_pixel(x, 0, 192,192,192)
      iiiii = iiiii + 1
      x = x + 1
      UH.show()
   elif iiiii >= 9 and iiiii < 17:
      if x == 8 :
         x = 0
      UH.set_pixel(x, 1, 192,192,192)
      iiiii = iiiii + 1
      x = x + 1
      UH.show()
   elif iiiii >= 17 and iiiii < 25 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 2, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   elif iiiii >= 25 and iiiii < 33 :
      if x == 8 :
         x = 0
      UH.set_pixel(x, 3, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   elif iiiii >= 33 and iiiii < 41  :
      if x == 8 : x = 0
      UH.set_pixel(x, 4, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   elif iiiii >= 41 and iiiii < 49  :
      if x == 8 : x = 0
      UH.set_pixel(x, 5, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   elif iiiii >= 49 and iiiii < 58  :
      if x == 8 : x = 0
      UH.set_pixel(x, 6, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   elif iiiii >= 58 and iiiii < 66  :
      if x == 8 : x = 0
      UH.set_pixel(x, 7, 192,192,192)
      UH.show()
      x = x + 1
      iiiii = iiiii + 1
   else:
      time.sleep(10)
      sys.exit()

time.sleep(10)
UH.clear()

 

Leave a Reply

Your email address will not be published. Required fields are marked *