fix: minor optimizations, trying to fix #333
This commit is contained in:
@@ -233,41 +233,70 @@ async def add_to_watchlist(
|
||||
}
|
||||
|
||||
# Fetch playlist details from Spotify to populate our DB (metadata only)
|
||||
cfg = get_config_params() or {}
|
||||
active_account = cfg.get("spotify")
|
||||
if not active_account:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={"error": "Active Spotify account not set in configuration."},
|
||||
# Use shared helper and add a safe fallback for missing 'id'
|
||||
try:
|
||||
from routes.utils.get_info import get_playlist_metadata
|
||||
|
||||
playlist_data = get_playlist_metadata(playlist_spotify_id) or {}
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to fetch playlist metadata for {playlist_spotify_id}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
blob_path = get_spotify_blob_path(active_account)
|
||||
if not blob_path.exists():
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={
|
||||
"error": f"Spotify credentials blob not found for account '{active_account}'"
|
||||
"error": f"Failed to fetch metadata for playlist {playlist_spotify_id}: {str(e)}"
|
||||
},
|
||||
)
|
||||
|
||||
client = get_client()
|
||||
try:
|
||||
playlist_data = get_playlist(
|
||||
client, playlist_spotify_id, expand_items=False
|
||||
# Some Librespot responses may omit 'id' even when the payload is valid.
|
||||
# Fall back to the path parameter to avoid false negatives.
|
||||
if playlist_data and "id" not in playlist_data:
|
||||
logger.warning(
|
||||
f"Playlist metadata for {playlist_spotify_id} missing 'id'. Injecting from path param. Keys: {list(playlist_data.keys())}"
|
||||
)
|
||||
finally:
|
||||
pass
|
||||
try:
|
||||
playlist_data["id"] = playlist_spotify_id
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not playlist_data or "id" not in playlist_data:
|
||||
# Validate minimal fields needed downstream and normalize shape to be resilient to client changes
|
||||
if not playlist_data or not playlist_data.get("name"):
|
||||
logger.error(
|
||||
f"Could not fetch details for playlist {playlist_spotify_id} from Spotify."
|
||||
f"Insufficient playlist metadata for {playlist_spotify_id}. Keys present: {list(playlist_data.keys()) if isinstance(playlist_data, dict) else type(playlist_data)}"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail={
|
||||
"error": f"Could not fetch details for playlist {playlist_spotify_id} from Spotify."
|
||||
"error": f"Could not fetch sufficient details for playlist {playlist_spotify_id} from Spotify."
|
||||
},
|
||||
)
|
||||
|
||||
# Ensure 'owner' is a dict with at least id/display_name to satisfy DB layer
|
||||
owner = playlist_data.get("owner")
|
||||
if not isinstance(owner, dict):
|
||||
owner = {}
|
||||
if "id" not in owner or not owner.get("id"):
|
||||
owner["id"] = "unknown_owner"
|
||||
if "display_name" not in owner or not owner.get("display_name"):
|
||||
owner["display_name"] = owner.get("id", "Unknown Owner")
|
||||
playlist_data["owner"] = owner
|
||||
|
||||
# Ensure 'tracks' is a dict with a numeric 'total'
|
||||
tracks = playlist_data.get("tracks")
|
||||
if not isinstance(tracks, dict):
|
||||
tracks = {}
|
||||
total = tracks.get("total")
|
||||
if not isinstance(total, int):
|
||||
items = tracks.get("items")
|
||||
if isinstance(items, list):
|
||||
total = len(items)
|
||||
else:
|
||||
total = 0
|
||||
tracks["total"] = total
|
||||
playlist_data["tracks"] = tracks
|
||||
|
||||
add_playlist_db(playlist_data) # This also creates the tracks table
|
||||
|
||||
logger.info(
|
||||
|
||||
Reference in New Issue
Block a user