Skip to content

Commit

Permalink
Update UI to remove console window and add connection fault label
Browse files Browse the repository at this point in the history
  • Loading branch information
henryvincent96 committed Nov 22, 2018
1 parent ac097fb commit f98bb53
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 108 deletions.
24 changes: 23 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
{
"cSpell.words": [
"a",
"aj",
"br",
"e",
"fys",
"gqxm",
"hj",
"hjzr",
"iza",
"l",
"ns",
"qmh",
"r",
"resizable",
"set"
"set",
"sjsb",
"sy",
"tze",
"usv",
"vd",
"y",
"yo",
"yw",
"z"
]
}
225 changes: 118 additions & 107 deletions pewdiepieDownfall.py → pewdiepieDownfall.pyw
Original file line number Diff line number Diff line change
@@ -1,108 +1,119 @@
import urllib.request
import json
import time
import platform
import threading
import os
from appJar import gui

#gets sub count from youtube for a given channel ID
def getSubCount(id):
data = urllib.request.urlopen('https://www.googleapis.com/youtube/v3/channels?part=statistics&id='
+ id + '&key=' + key).read()
return json.loads(data.decode('utf-8'))['items'][0]['statistics']['subscriberCount']

#Calculates the difference between PewDiePie and T Series
#For optimisation, sub counts are global variables
def getCountdown():
global pdpCount, tseriesCount
pdpCount = int(getSubCount(pdpID))
tseriesCount = int(getSubCount(tseriesID))
return pdpCount - tseriesCount

#variables required for API
key = 'AIzaSyBR7ZQmh02ETze1hjNS7rFYsSJSBYoUsvY'
pdpID = 'UC-lHJZR3Gqxm24_Vd_AJ5Yw'
tseriesID = 'UCq-Fj5jknLsUf-MWSy4_brA'

exitFlag = False

