BREAKING CHANGE: migrate api to librespot internal client

This commit is contained in:
Xoconoch
2025-08-27 10:06:33 -06:00
parent 443edd9c3d
commit 2de323a75f
17 changed files with 1035 additions and 933 deletions

View File

@@ -1,32 +1,46 @@
import re
from typing import List, Dict, Any
from fastapi import APIRouter, HTTPException
from typing import List
from fastapi import APIRouter
from pydantic import BaseModel
import logging
# Assuming these imports are available for queue management and Spotify info
from routes.utils.get_info import get_spotify_info
from routes.utils.get_info import (
get_client,
get_track,
get_album,
get_playlist,
get_artist,
)
from routes.utils.celery_tasks import download_track, download_album, download_playlist
router = APIRouter()
logger = logging.getLogger(__name__)
class BulkAddLinksRequest(BaseModel):
links: List[str]
@router.post("/bulk-add-spotify-links")
async def bulk_add_spotify_links(request: BulkAddLinksRequest):
added_count = 0
failed_links = []
total_links = len(request.links)
client = get_client()
for link in request.links:
# Assuming links are pre-filtered by the frontend,
# but still handle potential errors during info retrieval or unsupported types
# Extract type and ID from the link directly using regex
match = re.match(r"https://open\.spotify\.com(?:/intl-[a-z]{2})?/(track|album|playlist|artist)/([a-zA-Z0-9]+)(?:\?.*)?", link)
match = re.match(
r"https://open\.spotify\.com(?:/intl-[a-z]{2})?/(track|album|playlist|artist)/([a-zA-Z0-9]+)(?:\?.*)?",
link,
)
if not match:
logger.warning(f"Could not parse Spotify link (unexpected format after frontend filter): {link}")
logger.warning(
f"Could not parse Spotify link (unexpected format after frontend filter): {link}"
)
failed_links.append(link)
continue
@@ -35,18 +49,30 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
try:
# Get basic info to confirm existence and get name/artist
# For playlists, we might want to get full info later when adding to queue
if spotify_type == "playlist":
item_info = get_spotify_info(spotify_id, "playlist_metadata")
item_info = get_playlist(client, spotify_id, expand_items=False)
elif spotify_type == "track":
item_info = get_track(client, spotify_id)
elif spotify_type == "album":
item_info = get_album(client, spotify_id)
elif spotify_type == "artist":
# Not queued below, but fetch to validate link and name if needed
item_info = get_artist(client, spotify_id)
else:
item_info = get_spotify_info(spotify_id, spotify_type)
logger.warning(
f"Unsupported Spotify type: {spotify_type} for link: {link}"
)
failed_links.append(link)
continue
item_name = item_info.get("name", "Unknown Name")
artist_name = ""
if spotify_type in ["track", "album"]:
artists = item_info.get("artists", [])
if artists:
artist_name = ", ".join([a.get("name", "Unknown Artist") for a in artists])
artist_name = ", ".join(
[a.get("name", "Unknown Artist") for a in artists]
)
elif spotify_type == "playlist":
owner = item_info.get("owner", {})
artist_name = owner.get("display_name", "Unknown Owner")
@@ -83,12 +109,16 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
download_type="playlist",
)
else:
logger.warning(f"Unsupported Spotify type for download: {spotify_type} for link: {link}")
logger.warning(
f"Unsupported Spotify type for download: {spotify_type} for link: {link}"
)
failed_links.append(link)
continue
added_count += 1
logger.debug(f"Added {added_count+1}/{total_links} {spotify_type} '{item_name}' ({spotify_id}) to queue.")
logger.debug(
f"Added {added_count + 1}/{total_links} {spotify_type} '{item_name}' ({spotify_id}) to queue."
)
except Exception as e:
logger.error(f"Error processing Spotify link {link}: {e}", exc_info=True)
@@ -105,4 +135,4 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
"message": message,
"count": added_count,
"failed_links": failed_links,
}
}