man, markets are hard
This commit is contained in:
@@ -2,6 +2,7 @@ from deezspot.easy_spoty import Spo
|
||||
import json
|
||||
from pathlib import Path
|
||||
import logging
|
||||
from routes.utils.credentials import get_credential, _get_global_spotify_api_creds
|
||||
|
||||
# Configure logger
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -12,48 +13,38 @@ def search(
|
||||
limit: int = 3,
|
||||
main: str = None
|
||||
) -> dict:
|
||||
logger.info(f"Search requested: query='{query}', type={search_type}, limit={limit}, main={main}")
|
||||
logger.info(f"Search requested: query='{query}', type={search_type}, limit={limit}, main_account_name={main}")
|
||||
|
||||
# If main account is specified, load client ID and secret from the account's search.json
|
||||
client_id = None
|
||||
client_secret = None
|
||||
client_id, client_secret = _get_global_spotify_api_creds()
|
||||
|
||||
if not client_id or not client_secret:
|
||||
logger.error("Global Spotify API client_id or client_secret not configured in ./data/creds/search.json.")
|
||||
raise ValueError("Spotify API credentials are not configured globally for search.")
|
||||
|
||||
if main:
|
||||
search_creds_path = Path(f'./data/creds/spotify/{main}/search.json')
|
||||
logger.debug(f"Looking for credentials at: {search_creds_path}")
|
||||
|
||||
if search_creds_path.exists():
|
||||
try:
|
||||
with open(search_creds_path, 'r') as f:
|
||||
search_creds = json.load(f)
|
||||
client_id = search_creds.get('client_id')
|
||||
client_secret = search_creds.get('client_secret')
|
||||
logger.debug(f"Credentials loaded successfully for account: {main}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error loading search credentials: {e}")
|
||||
print(f"Error loading search credentials: {e}")
|
||||
else:
|
||||
logger.warning(f"Credentials file not found at: {search_creds_path}")
|
||||
|
||||
# Initialize the Spotify client with credentials (if available)
|
||||
if client_id and client_secret:
|
||||
logger.debug("Initializing Spotify client with account credentials")
|
||||
Spo.__init__(client_id, client_secret)
|
||||
logger.debug(f"Spotify account context '{main}' was provided for search. API keys are global, but this account might be used for other context by Spo if relevant.")
|
||||
try:
|
||||
get_credential('spotify', main)
|
||||
logger.debug(f"Spotify account '{main}' exists.")
|
||||
except FileNotFoundError:
|
||||
logger.warning(f"Spotify account '{main}' provided for search context not found in credentials. Search will proceed with global API keys.")
|
||||
except Exception as e:
|
||||
logger.warning(f"Error checking existence of Spotify account '{main}': {e}. Search will proceed with global API keys.")
|
||||
else:
|
||||
logger.debug("Using default Spotify client credentials")
|
||||
logger.debug("No specific 'main' account context provided for search. Using global API keys.")
|
||||
|
||||
# Perform the Spotify search
|
||||
logger.debug(f"Executing Spotify search with query='{query}', type={search_type}")
|
||||
logger.debug(f"Initializing Spotify client with global API credentials for search.")
|
||||
Spo.__init__(client_id, client_secret)
|
||||
|
||||
logger.debug(f"Executing Spotify search with query='{query}', type={search_type}, limit={limit}")
|
||||
try:
|
||||
spotify_response = Spo.search(
|
||||
query=query,
|
||||
search_type=search_type,
|
||||
limit=limit,
|
||||
client_id=client_id,
|
||||
client_secret=client_secret
|
||||
limit=limit
|
||||
)
|
||||
logger.info(f"Search completed successfully")
|
||||
logger.info(f"Search completed successfully for query: '{query}'")
|
||||
return spotify_response
|
||||
except Exception as e:
|
||||
logger.error(f"Error during Spotify search: {e}")
|
||||
logger.error(f"Error during Spotify search for query '{query}': {e}", exc_info=True)
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user