class Counter (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print('Starting ' + self.name)
startCounter(self.name, 5, self.counter)
print('Exiting ' + self.name)

#init
pdpCount = 0
tseriesCount = 0
netChange = 0
previousCount = 0
newCount = getCountdown()

def startCounter(threadName, counter, delay):
global newCount, netChange
while True:
#logic
try:
if(exitFlag):
threadName.exit()

previousCount = newCount
newCount = getCountdown()
netChange = newCount - previousCount

if netChange >= 0:
netChange = str('+' + str(netChange))
except TimeoutError:
print('Timeout error')
except urllib.error.URLError:
print('URL error')
#don't really want google thinking I'm DDOSing them
time.sleep(delay)

counterThread = Counter(1, "Main Counter", 1)
counterThread.start()

#GUI labels
pSubCountTtl = 'pSubCountTtl'
pSubCountVal = 'pSubCountval'
tSubCountTtl = 'tSubCountTtl'
tSubCountVal = 'tSubCountVal'
diffTtl = 'diffTtl'
diffVal = 'diffVal'

#Setup GUI
app = gui('PewDiePie Doom Countdown', showIcon=False)
app.setResizable(canResize=True)
app.setSize(330, 86)
app.setFont(size=11)
app.setStretch("none")
app.setSticky("w")

app.addLabel(pSubCountTtl, 'PewDiePie Sub Count:', 0, 0)
app.addLabel(tSubCountTtl, 'TSeries Sub Count:', 1, 0)
app.addLabel(diffTtl, 'Current Difference:', 2, 0)

app.addLabel(pSubCountVal, '{:,}'.format(pdpCount), 0, 1)
app.addLabel(tSubCountVal, '{:,}'.format(tseriesCount), 1 ,1)
app.addLabel(diffVal, '{:,}'.format(newCount) + ' (' + str(netChange) + ')', 2, 1)

def updateValLabels():
app.setLabel(pSubCountVal, '{:,}'.format(pdpCount))
app.setLabel(tSubCountVal, '{:,}'.format(tseriesCount))
app.setLabel(diffVal, '{:,}'.format(newCount) + ' (' + str(netChange) + ')')

def quitBehaviour():
os._exit(0)

app.registerEvent(updateValLabels)
app.setStopFunction(quitBehaviour)

#LaunchGUI
import urllib.request
import json
import time
import platform
import threading
import os
from appJar import gui

#gets sub count from youtube for a given channel ID
def getSubCount(id):
data = urllib.request.urlopen('https://www.googleapis.com/youtube/v3/channels?part=statistics&id='
+ id + '&key=' + key).read()
return json.loads(data.decode('utf-8'))['items'][0]['statistics']['subscriberCount']

#Calculates the difference between PewDiePie and T Series
#For optimisation, sub counts are global variables
def getCountdown():
global pdpCount, tseriesCount
pdpCount = int(getSubCount(pdpID))
tseriesCount = int(getSubCount(tseriesID))
return pdpCount - tseriesCount

#variables required for API
key = 'AIzaSyBR7ZQmh02ETze1hjNS7rFYsSJSBYoUsvY'
pdpID = 'UC-lHJZR3Gqxm24_Vd_AJ5Yw'
tseriesID = 'UCq-Fj5jknLsUf-MWSy4_brA'

exitFlag = False

class Counter (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print('Starting ' + self.name)
startCounter(self.name, 5, self.counter)
print('Exiting ' + self.name)

#init
pdpCount = 0
tseriesCount = 0
netChange = 0
previousCount = 0
newCount = getCountdown()
connectionFault = False

def startCounter(threadName, counter, delay):
global newCount, netChange, connectionFault
while True:
#logic
try:
if(exitFlag):
threadName.exit()

previousCount = newCount
newCount = getCountdown()
netChange = newCount - previousCount

if netChange >= 0:
netChange = str('+' + str(netChange))

if (connectionFault): connectionFault = False
except TimeoutError:
print('Timeout error')
connectionFault = True
except urllib.error.URLError:
print('URL error')
connectionFault = True
#don't really want google thinking I'm DDOSing them
time.sleep(delay)

counterThread = Counter(1, "Counter", 1)
counterThread.start()

#GUI labels
pSubCountTtl = 'pSubCountTtl'
pSubCountVal = 'pSubCountval'
tSubCountTtl = 'tSubCountTtl'
tSubCountVal = 'tSubCountVal'
diffTtl = 'diffTtl'
diffVal = 'diffVal'
errorTtl = 'errorTtl'

#Setup GUI
app = gui('PewDiePie Doom Countdown', showIcon=False)
app.setResizable(canResize=True)
app.setSize(330, 86)
app.setFont(size=11)
app.setStretch("none")
app.setSticky("w")

app.addLabel(pSubCountTtl, 'PewDiePie Sub Count:', 0, 0)
app.addLabel(tSubCountTtl, 'TSeries Sub Count:', 1, 0)
app.addLabel(diffTtl, 'Current Difference:', 2, 0)
app.addLabel(errorTtl, 'ERROR: ', 3, 0)

app.addLabel(pSubCountVal, '{:,}'.format(pdpCount), 0, 1)
app.addLabel(tSubCountVal, '{:,}'.format(tseriesCount), 1 ,1)
app.addLabel(diffVal, '{:,}'.format(newCount) + ' (' + str(netChange) + ')', 2, 1)

def updateValLabels():
app.setLabel(pSubCountVal, '{:,}'.format(pdpCount))
app.setLabel(tSubCountVal, '{:,}'.format(tseriesCount))
app.setLabel(diffVal, '{:,}'.format(newCount) + ' (' + str(netChange) + ')')
if(connectionFault):
app.setLabel(errorTtl, 'CONNECTION FAULT')
else:
app.setLabel(errorTtl, '')

def quitBehaviour():
os._exit(0)

app.registerEvent(updateValLabels)
app.setStopFunction(quitBehaviour)

#LaunchGUI
app.go()

0 comments on commit f98bb53

Please sign in to comment.