Refactored code for TRUE multithreaded performance startup time decreased from 2 secs to 0.2 secs updates song time decreased from 3 secs to 0.1 secs introduced proper and full error handler, can fully recover from errors (including performing a full restart) implemented web server to push updates and restart application (flask) getting lyrics is truely mulithreaded now, all in memory.
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
import urllib
|
|
from urllib.request import urlopen
|
|
from zipfile import ZipFile
|
|
from io import BytesIO
|
|
from time import sleep
|
|
import subprocess
|
|
from pynput import mouse
|
|
from functools import partial
|
|
from os import name, path, remove
|
|
|
|
|
|
# while True:
|
|
# try:
|
|
# urlopen('http://bbrunson.com', timeout=1)
|
|
# print("Connection to server established.")
|
|
# break
|
|
# except:
|
|
# pass
|
|
|
|
if name == 'nt':
|
|
print("Windows detected. Shutting down...")
|
|
exit()
|
|
|
|
# try:
|
|
# print("Checking for updates...")
|
|
# with ZipFile(BytesIO((urlopen('http://files.bbrunson.com/spotify-gui/update.zip')).read())) as zipObj:
|
|
# zipObj.extractall()
|
|
# print("Update successfully installed.")
|
|
# except urllib.error.HTTPError:
|
|
# print("No update available.")
|
|
# pass
|
|
|
|
def click(x, y, button, pressed, foo):
|
|
return False
|
|
|
|
bar = partial(click, foo="bar", button=True, pressed=True)
|
|
|
|
while True:
|
|
try:
|
|
urlopen('http://bbrunson.com', timeout=1)
|
|
print("Connection to server established.")
|
|
try:
|
|
if path.exists('web/update/update.zip'):
|
|
print("Update found. Installing...")
|
|
with ZipFile('web/update/update.zip') as zipObj:
|
|
zipObj.extractall()
|
|
remove('web/update/update.zip')
|
|
print("Uploaded update successfully installed.")
|
|
print("Checking for updates...")
|
|
with ZipFile(BytesIO((urlopen('http://files.bbrunson.com/spotify-gui/update.zip')).read())) as zipObj:
|
|
zipObj.extractall()
|
|
print("Update successfully downloaded and installed.")
|
|
except urllib.error.HTTPError:
|
|
print("No update available.")
|
|
pass
|
|
subprocess.Popen(['python3', 'web/web.py'])
|
|
subprocess.check_call(['python3', 'spotifycontroller.py'])
|
|
except Exception as e:
|
|
if e.args[0] == 1:
|
|
print("Program has requested sleep mode.")
|
|
with mouse.Listener(on_move=bar, on_click=bar, on_scroll=bar) as listener:
|
|
listener.join()
|
|
else:
|
|
print(e)
|
|
print("Restart procedure initiated: Checking server connection, checking for update, and then reestablishing the application in 10 seconds.")
|
|
sleep(10)
|
|
continue |