From 7c3fec67141696001462c865a8aa2b6c02b81726 Mon Sep 17 00:00:00 2001 From: Protonos Date: Fri, 8 Dec 2023 21:39:55 +0100 Subject: [PATCH] Re-arranged modules and cleanup --- components/shellbridge.py | 66 +---------------------- components/statusInfo.py | 109 ++++++++++++++++++++++++++++++++++++++ components/tools.py | 80 ++++++---------------------- main.py | 16 +++--- 4 files changed, 133 insertions(+), 138 deletions(-) create mode 100644 components/statusInfo.py diff --git a/components/shellbridge.py b/components/shellbridge.py index bc8712f..96df1a9 100644 --- a/components/shellbridge.py +++ b/components/shellbridge.py @@ -1,7 +1,6 @@ import sys import os import subprocess -import psutil import shutil from PyQt6.QtCore import Qt, QThread, pyqtSignal @@ -69,7 +68,7 @@ def run(self): subprocess.check_output('spicetify apply -q', shell=True) self.finished_signal.emit() -# Unisnatll spicetify task +# Uninstall spicetify task class UninstallSpicetify(QThread): @@ -113,38 +112,7 @@ def run(self): print("Error while running custom command!") -# Checks if spicetify is installed by checking appdata folder -def checkInstalled(): - folder_path = os.path.join(os.path.join( - os.path.expanduser('~'), 'AppData', 'Local'), 'spicetify') - if os.path.exists(folder_path) and os.path.isdir(folder_path): - return True - else: - return False - -# Checks if spicetify is applied by checking appdata folder of spotify - - -def checkApplied(): - folder_path = os.path.join(os.path.expanduser( - '~'), 'AppData', 'Roaming/Spotify/Apps/xpui') - if os.path.exists(folder_path) and os.path.isdir(folder_path): - return True - else: - return False - -# Checks if spicetify is running using a tasksearch - - -def checkSpotifyRunning(): - for process in psutil.process_iter(attrs=['pid', 'name']): - if 'Spotify.exe' in process.info['name']: - return True - return False - # Try blocking spotify updates by changing permissions (Windows only) - - def blockSpotifyUpdate(active): if active: try: @@ -174,35 +142,3 @@ def blockSpotifyUpdate(active): except subprocess.CalledProcessError as e: print(f'Error: {e.returncode}. patcher failed.') return e.returncode - -# Checks if spotify updates are blocked !WIP! - - -def checkUpdateSupression(): - if not os.path.exists(os.path.join(os.environ['LOCALAPPDATA'], "Spotify", "Update")): - return False - else: - return True - -# Patches Spotify with WatchWitch - - -def watchwitchInjector(mode): - try: - witchpath = os.path.join(os.path.join(os.path.expanduser( - '~'), 'AppData', 'Roaming'), 'Spotify', 'Apps', 'xpui', 'index.html') - patchstring = '''''' - if mode: - with open(witchpath, 'a', encoding='utf-8') as file: - print("patching") - file.write(patchstring) - else: - with open(witchpath, 'r+', encoding='utf-8') as file: - print("unpatching") - content = file.read() - updated_content = content.replace(patchstring, '') - file.seek(0) - file.write(updated_content) - file.truncate() - except: - print("Error while patching") diff --git a/components/statusInfo.py b/components/statusInfo.py new file mode 100644 index 0000000..925f334 --- /dev/null +++ b/components/statusInfo.py @@ -0,0 +1,109 @@ +import requests +import os +import winreg +import subprocess +import psutil + +# Checks for the latest manager version + + +def managerUpdateCheck(): + try: + url = f"https://spicetifymanagerapi.netlify.app/.netlify/functions/api/latest/manager" + response = requests.get(url) + + if response.status_code == 200: + latest_release = response.json() + tag_name = latest_release["tag_name"] + if int(tag_name.replace(".", "")) > 120: + return True + else: + return False + except: + return False + +# Checks for the latest spicetify version + + +def getLatestSpicetifyRelease(): + try: + url = f"https://spicetifymanagerapi.netlify.app/.netlify/functions/api/latest/spicetifycli" + response = requests.get(url) + + if response.status_code == 200: + latest_release = response.json() + tag_name = latest_release["tag_name"] + return tag_name + else: + return '0.0.0' + except: + return '0.0.0' + + +def checkSpotifyRunning(): + for process in psutil.process_iter(attrs=['pid', 'name']): + if 'Spotify.exe' in process.info['name']: + return True + return False + + +def isAddedToStartup(): + try: + key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, + "Software\Microsoft\Windows\CurrentVersion\Run", 0, winreg.KEY_READ) + _, value, _ = winreg.QueryValueEx(key, "SpicetifyManager") + winreg.CloseKey(key) + return True + except FileNotFoundError: + return False + except Exception as e: + print("Error while checking if added to startup") + print(e) + return False + + +def checkUpdateSupression(): + if not os.path.exists(os.path.join(os.environ['LOCALAPPDATA'], "Spotify", "Update")): + return False + else: + return True + +# Checks if spicetify is installed by checking appdata folder + + +def checkInstalled(): + folder_path = os.path.join(os.path.join( + os.path.expanduser('~'), 'AppData', 'Local'), 'spicetify') + if os.path.exists(folder_path) and os.path.isdir(folder_path): + return True + else: + return False + +# Checks if spicetify is applied by checking appdata folder of spotify + + +def checkApplied(): + folder_path = os.path.join(os.path.expanduser( + '~'), 'AppData', 'Roaming/Spotify/Apps/xpui') + if os.path.exists(folder_path) and os.path.isdir(folder_path): + return True + else: + return False + + +def spicetifyStatusCheck(): + try: + LOCALSPICETIFYVER = subprocess.check_output( + 'spicetify --version', shell=True).decode("utf-8").strip() + LATESTSPICETIFYVER = getLatestSpicetifyRelease().replace("v", "").strip() + linkpath = os.path.join(os.path.join(os.path.expanduser( + '~'), 'AppData', 'Roaming'), 'Spotify', 'Apps', 'login.spa') + if os.path.exists(linkpath): + if (LOCALSPICETIFYVER == LATESTSPICETIFYVER): + return 0 + else: + return 1 + else: + return 2 + except: + return 0 diff --git a/components/tools.py b/components/tools.py index e25795b..2fe6641 100644 --- a/components/tools.py +++ b/components/tools.py @@ -1,8 +1,6 @@ import configparser -import requests import os import winreg -import subprocess # Initiates the Manager.ini config file @@ -58,40 +56,6 @@ def writeConfig(section, key, value): except: print("Error while writing config file") -# Checks for the latest spicetify version - - -def getLatestSpicetifyRelease(): - try: - url = f"https://spicetifymanagerapi.netlify.app/.netlify/functions/api/latest/spicetifycli" - response = requests.get(url) - - if response.status_code == 200: - latest_release = response.json() - tag_name = latest_release["tag_name"] - return tag_name - else: - return '0.0.0' - except: - return '0.0.0' - -# Checks if a new version of the manager is available - - -def managerUpdateCheck(): - try: - url = f"https://spicetifymanagerapi.netlify.app/.netlify/functions/api/latest/manager" - response = requests.get(url) - - if response.status_code == 200: - latest_release = response.json() - tag_name = latest_release["tag_name"] - if int(tag_name.replace(".", "")) > 120: - return True - else: - return False - except: - return False # Add exe to startup of windows /remove it again @@ -116,35 +80,25 @@ def addToStartup(mode): except: print("Error while adding to startup") - -def isAddedToStartup(): - try: - key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, - "Software\Microsoft\Windows\CurrentVersion\Run", 0, winreg.KEY_READ) - _, value, _ = winreg.QueryValueEx(key, "SpicetifyManager") - winreg.CloseKey(key) - return True - except FileNotFoundError: - return False - except Exception as e: - print("Error while checking if added to startup") - print(e) - return False +# Patches Spotify with WatchWitch -def spicetifyStatusCheck(): +def watchwitchInjector(mode): try: - LOCALSPICETIFYVER = subprocess.check_output( - 'spicetify --version', shell=True).decode("utf-8").strip() - LATESTSPICETIFYVER = getLatestSpicetifyRelease().replace("v", "").strip() - linkpath = os.path.join(os.path.join(os.path.expanduser( - '~'), 'AppData', 'Roaming'), 'Spotify', 'Apps', 'login.spa') - if os.path.exists(linkpath): - if (LOCALSPICETIFYVER == LATESTSPICETIFYVER): - return 0 - else: - return 1 + witchpath = os.path.join(os.path.join(os.path.expanduser( + '~'), 'AppData', 'Roaming'), 'Spotify', 'Apps', 'xpui', 'index.html') + patchstring = '''''' + if mode: + with open(witchpath, 'a', encoding='utf-8') as file: + print("patching") + file.write(patchstring) else: - return 2 + with open(witchpath, 'r+', encoding='utf-8') as file: + print("unpatching") + content = file.read() + updated_content = content.replace(patchstring, '') + file.seek(0) + file.write(updated_content) + file.truncate() except: - return 0 + print("Error while patching") diff --git a/main.py b/main.py index 98012df..e40ef20 100644 --- a/main.py +++ b/main.py @@ -9,8 +9,9 @@ from PyQt6.uic import loadUi from PyQt6.QtGui import QDesktopServices, QMovie, QIcon from components.popups import errorDialog, infoDialog, windowsToast, confirmationModal -from components.shellbridge import InstallSpicetify, watchwitchInjector, UpdateSpicetify, ApplySpicetify, UninstallSpicetify, CustomCommand, checkApplied, blockSpotifyUpdate, checkUpdateSupression -from components.tools import managerUpdateCheck, getLatestSpicetifyRelease, readConfig, writeConfig, initConfig, spicetifyStatusCheck, isAddedToStartup, addToStartup +from components.shellbridge import InstallSpicetify, UpdateSpicetify, ApplySpicetify, UninstallSpicetify, CustomCommand, blockSpotifyUpdate +from components.statusInfo import * +from components.tools import * from components.dialog_windows import AfterInstall from werkzeug.wrappers import Request, Response from werkzeug.serving import run_simple @@ -27,15 +28,12 @@ def __init__(self): self.isSpotifyInstalled = False self.isSpicetifyInstalled = False self.isApplied = False - self.isBackedUp = False self.isActive = False self.isMarketInstalled = False self.isWatchWitched = False self.isAutoClosing = False self.managermode = 0 - self.LOCALSPOTIFYVER = '' - self.LATESTSPOTIFYVER = '' self.LOCALSPICETIFYVER = '' self.LATESTSPICETIFYVER = '' @@ -49,6 +47,7 @@ def __init__(self): if not "--startup" in sys.argv: # if "--startup" in sys.argv: + self.InitWindow() self.show() else: # Add task tray icon which makes menu window visible on click @@ -62,9 +61,6 @@ def __init__(self): self.tray.setVisible(True) # Check if window is visible and toggle visibility - self.InitWindow() - # self.show() - self.bt_master.clicked.connect(self.masterButton) self.bt_uninstall.clicked.connect(self.startRemoval) self.bt_cmd.clicked.connect(self.Custom) @@ -463,7 +459,7 @@ def run(self): # -# Spotify WatchWitch on new thread (it's helloween) +# Start Spotify WatchWitch on new thread (it's helloween) # # Server request endpoint for watchwitch @@ -486,7 +482,7 @@ def run(self): # Runs the server if enabled -if not (isAddedToStartup()): +if (isAddedToStartup()): watchwitch = WerkzeugThread() watchwitch.start()