44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
|
|
CONFIG_PATH = Path(__file__).with_name("spotify_config.json")
|
|
DEFAULT_REDIRECT_URI = "http://127.0.0.1:4002/auth"
|
|
|
|
|
|
def loadSpotifySettings():
|
|
if not CONFIG_PATH.exists():
|
|
return {}
|
|
|
|
with CONFIG_PATH.open("r", encoding="utf-8") as file:
|
|
return json.load(file)
|
|
|
|
|
|
def saveSpotifySettings(clientId, clientSecret, redirectUri=None):
|
|
settings = {
|
|
"client_id": clientId.strip(),
|
|
"client_secret": clientSecret.strip(),
|
|
"redirect_uri": (redirectUri or DEFAULT_REDIRECT_URI).strip(),
|
|
}
|
|
|
|
with CONFIG_PATH.open("w", encoding="utf-8") as file:
|
|
json.dump(settings, file, indent=4)
|
|
file.write("\n")
|
|
|
|
|
|
def spotifySettingsReady():
|
|
settings = loadSpotifySettings()
|
|
|
|
return bool(settings.get("client_id") and settings.get("client_secret"))
|
|
|
|
|
|
def getSpotifySettings():
|
|
settings = loadSpotifySettings()
|
|
|
|
if not settings.get("client_id") or not settings.get("client_secret"):
|
|
raise RuntimeError("Spotify setup is incomplete.")
|
|
|
|
settings.setdefault("redirect_uri", DEFAULT_REDIRECT_URI)
|
|
|
|
return settings
|