Merge branch 'dev' into fixup-bulk-add-celery
This commit is contained in:
@@ -11,25 +11,42 @@ from routes.auth.middleware import require_auth_from_state, User
|
||||
from routes.utils.get_info import get_spotify_info
|
||||
from routes.utils.celery_queue_manager import download_queue_manager
|
||||
|
||||
# Assuming these imports are available for queue management and Spotify info
|
||||
from routes.utils.get_info import (
|
||||
get_client,
|
||||
get_track,
|
||||
get_album,
|
||||
get_playlist,
|
||||
get_artist,
|
||||
)
|
||||
|
||||
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, req: Request, current_user: User = Depends(require_auth_from_state)):
|
||||
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
|
||||
|
||||
@@ -39,18 +56,30 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest, req: Request, cur
|
||||
|
||||
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")
|
||||
@@ -77,8 +106,16 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest, req: Request, cur
|
||||
added_count += 1
|
||||
logger.debug(f"Added {added_count}/{total_links} {spotify_type} '{item_name}' ({spotify_id}) to queue with task_id: {task_id}.")
|
||||
else:
|
||||
logger.warning(f"Failed to add {spotify_type} '{item_name}' ({spotify_id}) to queue.")
|
||||
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."
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing Spotify link {link}: {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user