#!/usr/bin/python
#Indiegogo & Kickstarter Tracker with Unicorn Hat

#Put your IGG url below
iggURL = "https://www.indiegogo.com/projects/ryanteck-budget-robotics-kit-for-raspberry-pi/x/6492676"
#Or put your KS URL below
ksURL = "https://www.kickstarter.com/projects/pimoroni/flotilla-for-raspberry-pi-making-for-everyone"
#Set the following to 0 for IGG, 1 for KS
site = 1


import unicornhat as UH
import time
import urllib
lastGoal = 0
sleepTime = 0.04

def getGoal():
	if (site == 0):
		#Lets get URL first
		html = urllib.urlopen(iggURL).read()
		if("InDemand" in html):
			#adds support for campaigns that finish with indemand enabled.
			print "In Demand"
			goal = html.split("% funded")[0]
			goal = goal.split("InDemand</div>\n")[1]
			goal = int(goal.strip())
			return float(goal/100.0)
		else:
			print "igg normal"
			goal = html.split('i-percent">')[1].split('%</div')[0]
			goal = goal.replace(',', '')
			#goal = htmlTest
			goal = int(goal)
			goal = float(goal/100.0)
			return goal
	elif (site == 1):
		print "Kickstarter"
		#Lets get URL first
		html = urllib.urlopen(ksURL).read()
		goal = html.split('data-percent-raised=')[1].split("data-pledged")[0].strip().replace('"','')
		#goal = htmlTest
		goal = float(goal)
		return goal
	
pixelsToXY =[
#row 0
[0,0], #0
[1,0], #1
[2,0], #2
[3,0], #3
[4,0], #4
[5,0], #5
[6,0], #6
[7,0], #7
#row 1
[0,1], #0
[1,1], #1
[2,1], #2
[3,1], #3
[4,1], #4
[5,1], #5
[6,1], #6
[7,1], #7
#row 2
[0,2], #0
[1,2], #1
[2,2], #2
[3,2], #3
[4,2], #4
[5,2], #5
[6,2], #6
[7,2], #7
#row 3
[0,3], #0
[1,3], #1
[2,3], #2
[3,3], #3
[4,3], #4
[5,3], #5
[6,3], #6
[7,3], #7
#row 4
[0,4], #0
[1,4], #1
[2,4], #2
[3,4], #3
[4,4], #4
[5,4], #5
[6,4], #6
[7,4], #7
#row 5
[0,5], #0
[1,5], #1
[2,5], #2
[3,5], #3
[4,5], #4
[5,5], #5
[6,5], #6
[7,5], #7
#row 6
[0,6], #0
[1,6], #1
[2,6], #2
[3,6], #3
[4,6], #4
[5,6], #5
[6,6], #6
[7,6], #7
#ro 7
[0,7], #0
[1,7], #1
[2,7], #2
[3,7], #3
[4,7], #4
[5,7], #5
[6,7], #6
[7,7], #7
]

	
	


#Lets make it SLIGHTLY brigher. TOO BRIGHT CAN DAMAGE EYES
UH.brightness(0.3)

red = 255,000,000
yellow = 255,200,50
green = 000,255,000
orange = 255,153,000
blue = 000,000,255
purple = 255,000,255

while True:
	print "getting goal"
	goal = getGoal()
		
	pixelsOn= int(64.00*(goal))
	if (pixelsOn > 320):
		pixelsOn = 320
	print pixelsOn
	pixel = 0
	
	while pixel < pixelsOn:
		if(pixel <16):
			color = red
			pixelXY = pixelsToXY[pixel]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			
		if(15<pixel and pixel<32):
			color = orange
			pixelXY = pixelsToXY[pixel]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			
		if(31<pixel and pixel<48):
			color = yellow
			pixelXY = pixelsToXY[pixel]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
		
		if(47<pixel and pixel<64):
			color = green	
			pixelXY = pixelsToXY[pixel]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
		
		if(63<pixel and pixel<128):
			color = blue
			pixel2 = pixel - 64
			pixelXY = pixelsToXY[pixel2]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			sleepTime = 0.03
			
		if(127<pixel and pixel<192):
			UH.brightness(0.4)
			color = purple
			pixel2 = pixel - 128
			pixelXY = pixelsToXY[pixel2]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			sleepTime = 0.02	
			
		if(191<pixel and pixel<256):
			UH.brightness(0.5)
			color = blue
			pixel2 = pixel - 192
			pixelXY = pixelsToXY[pixel2]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			sleepTime = 0.02	
			
		if(255<pixel and pixel<320):
			UH.brightness(0.6)
			color = orange
			pixel2 = pixel - 256
			pixelXY = pixelsToXY[pixel2]
			UH.set_pixel(pixelXY[0],pixelXY[1],color[0],color[1],color[2])
			sleepTime = 0.02		
			
		pixel = pixel+1
		time.sleep(sleepTime)
		UH.show()
		

	if(goal != lastGoal):
		repeat = 0
		while repeat < 5:
			UH.brightness(1)
			UH.show()
			time.sleep(0.1)
			UH.brightness(0.1)
			UH.show()
			time.sleep(0.1)
			repeat = repeat +1
		UH.brightness(0.3)
		UH.show()
		lastGoal = goal
		
	time.sleep(10)
	UH.clear()


