Skip to content

Commit

Permalink
Re-arranged modules and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Protonosgit committed Dec 8, 2023
1 parent 232a44c commit 7c3fec6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 138 deletions.
66 changes: 1 addition & 65 deletions components/shellbridge.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
import os
import subprocess
import psutil
import shutil
from PyQt6.QtCore import Qt, QThread, pyqtSignal

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = '''<script>fetch('http://localhost:1738/watchwitch/spotify/startup')</script>'''
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")
109 changes: 109 additions & 0 deletions components/statusInfo.py
Original file line number Diff line number Diff line change
@@ -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
80 changes: 17 additions & 63 deletions components/tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import configparser
import requests
import os
import winreg
import subprocess

# Initiates the Manager.ini config file

Expand Down Expand Up @@ -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

Expand All @@ -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 = '''<script>fetch('http://localhost:1738/watchwitch/spotify/startup')</script>'''
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")
16 changes: 6 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ''

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -486,7 +482,7 @@ def run(self):


# Runs the server if enabled
if not (isAddedToStartup()):
if (isAddedToStartup()):
watchwitch = WerkzeugThread()
watchwitch.start()

Expand Down

0 comments on commit 7c3fec6

Please sign in to comment.