Merge pull request #334 from Phlogi/fixup-bulk-add-celery

(fix): bulk add links correctly to celery manager
This commit is contained in:
Spotizerr
2025-08-28 06:53:07 -06:00
committed by GitHub

View File

@@ -1,10 +1,20 @@
import re import re
from typing import List from typing import List
from fastapi import APIRouter from fastapi import APIRouter, Request, Depends, Request, Depends
from pydantic import BaseModel from pydantic import BaseModel
import logging import logging
# Assuming these imports are available for queue management and Spotify info # Import authentication dependencies
from routes.auth.middleware import require_auth_from_state, User
# Import queue management and Spotify info
from routes.utils.get_info import get_spotify_info
from routes.utils.celery_queue_manager import download_queue_manager
# Import authentication dependencies
from routes.auth.middleware import require_auth_from_state, User
# Import queue management and Spotify info
from routes.utils.get_info import ( from routes.utils.get_info import (
get_client, get_client,
get_track, get_track,
@@ -12,7 +22,7 @@ from routes.utils.get_info import (
get_playlist, get_playlist,
get_artist, get_artist,
) )
from routes.utils.celery_tasks import download_track, download_album, download_playlist from routes.utils.celery_queue_manager import download_queue_manager
router = APIRouter() router = APIRouter()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -23,7 +33,7 @@ class BulkAddLinksRequest(BaseModel):
@router.post("/bulk-add-spotify-links") @router.post("/bulk-add-spotify-links")
async def bulk_add_spotify_links(request: BulkAddLinksRequest): async def bulk_add_spotify_links(request: BulkAddLinksRequest, req: Request, current_user: User = Depends(require_auth_from_state)):
added_count = 0 added_count = 0
failed_links = [] failed_links = []
total_links = len(request.links) total_links = len(request.links)
@@ -34,7 +44,7 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
# but still handle potential errors during info retrieval or unsupported types # but still handle potential errors during info retrieval or unsupported types
# Extract type and ID from the link directly using regex # Extract type and ID from the link directly using regex
match = re.match( match = re.match(
r"https://open\.spotify\.com(?:/intl-[a-z]{2})?/(track|album|playlist|artist)/([a-zA-Z0-9]+)(?:\?.*)?", r"https://open\.spotify\.com(?:/[a-z]{2})?/(track|album|playlist|artist)/([a-zA-Z0-9]+)(?:\?.*)?",
link, link,
) )
if not match: if not match:
@@ -46,6 +56,8 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
spotify_type = match.group(1) spotify_type = match.group(1)
spotify_id = match.group(2) spotify_id = match.group(2)
logger.debug(f"Extracted from link: spotify_type={spotify_type}, spotify_id={spotify_id}")
logger.debug(f"Extracted from link: spotify_type={spotify_type}, spotify_id={spotify_id}")
try: try:
# Get basic info to confirm existence and get name/artist # Get basic info to confirm existence and get name/artist
@@ -80,46 +92,29 @@ async def bulk_add_spotify_links(request: BulkAddLinksRequest):
# Construct URL for the download task # Construct URL for the download task
spotify_url = f"https://open.spotify.com/{spotify_type}/{spotify_id}" spotify_url = f"https://open.spotify.com/{spotify_type}/{spotify_id}"
# Add to Celery queue based on type # Prepare task data for the queue manager
if spotify_type == "track": task_data = {
download_track.delay( "download_type": spotify_type,
url=spotify_url, "url": spotify_url,
spotify_id=spotify_id, "name": item_name,
type=spotify_type, "artist": artist_name,
name=item_name, "spotify_id": spotify_id,
artist=artist_name, "type": spotify_type,
download_type="track", "username": current_user.username,
) "orig_request": dict(req.query_params),
elif spotify_type == "album": }
download_album.delay(
url=spotify_url, # Add to download queue using the queue manager
spotify_id=spotify_id, task_id = download_queue_manager.add_task(task_data)
type=spotify_type,
name=item_name, if task_id:
artist=artist_name, added_count += 1
download_type="album", logger.debug(f"Added {added_count}/{total_links} {spotify_type} '{item_name}' ({spotify_id}) to queue with task_id: {task_id}.")
)
elif spotify_type == "playlist":
download_playlist.delay(
url=spotify_url,
spotify_id=spotify_id,
type=spotify_type,
name=item_name,
artist=artist_name,
download_type="playlist",
)
else: else:
logger.warning( logger.warning(f"Failed to add {spotify_type} '{item_name}' ({spotify_id}) to queue.")
f"Unsupported Spotify type for download: {spotify_type} for link: {link}"
)
failed_links.append(link) failed_links.append(link)
continue 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: except Exception as e:
logger.error(f"Error processing Spotify link {link}: {e}", exc_info=True) logger.error(f"Error processing Spotify link {link}: {e}", exc_info=True)
failed_links.append(link) failed_links.append(link)