change YTM token saving file and location

This commit is contained in:
ChuckBuilds
2025-05-23 09:40:28 -05:00
parent 5d8e8c8d50
commit a20ec13f9b

View File

@@ -18,6 +18,12 @@ logging.getLogger('engineio.server').setLevel(logging.WARNING)
# Define paths relative to this file's location # Define paths relative to this file's location
CONFIG_DIR = os.path.join(os.path.dirname(__file__), '..', 'config') CONFIG_DIR = os.path.join(os.path.dirname(__file__), '..', 'config')
CONFIG_PATH = os.path.join(CONFIG_DIR, 'config.json') CONFIG_PATH = os.path.join(CONFIG_DIR, 'config.json')
# Resolve to an absolute path
CONFIG_PATH = os.path.abspath(CONFIG_PATH)
# Path for the separate YTM authentication token file
YTM_AUTH_CONFIG_PATH = os.path.join(CONFIG_DIR, 'ytm_auth.json')
YTM_AUTH_CONFIG_PATH = os.path.abspath(YTM_AUTH_CONFIG_PATH)
# YTM Companion App Constants # YTM Companion App Constants
YTM_APP_ID = "ledmatrixcontroller" YTM_APP_ID = "ledmatrixcontroller"
@@ -28,7 +34,7 @@ class YTMClient:
def __init__(self): def __init__(self):
self.base_url = None self.base_url = None
self.ytm_token = None # To store the auth token self.ytm_token = None # To store the auth token
self.load_config() self.load_config() # This will now load URL from main config and token from ytm_auth.json
# Explicitly disable internal loggers, rely on global config above # Explicitly disable internal loggers, rely on global config above
self.sio = socketio.Client(logger=False, engineio_logger=False) self.sio = socketio.Client(logger=False, engineio_logger=False)
self.last_known_track_data = None self.last_known_track_data = None
@@ -62,38 +68,47 @@ class YTMClient:
def load_config(self): def load_config(self):
default_url = "http://localhost:9863" default_url = "http://localhost:9863"
loaded_config = {} # To store the whole config for saving later loaded_config = {} # To store the whole config for saving later
# Load base_url from main config.json
if not os.path.exists(CONFIG_PATH): if not os.path.exists(CONFIG_PATH):
logging.error(f"Config file not found at {CONFIG_PATH}") logging.error(f"Main config file not found at {CONFIG_PATH}")
# We can still try to load a token if ytm_auth.json exists
# and use default URL
else:
try:
with open(CONFIG_PATH, 'r') as f:
loaded_config = json.load(f)
music_config = loaded_config.get("music", {})
self.base_url = music_config.get("YTM_COMPANION_URL", default_url)
if not self.base_url:
logging.warning("YTM_COMPANION_URL missing or empty in config.json music section, using default.")
self.base_url = default_url
except json.JSONDecodeError:
logging.error(f"Error decoding JSON from main config {CONFIG_PATH}")
except Exception as e:
logging.error(f"Error loading YTM_COMPANION_URL from main config {CONFIG_PATH}: {e}")
if not self.base_url: # If main config was missing or URL not found
self.base_url = default_url self.base_url = default_url
self.ytm_token = None
logging.warning(f"Using default YTM URL: {self.base_url}") logging.warning(f"Using default YTM URL: {self.base_url}")
# No config to save, so just return
return
try: # Load ytm_token from ytm_auth.json
with open(CONFIG_PATH, 'r') as f: self.ytm_token = None # Reset token before trying to load
loaded_config = json.load(f) # Load the entire config if os.path.exists(YTM_AUTH_CONFIG_PATH):
music_config = loaded_config.get("music", {}) try:
self.base_url = music_config.get("YTM_COMPANION_URL", default_url) with open(YTM_AUTH_CONFIG_PATH, 'r') as f:
self.ytm_token = music_config.get("YTM_COMPANION_TOKEN") # Load the token auth_data = json.load(f)
self.ytm_token = auth_data.get("YTM_COMPANION_TOKEN")
if not self.base_url: except json.JSONDecodeError:
logging.warning("YTM_COMPANION_URL missing or empty in config.json music section, using default.") logging.error(f"Error decoding JSON from YTM auth file {YTM_AUTH_CONFIG_PATH}")
self.base_url = default_url except Exception as e:
except json.JSONDecodeError: logging.error(f"Error loading YTM auth config {YTM_AUTH_CONFIG_PATH}: {e}")
logging.error(f"Error decoding JSON from {CONFIG_PATH}")
self.base_url = default_url
self.ytm_token = None
except Exception as e:
logging.error(f"Error loading YTM config: {e}")
self.base_url = default_url
self.ytm_token = None
logging.info(f"YTM Companion URL set to: {self.base_url}") logging.info(f"YTM Companion URL set to: {self.base_url}")
if self.ytm_token: if self.ytm_token:
logging.info("YTM Companion token loaded from config.") logging.info(f"YTM Companion token loaded from {YTM_AUTH_CONFIG_PATH}.")
else: else:
logging.info("No YTM Companion token found in config. Will attempt to register.") logging.info(f"No YTM Companion token found in {YTM_AUTH_CONFIG_PATH}. Will attempt to register.")
if self.base_url and self.base_url.startswith("ws://"): if self.base_url and self.base_url.startswith("ws://"):
self.base_url = "http://" + self.base_url[5:] self.base_url = "http://" + self.base_url[5:]
@@ -101,33 +116,30 @@ class YTMClient:
self.base_url = "https://" + self.base_url[6:] self.base_url = "https://" + self.base_url[6:]
# Store the loaded config for potential saving later # Store the loaded config for potential saving later
self._loaded_config_data = loaded_config self._loaded_config_data = loaded_config # Still keep main config data if needed elsewhere, but not for token saving
def _save_config(self): def _save_ytm_token(self):
"""Saves the current configuration, including the YTM token, back to config.json.""" """Saves the YTM token to ytm_auth.json."""
if not hasattr(self, '_loaded_config_data') or not self._loaded_config_data: if not self.ytm_token:
logging.warning("No configuration data loaded, cannot save token.") logging.warning("No YTM token to save.")
# If config.json didn't exist or was empty, we might need to create it. return
# For now, let's assume it exists if we're trying to save a token.
# A more robust approach would be to create/update the structure.
if not os.path.exists(CONFIG_DIR):
try:
os.makedirs(CONFIG_DIR)
except OSError as e:
logging.error(f"Could not create config directory {CONFIG_DIR}: {e}")
return
self._loaded_config_data = {"music": {}} # Initialize if totally empty
self._loaded_config_data.setdefault("music", {}) # Ensure music section exists if not os.path.exists(CONFIG_DIR):
self._loaded_config_data["music"]["YTM_COMPANION_URL"] = self.base_url # Save current base_url too try:
self._loaded_config_data["music"]["YTM_COMPANION_TOKEN"] = self.ytm_token os.makedirs(CONFIG_DIR)
logging.info(f"Created config directory: {CONFIG_DIR}")
except OSError as e:
logging.error(f"Could not create config directory {CONFIG_DIR}: {e}")
return
token_data = {"YTM_COMPANION_TOKEN": self.ytm_token}
try: try:
with open(CONFIG_PATH, 'w') as f: with open(YTM_AUTH_CONFIG_PATH, 'w') as f:
json.dump(self._loaded_config_data, f, indent=4) json.dump(token_data, f, indent=4)
logging.info(f"YTM Companion token saved to {CONFIG_PATH}") logging.info(f"YTM Companion token saved to {YTM_AUTH_CONFIG_PATH}")
except Exception as e: except Exception as e:
logging.error(f"Error saving YTM config with token: {e}") logging.error(f"Error saving YTM token to {YTM_AUTH_CONFIG_PATH}: {e}")
def _request_auth_code(self): def _request_auth_code(self):
"""Requests an authentication code from the YTM Companion server.""" """Requests an authentication code from the YTM Companion server."""
@@ -198,7 +210,7 @@ class YTMClient:
token = self._request_auth_token(code) token = self._request_auth_token(code)
if token: if token:
self.ytm_token = token self.ytm_token = token
self._save_config() # Save the new token self._save_ytm_token() # Save the new token to ytm_auth.json
return True return True
else: else:
logging.error("Failed to get YTM auth token.") logging.error("Failed to get YTM auth token.")