diff --git a/.env.example b/.env.example
index 8d4b3a5..50a5425 100644
--- a/.env.example
+++ b/.env.example
@@ -11,6 +11,7 @@
HOST=0.0.0.0
# Redis connection (external or internal).
+# Host name 'redis' works with docker-compose.yml setup
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=0
@@ -57,3 +58,8 @@ GOOGLE_CLIENT_SECRET=
# GitHub SSO (get from GitHub Developer Settings)
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
+
+# Log level for application logging.
+# Possible values: debug, info, warning, error, critical
+# Set to 'info' or 'warning' for general use. Use 'debug' for troubleshooting.
+LOG_LEVEL=info
diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml
new file mode 100644
index 0000000..f4cea3a
--- /dev/null
+++ b/.github/workflows/pr-build.yml
@@ -0,0 +1,60 @@
+name: PR Dev/Test Container
+
+on:
+ pull_request:
+ types: [opened, synchronize, reopened]
+ workflow_dispatch:
+ inputs:
+ pr_number:
+ description: 'Pull request number (optional, for manual runs)'
+ required: false
+ branch:
+ description: 'Branch to build (optional, defaults to PR head or main)'
+ required: false
+
+env:
+ REGISTRY: ghcr.io
+ IMAGE_NAME: ${{ github.repository }}
+
+jobs:
+ build-and-push:
+ runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ packages: write
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v4
+ with:
+ ref: ${{ github.event.inputs.branch || github.head_ref || github.ref }}
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v2
+
+ - name: Login to GHCR
+ uses: docker/login-action@v2
+ with:
+ registry: ${{ env.REGISTRY }}
+ username: ${{ github.actor }}
+ password: ${{ secrets.GITHUB_TOKEN }}
+
+ # Extract Docker metadata
+ - name: Extract Docker metadata
+ id: meta
+ uses: docker/metadata-action@v4
+ with:
+ images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
+ tags: |
+ type=raw,value=dev-pr-${{ github.event.inputs.pr_number || github.event.pull_request.number }}
+
+ # Build and push multi-arch dev image
+ - name: Build and push
+ uses: docker/build-push-action@v4
+ with:
+ context: .
+ platforms: linux/amd64,linux/arm64
+ push: true
+ tags: ${{ steps.meta.outputs.tags }}
+ labels: ${{ steps.meta.outputs.labels }}
+ cache-from: type=gha
+ cache-to: type=gha,mode=max
diff --git a/README.md b/README.md
index 6a980a6..80c7030 100644
--- a/README.md
+++ b/README.md
@@ -64,7 +64,7 @@ Access logs via Docker:
docker logs spotizerr
```
-**Log Locations:**
+**Log and File Locations:**
- Application Logs: `docker logs spotizerr` (main app and Celery workers)
- Individual Task Logs: `./logs/tasks/` (inside container, maps to your volume)
- Credentials: `./data/creds/`
@@ -74,6 +74,12 @@ docker logs spotizerr
- Download History Database: `./data/history/`
- Spotify Token Cache: `./.cache/` (if `SPOTIPY_CACHE_PATH` is mapped)
+**Global Logging Level:**
+The application's global logging level can be controlled via the `LOG_LEVEL` environment variable.
+Supported values (case-insensitive): `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`, `NOTSET`.
+If not set, the default logging level is `WARNING`.
+Example in `.env` file: `LOG_LEVEL=DEBUG`
+
## 🤝 Contributing
1. Fork the repository
@@ -81,6 +87,21 @@ docker logs spotizerr
3. Make your changes
4. Submit a pull request
+Here is the text to add to your `README.md` file, preferably after the "Quick Start" section:
+
+## 💻 Development Setup
+
+To run Spotizerr in development mode:
+
+1. **Backend (API):**
+ * Ensure Python dependencies are installed (e.g., using `uv pip install -r requirements.txt`).
+ * Start a Redis server.
+ * Run the app insidie your activated virtual env: `python3 app.py`
+2. **Frontend (UI):**
+ * Navigate to `spotizerr-ui/`.
+ * Install dependencies: `pnpm install`.
+ * Start the development server: `pnpm dev`.
+
## 📄 License
This project is licensed under the GPL yada yada, see [LICENSE](LICENSE) file for details.
diff --git a/app.py b/app.py
index c2449b4..511b344 100755
--- a/app.py
+++ b/app.py
@@ -12,6 +12,35 @@ import sys
import redis
import socket
from urllib.parse import urlparse
+from dotenv import load_dotenv
+
+load_dotenv()
+
+# Parse log level from environment as early as possible, default to INFO for visibility
+log_level_str = os.getenv("LOG_LEVEL", "WARNING").upper()
+log_level = getattr(logging, log_level_str, logging.WARNING)
+
+# Set up a very basic logging config immediately, so early logs (including import/migration errors) are visible
+logging.basicConfig(
+ level=log_level,
+ format="%(asctime)s [%(levelname)s] %(message)s",
+ datefmt="%Y-%m-%d %H:%M:%S",
+ stream=sys.stderr,
+)
+
+# Run DB migrations as early as possible, before importing any routers that may touch DBs
+try:
+ from routes.migrations import run_migrations_if_needed
+
+ run_migrations_if_needed()
+ logging.getLogger(__name__).info(
+ "Database migrations executed (if needed) early in startup."
+ )
+except Exception as e:
+ logging.getLogger(__name__).error(
+ f"Database migration step failed early in startup: {e}", exc_info=True
+ )
+ sys.exit(1)
# Apply process umask from environment as early as possible
_umask_value = os.getenv("UMASK")
@@ -23,6 +52,19 @@ if _umask_value:
pass
# Import and initialize routes (this will start the watch manager)
+from routes.auth.credentials import router as credentials_router # noqa: E402
+from routes.auth.auth import router as auth_router # noqa: E402
+from routes.content.album import router as album_router # noqa: E402
+from routes.content.artist import router as artist_router # noqa: E402
+from routes.content.track import router as track_router # noqa: E402
+from routes.content.playlist import router as playlist_router # noqa: E402
+from routes.content.bulk_add import router as bulk_add_router # noqa: E402
+from routes.core.search import router as search_router # noqa: E402
+from routes.core.history import router as history_router # noqa: E402
+from routes.system.progress import router as prgs_router # noqa: E402
+from routes.system.config import router as config_router # noqa: E402
+
+from routes.utils.celery_config import REDIS_URL # noqa: E402
# Configure application-wide logging
@@ -48,7 +90,7 @@ def setup_logging():
# Configure root logger
root_logger = logging.getLogger()
- root_logger.setLevel(logging.DEBUG)
+ root_logger.setLevel(log_level)
# Clear any existing handlers from the root logger
if root_logger.hasHandlers():
@@ -65,12 +107,12 @@ def setup_logging():
main_log, maxBytes=10 * 1024 * 1024, backupCount=5, encoding="utf-8"
)
file_handler.setFormatter(log_format)
- file_handler.setLevel(logging.INFO)
+ file_handler.setLevel(log_level)
# Console handler for stderr
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setFormatter(log_format)
- console_handler.setLevel(logging.INFO)
+ console_handler.setLevel(log_level)
# Add handlers to root logger
root_logger.addHandler(file_handler)
@@ -83,17 +125,22 @@ def setup_logging():
"routes.utils.celery_manager",
"routes.utils.celery_tasks",
"routes.utils.watch",
+ "uvicorn", # General Uvicorn logger
+ "uvicorn.access", # Uvicorn access logs
+ "uvicorn.error", # Uvicorn error logs
+ "spotizerr",
]:
logger = logging.getLogger(logger_name)
- logger.setLevel(logging.INFO)
- logger.propagate = True # Propagate to root logger
+ logger.setLevel(log_level)
+ # For uvicorn.access, we explicitly set propagate to False to prevent duplicate logging
+ # if access_log=False is used in uvicorn.run, and to ensure our middleware handles it.
+ logger.propagate = False if logger_name == "uvicorn.access" else True
- logging.info("Logging system initialized")
+ logger.info("Logging system initialized")
def check_redis_connection():
"""Check if Redis is available and accessible"""
- from routes.utils.celery_config import REDIS_URL
if not REDIS_URL:
logging.error("REDIS_URL is not configured. Please check your environment.")
@@ -139,6 +186,10 @@ async def lifespan(app: FastAPI):
"""Handle application startup and shutdown"""
# Startup
setup_logging()
+ effective_level = logging.getLevelName(log_level)
+ logging.getLogger(__name__).info(
+ f"Logging system fully initialized (lifespan startup). Effective log level: {effective_level}"
+ )
# Run migrations before initializing services
try:
@@ -165,8 +216,19 @@ async def lifespan(app: FastAPI):
try:
from routes.utils.celery_manager import celery_manager
- celery_manager.start()
- logging.info("Celery workers started successfully")
+ start_workers = os.getenv("START_EMBEDDED_WORKERS", "true").lower() in (
+ "1",
+ "true",
+ "yes",
+ "on",
+ )
+ if start_workers:
+ celery_manager.start()
+ logging.info("Celery workers started successfully")
+ else:
+ logging.info(
+ "START_EMBEDDED_WORKERS is false; skipping embedded Celery workers startup."
+ )
except Exception as e:
logging.error(f"Failed to start Celery workers: {e}")
@@ -196,8 +258,19 @@ async def lifespan(app: FastAPI):
try:
from routes.utils.celery_manager import celery_manager
- celery_manager.stop()
- logging.info("Celery workers stopped")
+ start_workers = os.getenv("START_EMBEDDED_WORKERS", "true").lower() in (
+ "1",
+ "true",
+ "yes",
+ "on",
+ )
+ if start_workers:
+ celery_manager.stop()
+ logging.info("Celery workers stopped")
+ else:
+ logging.info(
+ "START_EMBEDDED_WORKERS is false; no embedded Celery workers to stop."
+ )
except Exception as e:
logging.error(f"Error stopping Celery workers: {e}")
@@ -234,16 +307,6 @@ def create_app():
logging.warning(f"Auth system initialization failed or unavailable: {e}")
# Register routers with URL prefixes
- from routes.auth.auth import router as auth_router
- from routes.system.config import router as config_router
- from routes.core.search import router as search_router
- from routes.auth.credentials import router as credentials_router
- from routes.content.album import router as album_router
- from routes.content.track import router as track_router
- from routes.content.playlist import router as playlist_router
- from routes.content.artist import router as artist_router
- from routes.system.progress import router as prgs_router
- from routes.core.history import router as history_router
app.include_router(auth_router, prefix="/api/auth", tags=["auth"])
@@ -263,6 +326,7 @@ def create_app():
app.include_router(album_router, prefix="/api/album", tags=["album"])
app.include_router(track_router, prefix="/api/track", tags=["track"])
app.include_router(playlist_router, prefix="/api/playlist", tags=["playlist"])
+ app.include_router(bulk_add_router, prefix="/api/bulk", tags=["bulk"])
app.include_router(artist_router, prefix="/api/artist", tags=["artist"])
app.include_router(prgs_router, prefix="/api/prgs", tags=["progress"])
app.include_router(history_router, prefix="/api/history", tags=["history"])
@@ -386,4 +450,6 @@ if __name__ == "__main__":
except ValueError:
port = 7171
- uvicorn.run(app, host=host, port=port, log_level="info", access_log=True)
+ uvicorn.run(
+ app, host=host, port=port, log_level=log_level_str.lower(), access_log=False
+ )
diff --git a/docs/user/configuration.md b/docs/user/configuration.md
index 4d71ed5..3e24681 100644
--- a/docs/user/configuration.md
+++ b/docs/user/configuration.md
@@ -4,41 +4,63 @@ See also: [Environment variables](environment.md)
Open Configuration in the web UI. Tabs:
-- General (admin)
- - App version, basic info
-- Downloads (admin)
- - Concurrent downloads, retry behavior
- - Quality/format defaults and conversion
- - Real-time mode: aligns download time with track length
-- Formatting (admin)
- - File/folder naming patterns (examples)
- - `%artist%/%album%/%tracknum%. %title%`
- - `%ar_album%/%album% (%year%)/%title%`
-- Accounts (admin)
- - Spotify: use `spotizerr-auth` to add credentials
- - Deezer ARL (optional):
- - Chrome/Edge: DevTools → Application → Cookies → https://www.deezer.com → copy `arl`
- - Firefox: DevTools → Storage → Cookies → https://www.deezer.com → copy `arl`
- - Paste ARL in Accounts
- - Select main account when multiple exist
-- Watch (admin)
- - Enable/disable watch system
- - Set check intervals
- - Manually trigger checks (artists/playlists)
-- Server (admin)
+# General
+ - **Default service:** Right now, the only one available is Spotify. Deezer-only mode coming soon!
+ - **Active accounts:** Accounts to use for API-related things with the respective service.
+
+# Downloads
+ - **Max Concurrent Downloads:** Sets the maximum number of download tasks that can run simultaneously.
+ - **Real-Time Downloading:** Matches the download duration to the actual track length, helping to avoid rate limits.
+ - **Real-Time Multiplier:** When real-time downloading is enabled, this multiplier adjusts how much faster (or slower) the download occurs compared to the track length.
+ - **Download Fallback:** Download from Deezer with a fallback to Spotify.
+ - **Recursive Quality:** When download fallback is enabled, try with lower qualities if the specified Deezer quality is not available.
+ - **Separate Tracks by User:** When multi-user mode is enabled, separate every download in individual users' folders.
+ - **Spotify/Deezer Quality:** Quality to request to the service being used to download (account tier limitations apply).
+ - **Convert to Format:** Format to convert every file downloading.
+ - **Bitrate:** When convertion is enabled and a lossy format is enabled, this sets the bitrate with which perform the transcoding.
+ - **Max Retry Attempts:** Maximum number of automatic retries to perform
+ - **Initial Retry Delay:** Seconds between the first failure and the first retry.
+ - **Retry Delay Increase:** Seconds to increase to the delay beyween retries after each failure.
+
+
+# Formatting
+- **Custom Directory Format:** Choose which metadata fields determine how directories are named.
+- **Custom Track Format:** Choose which metadata fields determine how individual track files are named.
+- **Track Number Padding:** Enable or disable leading zeros for number-based metadata (e.g., `%tracknum%`, `%playlistnum%`).
+- **Track Number Padding Width:** Sets how many digits to use for padded numbers. For example:
+
+ * `01. Track` (width: 2)
+ * `001. Track` (width: 3)
+- **Artist Separator:** When a track has multiple artists (or album artists), this string will be used to separate them in both metadata and file/directory naming.
+- **Save Album Cover:** Whether to save the cover as a separate `cover.jpg` file or not.
+- **Use Spotify Metadata in Deezer Fallback:** Whether to use Spotify metadata when downloading from Deezer or not. It generally is better to leave this enabled, since it has no added API cost and Spotify's metadata tends to be better.
+
+# Accounts
+ - **Spotify:** use `spotizerr-auth` to add credentials.
+ - **Deezer ARL (optional but recommended):**
+ - Chrome/Edge: DevTools → Application → Cookies → https://www.deezer.com → copy `arl`.
+ - Firefox: DevTools → Storage → Cookies → https://www.deezer.com → copy `arl`.
+ - Paste ARL in Accounts.
+ - Select main account when multiple exist.
+
+# Watch
+ - Enable/disable watch system.
+ - Set check intervals.
+ - Set check chunk size.
+ - Set album groups to consider for watched artists.
+
+
+# Server
- System info and advanced settings
-- Profile (all users when auth is enabled)
+
+# Profile
- Change password, view role and email
-Quality formats (reference):
+# Quality formats (reference)
- Spotify: OGG 96k/160k/320k (320k requires Premium)
- Deezer: MP3 128k/320k (320k may require Premium), FLAC (Premium)
- Conversion: MP3/FLAC/AAC/OGG/OPUS/WAV/ALAC with custom bitrate
-Fallback system:
-- Configure primary and fallback services
-- Automatically switches if primary fails (useful for geo/account limits)
-
-Notes:
+# Notes
- Explicit content filter applies in pages (e.g., hides explicit tracks on album/playlist views)
- Watch system must be enabled before adding items
\ No newline at end of file
diff --git a/docs/user/environment.md b/docs/user/environment.md
index 7d0df4c..1d913f1 100644
--- a/docs/user/environment.md
+++ b/docs/user/environment.md
@@ -30,6 +30,24 @@ Location: project `.env`. Minimal reference for server admins.
- FRONTEND_URL: Public UI base (e.g. `http://127.0.0.1:7171`)
- GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET
- GITHUB_CLIENT_ID / GITHUB_CLIENT_SECRET
+- Custom/Generic OAuth (set all to enable a custom provider):
+ - CUSTOM_SSO_CLIENT_ID / CUSTOM_SSO_CLIENT_SECRET
+ - CUSTOM_SSO_AUTHORIZATION_ENDPOINT
+ - CUSTOM_SSO_TOKEN_ENDPOINT
+ - CUSTOM_SSO_USERINFO_ENDPOINT
+ - CUSTOM_SSO_SCOPE: Comma-separated scopes (optional)
+ - CUSTOM_SSO_NAME: Internal provider name (optional, default `custom`)
+ - CUSTOM_SSO_DISPLAY_NAME: UI name (optional, default `Custom`)
+- Multiple Custom/Generic OAuth providers (up to 10):
+ - For provider index `i` (1..10), set:
+ - CUSTOM_SSO_CLIENT_ID_i / CUSTOM_SSO_CLIENT_SECRET_i
+ - CUSTOM_SSO_AUTHORIZATION_ENDPOINT_i
+ - CUSTOM_SSO_TOKEN_ENDPOINT_i
+ - CUSTOM_SSO_USERINFO_ENDPOINT_i
+ - CUSTOM_SSO_SCOPE_i (optional)
+ - CUSTOM_SSO_NAME_i (optional, default `custom{i}`)
+ - CUSTOM_SSO_DISPLAY_NAME_i (optional, default `Custom {i}`)
+ - Login URLs will be `/api/auth/sso/login/custom/i` and callback `/api/auth/sso/callback/custom/i`.
### Tips
- If running behind a reverse proxy, set `FRONTEND_URL` and `SSO_BASE_REDIRECT_URI` to public URLs.
diff --git a/log.txt b/log.txt
new file mode 100644
index 0000000..e69de29
diff --git a/requirements.txt b/requirements.txt
index 2ea3f67..d3c468d 100755
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,7 +1,7 @@
fastapi==0.116.1
uvicorn[standard]==0.35.0
celery==5.5.3
-deezspot-spotizerr==2.7.6
+deezspot-spotizerr==3.1.5
httpx==0.28.1
bcrypt==4.2.1
PyJWT==2.10.1
diff --git a/routes/__init__.py b/routes/__init__.py
index 2fa27c9..eea436a 100755
--- a/routes/__init__.py
+++ b/routes/__init__.py
@@ -1,7 +1,3 @@
import logging
-# Configure basic logging for the application if not already configured
-# This remains safe to execute on import
-logging.basicConfig(level=logging.INFO, format="%(message)s")
-
logger = logging.getLogger(__name__)
diff --git a/routes/auth/auth.py b/routes/auth/auth.py
index fa41290..c29fb77 100644
--- a/routes/auth/auth.py
+++ b/routes/auth/auth.py
@@ -1,4 +1,4 @@
-from fastapi import APIRouter, HTTPException, Depends, Request
+from fastapi import APIRouter, HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
from typing import Optional, List
@@ -14,6 +14,7 @@ security = HTTPBearer(auto_error=False)
# Include SSO sub-router
try:
from .sso import router as sso_router
+
router.include_router(sso_router, tags=["sso"])
logging.info("SSO sub-router included in auth router")
except ImportError as e:
@@ -34,6 +35,7 @@ class RegisterRequest(BaseModel):
class CreateUserRequest(BaseModel):
"""Admin-only request to create users when registration is disabled"""
+
username: str
password: str
email: Optional[str] = None
@@ -42,17 +44,20 @@ class CreateUserRequest(BaseModel):
class RoleUpdateRequest(BaseModel):
"""Request to update user role"""
+
role: str
class PasswordChangeRequest(BaseModel):
"""Request to change user password"""
+
current_password: str
new_password: str
class AdminPasswordResetRequest(BaseModel):
"""Request for admin to reset user password"""
+
new_password: str
@@ -87,20 +92,20 @@ class AuthStatusResponse(BaseModel):
# Dependency to get current user
async def get_current_user(
- credentials: HTTPAuthorizationCredentials = Depends(security)
+ credentials: HTTPAuthorizationCredentials = Depends(security),
) -> Optional[User]:
"""Get current user from JWT token"""
if not AUTH_ENABLED:
# When auth is disabled, return a mock admin user
return User(username="system", role="admin")
-
+
if not credentials:
return None
-
+
payload = token_manager.verify_token(credentials.credentials)
if not payload:
return None
-
+
user = user_manager.get_user(payload["username"])
return user
@@ -109,25 +114,22 @@ async def require_auth(current_user: User = Depends(get_current_user)) -> User:
"""Require authentication - raises HTTPException if not authenticated"""
if not AUTH_ENABLED:
return User(username="system", role="admin")
-
+
if not current_user:
raise HTTPException(
status_code=401,
detail="Authentication required",
headers={"WWW-Authenticate": "Bearer"},
)
-
+
return current_user
async def require_admin(current_user: User = Depends(require_auth)) -> User:
"""Require admin role - raises HTTPException if not admin"""
if current_user.role != "admin":
- raise HTTPException(
- status_code=403,
- detail="Admin access required"
- )
-
+ raise HTTPException(status_code=403, detail="Admin access required")
+
return current_user
@@ -138,24 +140,33 @@ async def auth_status(current_user: Optional[User] = Depends(get_current_user)):
# Check if SSO is enabled and get available providers
sso_enabled = False
sso_providers = []
-
+
try:
from . import sso
+
sso_enabled = sso.SSO_ENABLED and AUTH_ENABLED
if sso.google_sso:
sso_providers.append("google")
if sso.github_sso:
sso_providers.append("github")
+ if getattr(sso, "custom_sso", None):
+ sso_providers.append("custom")
+ if getattr(sso, "custom_sso_providers", None):
+ if (
+ len(getattr(sso, "custom_sso_providers", {})) > 0
+ and "custom" not in sso_providers
+ ):
+ sso_providers.append("custom")
except ImportError:
pass # SSO module not available
-
+
return AuthStatusResponse(
auth_enabled=AUTH_ENABLED,
authenticated=current_user is not None,
user=UserResponse(**current_user.to_public_dict()) if current_user else None,
registration_enabled=AUTH_ENABLED and not DISABLE_REGISTRATION,
sso_enabled=sso_enabled,
- sso_providers=sso_providers
+ sso_providers=sso_providers,
)
@@ -163,23 +174,16 @@ async def auth_status(current_user: Optional[User] = Depends(get_current_user)):
async def login(request: LoginRequest):
"""Authenticate user and return access token"""
if not AUTH_ENABLED:
- raise HTTPException(
- status_code=400,
- detail="Authentication is disabled"
- )
-
+ raise HTTPException(status_code=400, detail="Authentication is disabled")
+
user = user_manager.authenticate_user(request.username, request.password)
if not user:
- raise HTTPException(
- status_code=401,
- detail="Invalid username or password"
- )
-
+ raise HTTPException(status_code=401, detail="Invalid username or password")
+
access_token = token_manager.create_token(user)
-
+
return LoginResponse(
- access_token=access_token,
- user=UserResponse(**user.to_public_dict())
+ access_token=access_token, user=UserResponse(**user.to_public_dict())
)
@@ -187,31 +191,28 @@ async def login(request: LoginRequest):
async def register(request: RegisterRequest):
"""Register a new user"""
if not AUTH_ENABLED:
- raise HTTPException(
- status_code=400,
- detail="Authentication is disabled"
- )
-
+ raise HTTPException(status_code=400, detail="Authentication is disabled")
+
if DISABLE_REGISTRATION:
raise HTTPException(
status_code=403,
- detail="Public registration is disabled. Contact an administrator to create an account."
+ detail="Public registration is disabled. Contact an administrator to create an account.",
)
-
+
# Check if this is the first user (should be admin)
existing_users = user_manager.list_users()
role = "admin" if len(existing_users) == 0 else "user"
-
+
success, message = user_manager.create_user(
username=request.username,
password=request.password,
email=request.email,
- role=role
+ role=role,
)
-
+
if not success:
raise HTTPException(status_code=400, detail=message)
-
+
return MessageResponse(message=message)
@@ -233,70 +234,57 @@ async def list_users(current_user: User = Depends(require_admin)):
async def delete_user(username: str, current_user: User = Depends(require_admin)):
"""Delete a user (admin only)"""
if username == current_user.username:
- raise HTTPException(
- status_code=400,
- detail="Cannot delete your own account"
- )
-
+ raise HTTPException(status_code=400, detail="Cannot delete your own account")
+
success, message = user_manager.delete_user(username)
if not success:
raise HTTPException(status_code=404, detail=message)
-
+
return MessageResponse(message=message)
@router.put("/users/{username}/role", response_model=MessageResponse)
async def update_user_role(
- username: str,
- request: RoleUpdateRequest,
- current_user: User = Depends(require_admin)
+ username: str,
+ request: RoleUpdateRequest,
+ current_user: User = Depends(require_admin),
):
"""Update user role (admin only)"""
if request.role not in ["user", "admin"]:
- raise HTTPException(
- status_code=400,
- detail="Role must be 'user' or 'admin'"
- )
-
+ raise HTTPException(status_code=400, detail="Role must be 'user' or 'admin'")
+
if username == current_user.username:
- raise HTTPException(
- status_code=400,
- detail="Cannot change your own role"
- )
-
+ raise HTTPException(status_code=400, detail="Cannot change your own role")
+
success, message = user_manager.update_user_role(username, request.role)
if not success:
raise HTTPException(status_code=404, detail=message)
-
+
return MessageResponse(message=message)
@router.post("/users/create", response_model=MessageResponse)
-async def create_user_admin(request: CreateUserRequest, current_user: User = Depends(require_admin)):
+async def create_user_admin(
+ request: CreateUserRequest, current_user: User = Depends(require_admin)
+):
"""Create a new user (admin only) - for use when registration is disabled"""
if not AUTH_ENABLED:
- raise HTTPException(
- status_code=400,
- detail="Authentication is disabled"
- )
-
+ raise HTTPException(status_code=400, detail="Authentication is disabled")
+
# Validate role
if request.role not in ["user", "admin"]:
- raise HTTPException(
- status_code=400,
- detail="Role must be 'user' or 'admin'"
- )
-
+ raise HTTPException(status_code=400, detail="Role must be 'user' or 'admin'")
+
success, message = user_manager.create_user(
username=request.username,
password=request.password,
email=request.email,
- role=request.role
+ role=request.role,
)
-
+
if not success:
raise HTTPException(status_code=400, detail=message)
-
+
return MessageResponse(message=message)
@@ -309,22 +297,18 @@ async def get_profile(current_user: User = Depends(require_auth)):
@router.put("/profile/password", response_model=MessageResponse)
async def change_password(
- request: PasswordChangeRequest,
- current_user: User = Depends(require_auth)
+ request: PasswordChangeRequest, current_user: User = Depends(require_auth)
):
"""Change current user's password"""
if not AUTH_ENABLED:
- raise HTTPException(
- status_code=400,
- detail="Authentication is disabled"
- )
-
+ raise HTTPException(status_code=400, detail="Authentication is disabled")
+
success, message = user_manager.change_password(
username=current_user.username,
current_password=request.current_password,
- new_password=request.new_password
+ new_password=request.new_password,
)
-
+
if not success:
# Determine appropriate HTTP status code based on error message
if "Current password is incorrect" in message:
@@ -333,9 +317,9 @@ async def change_password(
status_code = 404
else:
status_code = 400
-
+
raise HTTPException(status_code=status_code, detail=message)
-
+
return MessageResponse(message=message)
@@ -343,30 +327,26 @@ async def change_password(
async def admin_reset_password(
username: str,
request: AdminPasswordResetRequest,
- current_user: User = Depends(require_admin)
+ current_user: User = Depends(require_admin),
):
"""Admin reset user password (admin only)"""
if not AUTH_ENABLED:
- raise HTTPException(
- status_code=400,
- detail="Authentication is disabled"
- )
-
+ raise HTTPException(status_code=400, detail="Authentication is disabled")
+
success, message = user_manager.admin_reset_password(
- username=username,
- new_password=request.new_password
+ username=username, new_password=request.new_password
)
-
+
if not success:
# Determine appropriate HTTP status code based on error message
if "User not found" in message:
status_code = 404
else:
status_code = 400
-
+
raise HTTPException(status_code=status_code, detail=message)
-
+
return MessageResponse(message=message)
-# Note: SSO routes are included in the main app, not here to avoid circular imports
\ No newline at end of file
+# Note: SSO routes are included in the main app, not here to avoid circular imports
diff --git a/routes/auth/sso.py b/routes/auth/sso.py
index f7ae7e5..f5ad728 100644
--- a/routes/auth/sso.py
+++ b/routes/auth/sso.py
@@ -1,17 +1,19 @@
"""
SSO (Single Sign-On) implementation for Google and GitHub authentication
"""
+
import os
import logging
from typing import Optional, Dict, Any
from datetime import datetime, timedelta
-from fastapi import APIRouter, Request, HTTPException, Depends
+from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import RedirectResponse
from fastapi_sso.sso.google import GoogleSSO
from fastapi_sso.sso.github import GithubSSO
from fastapi_sso.sso.base import OpenID
from pydantic import BaseModel
+from fastapi_sso.sso.generic import create_provider
from . import user_manager, token_manager, User, AUTH_ENABLED, DISABLE_REGISTRATION
@@ -25,11 +27,14 @@ GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID")
GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET")
-SSO_BASE_REDIRECT_URI = os.getenv("SSO_BASE_REDIRECT_URI", "http://localhost:7171/api/auth/sso/callback")
+SSO_BASE_REDIRECT_URI = os.getenv(
+ "SSO_BASE_REDIRECT_URI", "http://localhost:7171/api/auth/sso/callback"
+)
# Initialize SSO providers
google_sso = None
github_sso = None
+custom_sso = None
if GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET:
google_sso = GoogleSSO(
@@ -47,6 +52,154 @@ if GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET:
allow_insecure_http=True, # Set to False in production with HTTPS
)
+# Custom/Generic OAuth provider configuration
+CUSTOM_SSO_CLIENT_ID = os.getenv("CUSTOM_SSO_CLIENT_ID")
+CUSTOM_SSO_CLIENT_SECRET = os.getenv("CUSTOM_SSO_CLIENT_SECRET")
+CUSTOM_SSO_AUTHORIZATION_ENDPOINT = os.getenv("CUSTOM_SSO_AUTHORIZATION_ENDPOINT")
+CUSTOM_SSO_TOKEN_ENDPOINT = os.getenv("CUSTOM_SSO_TOKEN_ENDPOINT")
+CUSTOM_SSO_USERINFO_ENDPOINT = os.getenv("CUSTOM_SSO_USERINFO_ENDPOINT")
+CUSTOM_SSO_SCOPE = os.getenv("CUSTOM_SSO_SCOPE") # comma-separated list
+CUSTOM_SSO_NAME = os.getenv("CUSTOM_SSO_NAME", "custom")
+CUSTOM_SSO_DISPLAY_NAME = os.getenv("CUSTOM_SSO_DISPLAY_NAME", "Custom")
+
+
+def _default_custom_response_convertor(
+ userinfo: Dict[str, Any], _client=None
+) -> OpenID:
+ """Best-effort convertor from generic userinfo to OpenID."""
+ user_id = (
+ userinfo.get("sub")
+ or userinfo.get("id")
+ or userinfo.get("user_id")
+ or userinfo.get("uid")
+ or userinfo.get("uuid")
+ )
+ email = userinfo.get("email")
+ display_name = (
+ userinfo.get("name")
+ or userinfo.get("preferred_username")
+ or userinfo.get("login")
+ or email
+ or (str(user_id) if user_id is not None else None)
+ )
+ picture = userinfo.get("picture") or userinfo.get("avatar_url")
+ if not user_id and email:
+ user_id = email
+ return OpenID(
+ id=str(user_id) if user_id is not None else "",
+ email=email,
+ display_name=display_name,
+ picture=picture,
+ provider=CUSTOM_SSO_NAME,
+ )
+
+
+if all(
+ [
+ CUSTOM_SSO_CLIENT_ID,
+ CUSTOM_SSO_CLIENT_SECRET,
+ CUSTOM_SSO_AUTHORIZATION_ENDPOINT,
+ CUSTOM_SSO_TOKEN_ENDPOINT,
+ CUSTOM_SSO_USERINFO_ENDPOINT,
+ ]
+):
+ discovery = {
+ "authorization_endpoint": CUSTOM_SSO_AUTHORIZATION_ENDPOINT,
+ "token_endpoint": CUSTOM_SSO_TOKEN_ENDPOINT,
+ "userinfo_endpoint": CUSTOM_SSO_USERINFO_ENDPOINT,
+ }
+ default_scope = (
+ [s.strip() for s in CUSTOM_SSO_SCOPE.split(",") if s.strip()]
+ if CUSTOM_SSO_SCOPE
+ else None
+ )
+ CustomProvider = create_provider(
+ name=CUSTOM_SSO_NAME,
+ discovery_document=discovery,
+ response_convertor=_default_custom_response_convertor,
+ default_scope=default_scope,
+ )
+ custom_sso = CustomProvider(
+ client_id=CUSTOM_SSO_CLIENT_ID,
+ client_secret=CUSTOM_SSO_CLIENT_SECRET,
+ redirect_uri=f"{SSO_BASE_REDIRECT_URI}/custom",
+ allow_insecure_http=True, # Set to False in production with HTTPS
+ )
+
+# Support multiple indexed custom providers (CUSTOM_*_i), up to 10
+custom_sso_providers: Dict[int, Dict[str, Any]] = {}
+
+
+def _make_response_convertor(provider_name: str):
+ def _convert(userinfo: Dict[str, Any], _client=None) -> OpenID:
+ user_id = (
+ userinfo.get("sub")
+ or userinfo.get("id")
+ or userinfo.get("user_id")
+ or userinfo.get("uid")
+ or userinfo.get("uuid")
+ )
+ email = userinfo.get("email")
+ display_name = (
+ userinfo.get("name")
+ or userinfo.get("preferred_username")
+ or userinfo.get("login")
+ or email
+ or (str(user_id) if user_id is not None else None)
+ )
+ picture = userinfo.get("picture") or userinfo.get("avatar_url")
+ if not user_id and email:
+ user_id = email
+ return OpenID(
+ id=str(user_id) if user_id is not None else "",
+ email=email,
+ display_name=display_name,
+ picture=picture,
+ provider=provider_name,
+ )
+
+ return _convert
+
+
+for i in range(1, 11):
+ cid = os.getenv(f"CUSTOM_SSO_CLIENT_ID_{i}")
+ csecret = os.getenv(f"CUSTOM_SSO_CLIENT_SECRET_{i}")
+ auth_ep = os.getenv(f"CUSTOM_SSO_AUTHORIZATION_ENDPOINT_{i}")
+ token_ep = os.getenv(f"CUSTOM_SSO_TOKEN_ENDPOINT_{i}")
+ userinfo_ep = os.getenv(f"CUSTOM_SSO_USERINFO_ENDPOINT_{i}")
+ scope_raw = os.getenv(f"CUSTOM_SSO_SCOPE_{i}")
+ name_i = os.getenv(f"CUSTOM_SSO_NAME_{i}", f"custom{i}")
+ display_name_i = os.getenv(f"CUSTOM_SSO_DISPLAY_NAME_{i}", f"Custom {i}")
+
+ if all([cid, csecret, auth_ep, token_ep, userinfo_ep]):
+ discovery_i = {
+ "authorization_endpoint": auth_ep,
+ "token_endpoint": token_ep,
+ "userinfo_endpoint": userinfo_ep,
+ }
+ default_scope_i = (
+ [s.strip() for s in scope_raw.split(",") if s.strip()]
+ if scope_raw
+ else None
+ )
+ ProviderClass = create_provider(
+ name=name_i,
+ discovery_document=discovery_i,
+ response_convertor=_make_response_convertor(name_i),
+ default_scope=default_scope_i,
+ )
+ provider_instance = ProviderClass(
+ client_id=cid,
+ client_secret=csecret,
+ redirect_uri=f"{SSO_BASE_REDIRECT_URI}/custom/{i}",
+ allow_insecure_http=True, # Set to False in production with HTTPS
+ )
+ custom_sso_providers[i] = {
+ "sso": provider_instance,
+ "name": name_i,
+ "display_name": display_name_i,
+ }
+
class MessageResponse(BaseModel):
message: str
@@ -70,21 +223,25 @@ def create_or_update_sso_user(openid: OpenID, provider: str) -> User:
# Generate username from email or use provider ID
email = openid.email
if not email:
- raise HTTPException(status_code=400, detail="Email is required for SSO authentication")
-
+ raise HTTPException(
+ status_code=400, detail="Email is required for SSO authentication"
+ )
+
# Use email prefix as username, fallback to provider + id
username = email.split("@")[0]
if not username:
username = f"{provider}_{openid.id}"
-
+
# Check if user already exists by email
existing_user = None
users = user_manager.load_users()
for user_data in users.values():
if user_data.get("email") == email:
- existing_user = User(**{k: v for k, v in user_data.items() if k != "password_hash"})
+ existing_user = User(
+ **{k: v for k, v in user_data.items() if k != "password_hash"}
+ )
break
-
+
if existing_user:
# Update last login for existing user (always allowed)
users[existing_user.username]["last_login"] = datetime.utcnow().isoformat()
@@ -96,10 +253,10 @@ def create_or_update_sso_user(openid: OpenID, provider: str) -> User:
# Check if registration is disabled before creating new user
if DISABLE_REGISTRATION:
raise HTTPException(
- status_code=403,
- detail="Registration is disabled. Contact an administrator to create an account."
+ status_code=403,
+ detail="Registration is disabled. Contact an administrator to create an account.",
)
-
+
# Create new user
# Ensure username is unique
counter = 1
@@ -107,20 +264,20 @@ def create_or_update_sso_user(openid: OpenID, provider: str) -> User:
while username in users:
username = f"{original_username}{counter}"
counter += 1
-
+
user = User(
username=username,
email=email,
- role="user" # Default role for SSO users
+ role="user", # Default role for SSO users
)
-
+
users[username] = {
**user.to_dict(),
"sso_provider": provider,
"sso_id": openid.id,
- "password_hash": None # SSO users don't have passwords
+ "password_hash": None, # SSO users don't have passwords
}
-
+
user_manager.save_users(users)
logger.info(f"Created SSO user: {username} via {provider}")
return user
@@ -130,27 +287,51 @@ def create_or_update_sso_user(openid: OpenID, provider: str) -> User:
async def sso_status():
"""Get SSO status and available providers"""
providers = []
-
+
if google_sso:
- providers.append(SSOProvider(
- name="google",
- display_name="Google",
- enabled=True,
- login_url="/api/auth/sso/login/google"
- ))
-
+ providers.append(
+ SSOProvider(
+ name="google",
+ display_name="Google",
+ enabled=True,
+ login_url="/api/auth/sso/login/google",
+ )
+ )
+
if github_sso:
- providers.append(SSOProvider(
- name="github",
- display_name="GitHub",
- enabled=True,
- login_url="/api/auth/sso/login/github"
- ))
-
+ providers.append(
+ SSOProvider(
+ name="github",
+ display_name="GitHub",
+ enabled=True,
+ login_url="/api/auth/sso/login/github",
+ )
+ )
+
+ if custom_sso:
+ providers.append(
+ SSOProvider(
+ name="custom",
+ display_name=CUSTOM_SSO_DISPLAY_NAME,
+ enabled=True,
+ login_url="/api/auth/sso/login/custom",
+ )
+ )
+
+ for idx, cfg in custom_sso_providers.items():
+ providers.append(
+ SSOProvider(
+ name=cfg["name"],
+ display_name=cfg.get("display_name", cfg["name"]),
+ enabled=True,
+ login_url=f"/api/auth/sso/login/custom/{idx}",
+ )
+ )
+
return SSOStatusResponse(
sso_enabled=SSO_ENABLED and AUTH_ENABLED,
providers=providers,
- registration_enabled=not DISABLE_REGISTRATION
+ registration_enabled=not DISABLE_REGISTRATION,
)
@@ -159,12 +340,14 @@ async def google_login():
"""Initiate Google SSO login"""
if not SSO_ENABLED or not AUTH_ENABLED:
raise HTTPException(status_code=400, detail="SSO is disabled")
-
+
if not google_sso:
raise HTTPException(status_code=400, detail="Google SSO is not configured")
-
+
async with google_sso:
- return await google_sso.get_login_redirect(params={"prompt": "consent", "access_type": "offline"})
+ return await google_sso.get_login_redirect(
+ params={"prompt": "consent", "access_type": "offline"}
+ )
@router.get("/sso/login/github")
@@ -172,37 +355,66 @@ async def github_login():
"""Initiate GitHub SSO login"""
if not SSO_ENABLED or not AUTH_ENABLED:
raise HTTPException(status_code=400, detail="SSO is disabled")
-
+
if not github_sso:
raise HTTPException(status_code=400, detail="GitHub SSO is not configured")
-
+
async with github_sso:
return await github_sso.get_login_redirect()
+@router.get("/sso/login/custom")
+async def custom_login():
+ """Initiate Custom SSO login"""
+ if not SSO_ENABLED or not AUTH_ENABLED:
+ raise HTTPException(status_code=400, detail="SSO is disabled")
+
+ if not custom_sso:
+ raise HTTPException(status_code=400, detail="Custom SSO is not configured")
+
+ async with custom_sso:
+ return await custom_sso.get_login_redirect()
+
+
+@router.get("/sso/login/custom/{index}")
+async def custom_login_indexed(index: int):
+ """Initiate indexed Custom SSO login"""
+ if not SSO_ENABLED or not AUTH_ENABLED:
+ raise HTTPException(status_code=400, detail="SSO is disabled")
+
+ cfg = custom_sso_providers.get(index)
+ if not cfg:
+ raise HTTPException(
+ status_code=400, detail="Custom SSO provider not configured"
+ )
+
+ async with cfg["sso"]:
+ return await cfg["sso"].get_login_redirect()
+
+
@router.get("/sso/callback/google")
async def google_callback(request: Request):
"""Handle Google SSO callback"""
if not SSO_ENABLED or not AUTH_ENABLED:
raise HTTPException(status_code=400, detail="SSO is disabled")
-
+
if not google_sso:
raise HTTPException(status_code=400, detail="Google SSO is not configured")
-
+
try:
async with google_sso:
openid = await google_sso.verify_and_process(request)
-
+
# Create or update user
user = create_or_update_sso_user(openid, "google")
-
+
# Create JWT token
access_token = token_manager.create_token(user)
-
+
# Redirect to frontend with token (you might want to customize this)
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
response = RedirectResponse(url=f"{frontend_url}?token={access_token}")
-
+
# Also set as HTTP-only cookie
response.set_cookie(
key="access_token",
@@ -210,18 +422,18 @@ async def google_callback(request: Request):
httponly=True,
secure=False, # Set to True in production with HTTPS
samesite="lax",
- max_age=timedelta(hours=24).total_seconds()
+ max_age=timedelta(hours=24).total_seconds(),
)
-
+
return response
-
+
except HTTPException as e:
# Handle specific HTTP exceptions (like registration disabled)
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
- error_msg = e.detail if hasattr(e, 'detail') else "Authentication failed"
+ error_msg = e.detail if hasattr(e, "detail") else "Authentication failed"
logger.warning(f"Google SSO callback error: {error_msg}")
return RedirectResponse(url=f"{frontend_url}?error={error_msg}")
-
+
except Exception as e:
logger.error(f"Google SSO callback error: {e}")
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
@@ -233,24 +445,24 @@ async def github_callback(request: Request):
"""Handle GitHub SSO callback"""
if not SSO_ENABLED or not AUTH_ENABLED:
raise HTTPException(status_code=400, detail="SSO is disabled")
-
+
if not github_sso:
raise HTTPException(status_code=400, detail="GitHub SSO is not configured")
-
+
try:
async with github_sso:
openid = await github_sso.verify_and_process(request)
-
+
# Create or update user
user = create_or_update_sso_user(openid, "github")
-
+
# Create JWT token
access_token = token_manager.create_token(user)
-
+
# Redirect to frontend with token (you might want to customize this)
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
response = RedirectResponse(url=f"{frontend_url}?token={access_token}")
-
+
# Also set as HTTP-only cookie
response.set_cookie(
key="access_token",
@@ -258,24 +470,123 @@ async def github_callback(request: Request):
httponly=True,
secure=False, # Set to True in production with HTTPS
samesite="lax",
- max_age=timedelta(hours=24).total_seconds()
+ max_age=timedelta(hours=24).total_seconds(),
)
-
+
return response
-
+
except HTTPException as e:
# Handle specific HTTP exceptions (like registration disabled)
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
- error_msg = e.detail if hasattr(e, 'detail') else "Authentication failed"
+ error_msg = e.detail if hasattr(e, "detail") else "Authentication failed"
logger.warning(f"GitHub SSO callback error: {error_msg}")
return RedirectResponse(url=f"{frontend_url}?error={error_msg}")
-
+
except Exception as e:
logger.error(f"GitHub SSO callback error: {e}")
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
return RedirectResponse(url=f"{frontend_url}?error=Authentication failed")
+@router.get("/sso/callback/custom")
+async def custom_callback(request: Request):
+ """Handle Custom SSO callback"""
+ if not SSO_ENABLED or not AUTH_ENABLED:
+ raise HTTPException(status_code=400, detail="SSO is disabled")
+
+ if not custom_sso:
+ raise HTTPException(status_code=400, detail="Custom SSO is not configured")
+
+ try:
+ async with custom_sso:
+ openid = await custom_sso.verify_and_process(request)
+
+ # Create or update user
+ user = create_or_update_sso_user(openid, "custom")
+
+ # Create JWT token
+ access_token = token_manager.create_token(user)
+
+ # Redirect to frontend with token (you might want to customize this)
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ response = RedirectResponse(url=f"{frontend_url}?token={access_token}")
+
+ # Also set as HTTP-only cookie
+ response.set_cookie(
+ key="access_token",
+ value=access_token,
+ httponly=True,
+ secure=False, # Set to True in production with HTTPS
+ samesite="lax",
+ max_age=timedelta(hours=24).total_seconds(),
+ )
+
+ return response
+
+ except HTTPException as e:
+ # Handle specific HTTP exceptions (like registration disabled)
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ error_msg = e.detail if hasattr(e, "detail") else "Authentication failed"
+ logger.warning(f"Custom SSO callback error: {error_msg}")
+ return RedirectResponse(url=f"{frontend_url}?error={error_msg}")
+
+ except Exception as e:
+ logger.error(f"Custom SSO callback error: {e}")
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ return RedirectResponse(url=f"{frontend_url}?error=Authentication failed")
+
+
+@router.get("/sso/callback/custom/{index}")
+async def custom_callback_indexed(request: Request, index: int):
+ """Handle indexed Custom SSO callback"""
+ if not SSO_ENABLED or not AUTH_ENABLED:
+ raise HTTPException(status_code=400, detail="SSO is disabled")
+
+ cfg = custom_sso_providers.get(index)
+ if not cfg:
+ raise HTTPException(
+ status_code=400, detail="Custom SSO provider not configured"
+ )
+
+ try:
+ async with cfg["sso"]:
+ openid = await cfg["sso"].verify_and_process(request)
+
+ # Create or update user
+ user = create_or_update_sso_user(openid, cfg["name"])
+
+ # Create JWT token
+ access_token = token_manager.create_token(user)
+
+ # Redirect to frontend with token (you might want to customize this)
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ response = RedirectResponse(url=f"{frontend_url}?token={access_token}")
+
+ # Also set as HTTP-only cookie
+ response.set_cookie(
+ key="access_token",
+ value=access_token,
+ httponly=True,
+ secure=False, # Set to True in production with HTTPS
+ samesite="lax",
+ max_age=timedelta(hours=24).total_seconds(),
+ )
+
+ return response
+
+ except HTTPException as e:
+ # Handle specific HTTP exceptions (like registration disabled)
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ error_msg = e.detail if hasattr(e, "detail") else "Authentication failed"
+ logger.warning(f"Custom[{index}] SSO callback error: {error_msg}")
+ return RedirectResponse(url=f"{frontend_url}?error={error_msg}")
+
+ except Exception as e:
+ logger.error(f"Custom[{index}] SSO callback error: {e}")
+ frontend_url = os.getenv("FRONTEND_URL", "http://localhost:3000")
+ return RedirectResponse(url=f"{frontend_url}?error=Authentication failed")
+
+
@router.post("/sso/unlink/{provider}", response_model=MessageResponse)
async def unlink_sso_provider(
provider: str,
@@ -284,27 +595,42 @@ async def unlink_sso_provider(
"""Unlink SSO provider from user account"""
if not SSO_ENABLED or not AUTH_ENABLED:
raise HTTPException(status_code=400, detail="SSO is disabled")
-
- if provider not in ["google", "github"]:
+
+ available = []
+ if google_sso:
+ available.append("google")
+ if github_sso:
+ available.append("github")
+ if custom_sso:
+ available.append("custom")
+
+ for cfg in custom_sso_providers.values():
+ available.append(cfg["name"])
+
+ if provider not in available:
raise HTTPException(status_code=400, detail="Invalid SSO provider")
-
+
# Get current user from request (avoiding circular imports)
from .middleware import require_auth_from_state
-
+
current_user = await require_auth_from_state(request)
-
+
if not current_user.sso_provider:
- raise HTTPException(status_code=400, detail="User is not linked to any SSO provider")
-
+ raise HTTPException(
+ status_code=400, detail="User is not linked to any SSO provider"
+ )
+
if current_user.sso_provider != provider:
raise HTTPException(status_code=400, detail=f"User is not linked to {provider}")
-
+
# Update user to remove SSO linkage
users = user_manager.load_users()
if current_user.username in users:
users[current_user.username]["sso_provider"] = None
users[current_user.username]["sso_id"] = None
user_manager.save_users(users)
- logger.info(f"Unlinked SSO provider {provider} from user {current_user.username}")
-
- return MessageResponse(message=f"SSO provider {provider} unlinked successfully")
\ No newline at end of file
+ logger.info(
+ f"Unlinked SSO provider {provider} from user {current_user.username}"
+ )
+
+ return MessageResponse(message=f"SSO provider {provider} unlinked successfully")
diff --git a/routes/content/album.py b/routes/content/album.py
index de98864..d9d8ca4 100755
--- a/routes/content/album.py
+++ b/routes/content/album.py
@@ -5,11 +5,12 @@ import uuid
import time
from routes.utils.celery_queue_manager import download_queue_manager
from routes.utils.celery_tasks import store_task_info, store_task_status, ProgressState
-from routes.utils.get_info import get_spotify_info
+from routes.utils.get_info import get_client, get_album
from routes.utils.errors import DuplicateDownloadError
# Import authentication dependencies
from routes.auth.middleware import require_auth_from_state, User
+# Config and credentials helpers
router = APIRouter()
@@ -34,7 +35,8 @@ async def handle_download(
# Fetch metadata from Spotify
try:
- album_info = get_spotify_info(album_id, "album")
+ client = get_client()
+ album_info = get_album(client, album_id)
if (
not album_info
or not album_info.get("name")
@@ -155,6 +157,7 @@ async def get_album_info(
"""
Retrieve Spotify album metadata given a Spotify album ID.
Expects a query parameter 'id' that contains the Spotify album ID.
+ Returns the raw JSON from get_album in routes.utils.get_info.
"""
spotify_id = request.query_params.get("id")
@@ -162,27 +165,9 @@ async def get_album_info(
return JSONResponse(content={"error": "Missing parameter: id"}, status_code=400)
try:
- # Optional pagination params for tracks
- limit_param = request.query_params.get("limit")
- offset_param = request.query_params.get("offset")
- limit = int(limit_param) if limit_param is not None else None
- offset = int(offset_param) if offset_param is not None else None
-
- # Fetch album metadata
- album_info = get_spotify_info(spotify_id, "album")
- # Fetch album tracks with pagination
- album_tracks = get_spotify_info(
- spotify_id, "album_tracks", limit=limit, offset=offset
- )
-
- # Merge tracks into album payload in the same shape Spotify returns on album
- album_info["tracks"] = album_tracks
-
+ client = get_client()
+ album_info = get_album(client, spotify_id)
return JSONResponse(content=album_info, status_code=200)
- except ValueError as ve:
- return JSONResponse(
- content={"error": f"Invalid limit/offset: {str(ve)}"}, status_code=400
- )
except Exception as e:
error_data = {"error": str(e), "traceback": traceback.format_exc()}
return JSONResponse(content=error_data, status_code=500)
diff --git a/routes/content/artist.py b/routes/content/artist.py
index b4c5302..4649d27 100644
--- a/routes/content/artist.py
+++ b/routes/content/artist.py
@@ -18,10 +18,9 @@ from routes.utils.watch.db import (
get_watched_artists,
add_specific_albums_to_artist_table,
remove_specific_albums_from_artist_table,
- is_album_in_artist_db,
)
from routes.utils.watch.manager import check_watched_artists, get_watch_config
-from routes.utils.get_info import get_spotify_info
+from routes.utils.get_info import get_client, get_artist, get_album
# Import authentication dependencies
from routes.auth.middleware import require_auth_from_state, User
@@ -66,9 +65,6 @@ async def handle_artist_download(
)
try:
- # Import and call the updated download_artist_albums() function.
- # from routes.utils.artist import download_artist_albums # Already imported at top
-
# Delegate to the download_artist_albums function which will handle album filtering
successfully_queued_albums, duplicate_albums = download_artist_albums(
url=url,
@@ -118,13 +114,15 @@ async def cancel_artist_download():
@router.get("/info")
async def get_artist_info(
- request: Request, current_user: User = Depends(require_auth_from_state),
- limit: int = Query(10, ge=1), # default=10, must be >=1
- offset: int = Query(0, ge=0) # default=0, must be >=0
+ request: Request,
+ current_user: User = Depends(require_auth_from_state),
+ limit: int = Query(10, ge=1), # default=10, must be >=1
+ offset: int = Query(0, ge=0), # default=0, must be >=0
):
"""
Retrieves Spotify artist metadata given a Spotify artist ID.
Expects a query parameter 'id' with the Spotify artist ID.
+ Returns the raw JSON from get_artist in routes.utils.get_info.
"""
spotify_id = request.query_params.get("id")
@@ -132,37 +130,8 @@ async def get_artist_info(
return JSONResponse(content={"error": "Missing parameter: id"}, status_code=400)
try:
- # Get artist metadata first
- artist_metadata = get_spotify_info(spotify_id, "artist")
-
- # Get artist discography for albums
- artist_discography = get_spotify_info(spotify_id, "artist_discography", limit=limit, offset=offset)
-
- # Combine metadata with discography
- artist_info = {**artist_metadata, "albums": artist_discography}
-
- # If artist_info is successfully fetched and has albums,
- # check if the artist is watched and augment album items with is_locally_known status
- if (
- artist_info
- and artist_info.get("albums")
- and artist_info["albums"].get("items")
- ):
- watched_artist_details = get_watched_artist(
- spotify_id
- ) # spotify_id is the artist ID
- if watched_artist_details: # Artist is being watched
- for album_item in artist_info["albums"]["items"]:
- if album_item and album_item.get("id"):
- album_id = album_item["id"]
- album_item["is_locally_known"] = is_album_in_artist_db(
- spotify_id, album_id
- )
- elif album_item: # Album object exists but no ID
- album_item["is_locally_known"] = False
- # If not watched, or no albums, is_locally_known will not be added.
- # Frontend should handle absence of this key as false.
-
+ client = get_client()
+ artist_info = get_artist(client, spotify_id)
return JSONResponse(content=artist_info, status_code=200)
except Exception as e:
return JSONResponse(
@@ -191,15 +160,9 @@ async def add_artist_to_watchlist(
if get_watched_artist(artist_spotify_id):
return {"message": f"Artist {artist_spotify_id} is already being watched."}
- # Get artist metadata directly for name and basic info
- artist_metadata = get_spotify_info(artist_spotify_id, "artist")
+ client = get_client()
+ artist_metadata = get_artist(client, artist_spotify_id)
- # Get artist discography for album count
- artist_album_list_data = get_spotify_info(
- artist_spotify_id, "artist_discography"
- )
-
- # Check if we got artist metadata
if not artist_metadata or not artist_metadata.get("name"):
logger.error(
f"Could not fetch artist metadata for {artist_spotify_id} from Spotify."
@@ -211,24 +174,22 @@ async def add_artist_to_watchlist(
},
)
- # Check if we got album data
- if not artist_album_list_data or not isinstance(
- artist_album_list_data.get("items"), list
+ # Derive a rough total album count from groups if present
+ total_albums = 0
+ for key in (
+ "album_group",
+ "single_group",
+ "compilation_group",
+ "appears_on_group",
):
- logger.warning(
- f"Could not fetch album list details for artist {artist_spotify_id} from Spotify. Proceeding with metadata only."
- )
+ grp = artist_metadata.get(key)
+ if isinstance(grp, list):
+ total_albums += len(grp)
- # Construct the artist_data object expected by add_artist_db
artist_data_for_db = {
"id": artist_spotify_id,
"name": artist_metadata.get("name", "Unknown Artist"),
- "albums": { # Mimic structure if add_artist_db expects it for total_albums
- "total": artist_album_list_data.get("total", 0)
- if artist_album_list_data
- else 0
- },
- # Add any other fields add_artist_db might expect from a true artist object if necessary
+ "albums": {"total": total_albums},
}
add_artist_db(artist_data_for_db)
@@ -446,21 +407,25 @@ async def mark_albums_as_known_for_artist(
detail={"error": f"Artist {artist_spotify_id} is not being watched."},
)
+ client = get_client()
fetched_albums_details = []
- for album_id in album_ids:
- try:
- # We need full album details. get_spotify_info with type "album" should provide this.
- album_detail = get_spotify_info(album_id, "album")
- if album_detail and album_detail.get("id"):
- fetched_albums_details.append(album_detail)
- else:
- logger.warning(
- f"Could not fetch details for album {album_id} when marking as known for artist {artist_spotify_id}."
+ try:
+ for album_id in album_ids:
+ try:
+ album_detail = get_album(client, album_id)
+ if album_detail and album_detail.get("id"):
+ fetched_albums_details.append(album_detail)
+ else:
+ logger.warning(
+ f"Could not fetch details for album {album_id} when marking as known for artist {artist_spotify_id}."
+ )
+ except Exception as e:
+ logger.error(
+ f"Failed to fetch Spotify details for album {album_id}: {e}"
)
- except Exception as e:
- logger.error(
- f"Failed to fetch Spotify details for album {album_id}: {e}"
- )
+ finally:
+ # No need to close_client here, as get_client is shared
+ pass
if not fetched_albums_details:
return {
diff --git a/routes/content/bulk_add.py b/routes/content/bulk_add.py
new file mode 100644
index 0000000..ac81959
--- /dev/null
+++ b/routes/content/bulk_add.py
@@ -0,0 +1,142 @@
+import re
+from typing import List
+from fastapi import APIRouter, Request, Depends
+from pydantic import BaseModel
+import logging
+
+# Import authentication dependencies
+from routes.auth.middleware import require_auth_from_state, User
+
+# Import queue management and Spotify info
+from routes.utils.celery_queue_manager import download_queue_manager
+
+# Import authentication dependencies
+
+# Import 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(?:/[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}"
+ )
+ failed_links.append(link)
+ continue
+
+ spotify_type = match.group(1)
+ 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:
+ # Get basic info to confirm existence and get name/artist
+ if spotify_type == "playlist":
+ 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:
+ 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]
+ )
+ elif spotify_type == "playlist":
+ owner = item_info.get("owner", {})
+ artist_name = owner.get("display_name", "Unknown Owner")
+
+ # Construct URL for the download task
+ spotify_url = f"https://open.spotify.com/{spotify_type}/{spotify_id}"
+
+ # Prepare task data for the queue manager
+ task_data = {
+ "download_type": spotify_type,
+ "url": spotify_url,
+ "name": item_name,
+ "artist": artist_name,
+ "spotify_id": spotify_id,
+ "type": spotify_type,
+ "username": current_user.username,
+ "orig_request": dict(req.query_params),
+ }
+
+ # Add to download queue using the queue manager
+ task_id = download_queue_manager.add_task(task_data)
+
+ if task_id:
+ 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."
+ )
+ failed_links.append(link)
+ continue
+
+ except Exception as e:
+ logger.error(f"Error processing Spotify link {link}: {e}", exc_info=True)
+ failed_links.append(link)
+
+ message = f"Successfully added {added_count}/{total_links} links to queue."
+ if failed_links:
+ message += f" Failed to add {len(failed_links)} links."
+ logger.warning(f"Bulk add completed with {len(failed_links)} failures.")
+ else:
+ logger.info(f"Bulk add completed successfully. Added {added_count} links.")
+
+ return {
+ "message": message,
+ "count": added_count,
+ "failed_links": failed_links,
+ }
diff --git a/routes/content/playlist.py b/routes/content/playlist.py
index 066fafe..1d35d77 100755
--- a/routes/content/playlist.py
+++ b/routes/content/playlist.py
@@ -1,6 +1,5 @@
from fastapi import APIRouter, HTTPException, Request, Depends
from fastapi.responses import JSONResponse
-import json
import traceback
import logging # Added logging import
import uuid # For generating error task IDs
@@ -20,10 +19,9 @@ from routes.utils.watch.db import (
get_watched_playlist,
get_watched_playlists,
add_specific_tracks_to_playlist_table,
- remove_specific_tracks_from_playlist_table,
- is_track_in_playlist_db, # Added import
+ remove_specific_tracks_from_playlist_table, # Added import
)
-from routes.utils.get_info import get_spotify_info # Already used, but ensure it's here
+from routes.utils.get_info import get_client, get_playlist, get_track
from routes.utils.watch.manager import (
check_watched_playlists,
get_watch_config,
@@ -31,7 +29,9 @@ from routes.utils.watch.manager import (
from routes.utils.errors import DuplicateDownloadError
# Import authentication dependencies
-from routes.auth.middleware import require_auth_from_state, require_admin_from_state, User
+from routes.auth.middleware import require_auth_from_state, User
+from routes.utils.celery_config import get_config_params
+from routes.utils.credentials import get_spotify_blob_path
logger = logging.getLogger(__name__) # Added logger initialization
router = APIRouter()
@@ -43,7 +43,11 @@ def construct_spotify_url(item_id: str, item_type: str = "track") -> str:
@router.get("/download/{playlist_id}")
-async def handle_download(playlist_id: str, request: Request, current_user: User = Depends(require_auth_from_state)):
+async def handle_download(
+ playlist_id: str,
+ request: Request,
+ current_user: User = Depends(require_auth_from_state),
+):
# Retrieve essential parameters from the request.
# name = request.args.get('name') # Removed
# artist = request.args.get('artist') # Removed
@@ -51,11 +55,14 @@ async def handle_download(playlist_id: str, request: Request, current_user: User
# Construct the URL from playlist_id
url = construct_spotify_url(playlist_id, "playlist")
- orig_params["original_url"] = str(request.url) # Update original_url to the constructed one
+ orig_params["original_url"] = str(
+ request.url
+ ) # Update original_url to the constructed one
# Fetch metadata from Spotify using optimized function
try:
from routes.utils.get_info import get_playlist_metadata
+
playlist_info = get_playlist_metadata(playlist_id)
if (
not playlist_info
@@ -66,7 +73,7 @@ async def handle_download(playlist_id: str, request: Request, current_user: User
content={
"error": f"Could not retrieve metadata for playlist ID: {playlist_id}"
},
- status_code=404
+ status_code=404,
)
name_from_spotify = playlist_info.get("name")
@@ -79,14 +86,13 @@ async def handle_download(playlist_id: str, request: Request, current_user: User
content={
"error": f"Failed to fetch metadata for playlist {playlist_id}: {str(e)}"
},
- status_code=500
+ status_code=500,
)
# Validate required parameters
if not url: # This check might be redundant now but kept for safety
return JSONResponse(
- content={"error": "Missing required parameter: url"},
- status_code=400
+ content={"error": "Missing required parameter: url"}, status_code=400
)
try:
@@ -106,7 +112,7 @@ async def handle_download(playlist_id: str, request: Request, current_user: User
"error": "Duplicate download detected.",
"existing_task": e.existing_task,
},
- status_code=409
+ status_code=409,
)
except Exception as e:
# Generic error handling for other issues during task submission
@@ -136,25 +142,23 @@ async def handle_download(playlist_id: str, request: Request, current_user: User
"error": f"Failed to queue playlist download: {str(e)}",
"task_id": error_task_id,
},
- status_code=500
+ status_code=500,
)
- return JSONResponse(
- content={"task_id": task_id},
- status_code=202
- )
+ return JSONResponse(content={"task_id": task_id}, status_code=202)
@router.get("/download/cancel")
-async def cancel_download(request: Request, current_user: User = Depends(require_auth_from_state)):
+async def cancel_download(
+ request: Request, current_user: User = Depends(require_auth_from_state)
+):
"""
Cancel a running playlist download process by its task id.
"""
task_id = request.query_params.get("task_id")
if not task_id:
return JSONResponse(
- content={"error": "Missing task id (task_id) parameter"},
- status_code=400
+ content={"error": "Missing task id (task_id) parameter"}, status_code=400
)
# Use the queue manager's cancellation method.
@@ -165,144 +169,138 @@ async def cancel_download(request: Request, current_user: User = Depends(require
@router.get("/info")
-async def get_playlist_info(request: Request, current_user: User = Depends(require_auth_from_state)):
+async def get_playlist_info(
+ request: Request, current_user: User = Depends(require_auth_from_state)
+):
"""
Retrieve Spotify playlist metadata given a Spotify playlist ID.
Expects a query parameter 'id' that contains the Spotify playlist ID.
- """
- spotify_id = request.query_params.get("id")
- include_tracks = request.query_params.get("include_tracks", "false").lower() == "true"
-
- if not spotify_id:
- return JSONResponse(
- content={"error": "Missing parameter: id"},
- status_code=400
- )
-
- try:
- # Use the optimized playlist info function
- from routes.utils.get_info import get_playlist_info_optimized
- playlist_info = get_playlist_info_optimized(spotify_id, include_tracks=include_tracks)
-
- # If playlist_info is successfully fetched, check if it's watched
- # and augment track items with is_locally_known status
- if playlist_info and playlist_info.get("id"):
- watched_playlist_details = get_watched_playlist(playlist_info["id"])
- if watched_playlist_details: # Playlist is being watched
- if playlist_info.get("tracks") and playlist_info["tracks"].get("items"):
- for item in playlist_info["tracks"]["items"]:
- if item and item.get("track") and item["track"].get("id"):
- track_id = item["track"]["id"]
- item["track"]["is_locally_known"] = is_track_in_playlist_db(
- playlist_info["id"], track_id
- )
- elif item and item.get(
- "track"
- ): # Track object exists but no ID
- item["track"]["is_locally_known"] = False
- # If not watched, or no tracks, is_locally_known will not be added, or tracks won't exist to add it to.
- # Frontend should handle absence of this key as false.
-
- return JSONResponse(
- content=playlist_info, status_code=200
- )
- except Exception as e:
- error_data = {"error": str(e), "traceback": traceback.format_exc()}
- return JSONResponse(content=error_data, status_code=500)
-
-
-@router.get("/metadata")
-async def get_playlist_metadata(request: Request, current_user: User = Depends(require_auth_from_state)):
- """
- Retrieve only Spotify playlist metadata (no tracks) to avoid rate limiting.
- Expects a query parameter 'id' that contains the Spotify playlist ID.
+ Always returns the raw JSON from get_playlist with expand_items=False.
"""
spotify_id = request.query_params.get("id")
if not spotify_id:
- return JSONResponse(
- content={"error": "Missing parameter: id"},
- status_code=400
- )
+ return JSONResponse(content={"error": "Missing parameter: id"}, status_code=400)
try:
- # Use the optimized playlist metadata function
- from routes.utils.get_info import get_playlist_metadata
- playlist_metadata = get_playlist_metadata(spotify_id)
+ # Resolve active account's credentials blob
+ cfg = get_config_params() or {}
+ active_account = cfg.get("spotify")
+ if not active_account:
+ return JSONResponse(
+ content={"error": "Active Spotify account not set in configuration."},
+ status_code=500,
+ )
+ blob_path = get_spotify_blob_path(active_account)
+ if not blob_path.exists():
+ return JSONResponse(
+ content={
+ "error": f"Spotify credentials blob not found for account '{active_account}'"
+ },
+ status_code=500,
+ )
- return JSONResponse(
- content=playlist_metadata, status_code=200
- )
- except Exception as e:
- error_data = {"error": str(e), "traceback": traceback.format_exc()}
- return JSONResponse(content=error_data, status_code=500)
+ client = get_client()
+ try:
+ playlist_info = get_playlist(client, spotify_id, expand_items=False)
+ finally:
+ pass
+ # Ensure id field is present (librespot sometimes omits it)
+ if playlist_info and "id" not in playlist_info:
+ playlist_info["id"] = spotify_id
-
-@router.get("/tracks")
-async def get_playlist_tracks(request: Request, current_user: User = Depends(require_auth_from_state)):
- """
- Retrieve playlist tracks with pagination support for progressive loading.
- Expects query parameters: 'id' (playlist ID), 'limit' (optional), 'offset' (optional).
- """
- spotify_id = request.query_params.get("id")
- limit = int(request.query_params.get("limit", 50))
- offset = int(request.query_params.get("offset", 0))
-
- if not spotify_id:
- return JSONResponse(
- content={"error": "Missing parameter: id"},
- status_code=400
- )
-
- try:
- # Use the optimized playlist tracks function
- from routes.utils.get_info import get_playlist_tracks
- tracks_data = get_playlist_tracks(spotify_id, limit=limit, offset=offset)
-
- return JSONResponse(
- content=tracks_data, status_code=200
- )
+ return JSONResponse(content=playlist_info, status_code=200)
except Exception as e:
error_data = {"error": str(e), "traceback": traceback.format_exc()}
return JSONResponse(content=error_data, status_code=500)
@router.put("/watch/{playlist_spotify_id}")
-async def add_to_watchlist(playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)):
+async def add_to_watchlist(
+ playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)
+):
"""Adds a playlist to the watchlist."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
- raise HTTPException(status_code=403, detail={"error": "Watch feature is currently disabled globally."})
+ raise HTTPException(
+ status_code=403,
+ detail={"error": "Watch feature is currently disabled globally."},
+ )
logger.info(f"Attempting to add playlist {playlist_spotify_id} to watchlist.")
try:
# Check if already watched
if get_watched_playlist(playlist_spotify_id):
- return {"message": f"Playlist {playlist_spotify_id} is already being watched."}
+ return {
+ "message": f"Playlist {playlist_spotify_id} is already being watched."
+ }
- # Fetch playlist details from Spotify to populate our DB
- from routes.utils.get_info import get_playlist_metadata
- playlist_data = get_playlist_metadata(playlist_spotify_id)
- if not playlist_data or "id" not in playlist_data:
+ # Fetch playlist details from Spotify to populate our DB (metadata only)
+ # 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"Could not fetch details for playlist {playlist_spotify_id} from Spotify."
+ f"Failed to fetch playlist metadata for {playlist_spotify_id}: {e}",
+ exc_info=True,
+ )
+ raise HTTPException(
+ status_code=500,
+ detail={
+ "error": f"Failed to fetch metadata for playlist {playlist_spotify_id}: {str(e)}"
+ },
+ )
+
+ # 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())}"
+ )
+ try:
+ playlist_data["id"] = playlist_spotify_id
+ except Exception:
+ pass
+
+ # 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"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."
+ },
)
- add_playlist_db(playlist_data) # This also creates the tracks table
+ # 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
- # REMOVED: Do not add initial tracks directly to DB.
- # The playlist watch manager will pick them up as new and queue downloads.
- # Tracks will be added to DB only after successful download via Celery task callback.
- # initial_track_items = playlist_data.get('tracks', {}).get('items', [])
- # if initial_track_items:
- # from routes.utils.watch.db import add_tracks_to_playlist_db # Keep local import for clarity
- # add_tracks_to_playlist_db(playlist_spotify_id, initial_track_items)
+ # 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(
f"Playlist {playlist_spotify_id} added to watchlist. Its tracks will be processed by the watch manager."
@@ -317,11 +315,16 @@ async def add_to_watchlist(playlist_spotify_id: str, current_user: User = Depend
f"Error adding playlist {playlist_spotify_id} to watchlist: {e}",
exc_info=True,
)
- raise HTTPException(status_code=500, detail={"error": f"Could not add playlist to watchlist: {str(e)}"})
+ raise HTTPException(
+ status_code=500,
+ detail={"error": f"Could not add playlist to watchlist: {str(e)}"},
+ )
@router.get("/watch/{playlist_spotify_id}/status")
-async def get_playlist_watch_status(playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)):
+async def get_playlist_watch_status(
+ playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)
+):
"""Checks if a specific playlist is being watched."""
logger.info(f"Checking watch status for playlist {playlist_spotify_id}.")
try:
@@ -337,22 +340,31 @@ async def get_playlist_watch_status(playlist_spotify_id: str, current_user: User
f"Error checking watch status for playlist {playlist_spotify_id}: {e}",
exc_info=True,
)
- raise HTTPException(status_code=500, detail={"error": f"Could not check watch status: {str(e)}"})
+ raise HTTPException(
+ status_code=500, detail={"error": f"Could not check watch status: {str(e)}"}
+ )
@router.delete("/watch/{playlist_spotify_id}")
-async def remove_from_watchlist(playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)):
+async def remove_from_watchlist(
+ playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)
+):
"""Removes a playlist from the watchlist."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
- raise HTTPException(status_code=403, detail={"error": "Watch feature is currently disabled globally."})
+ raise HTTPException(
+ status_code=403,
+ detail={"error": "Watch feature is currently disabled globally."},
+ )
logger.info(f"Attempting to remove playlist {playlist_spotify_id} from watchlist.")
try:
if not get_watched_playlist(playlist_spotify_id):
raise HTTPException(
status_code=404,
- detail={"error": f"Playlist {playlist_spotify_id} not found in watchlist."}
+ detail={
+ "error": f"Playlist {playlist_spotify_id} not found in watchlist."
+ },
)
remove_playlist_db(playlist_spotify_id)
@@ -369,12 +381,16 @@ async def remove_from_watchlist(playlist_spotify_id: str, current_user: User = D
)
raise HTTPException(
status_code=500,
- detail={"error": f"Could not remove playlist from watchlist: {str(e)}"}
+ detail={"error": f"Could not remove playlist from watchlist: {str(e)}"},
)
@router.post("/watch/{playlist_spotify_id}/tracks")
-async def mark_tracks_as_known(playlist_spotify_id: str, request: Request, current_user: User = Depends(require_auth_from_state)):
+async def mark_tracks_as_known(
+ playlist_spotify_id: str,
+ request: Request,
+ current_user: User = Depends(require_auth_from_state),
+):
"""Fetches details for given track IDs and adds/updates them in the playlist's local DB table."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
@@ -382,7 +398,7 @@ async def mark_tracks_as_known(playlist_spotify_id: str, request: Request, curre
status_code=403,
detail={
"error": "Watch feature is currently disabled globally. Cannot mark tracks."
- }
+ },
)
logger.info(
@@ -397,19 +413,22 @@ async def mark_tracks_as_known(playlist_spotify_id: str, request: Request, curre
status_code=400,
detail={
"error": "Invalid request body. Expecting a JSON array of track Spotify IDs."
- }
+ },
)
if not get_watched_playlist(playlist_spotify_id):
raise HTTPException(
status_code=404,
- detail={"error": f"Playlist {playlist_spotify_id} is not being watched."}
+ detail={
+ "error": f"Playlist {playlist_spotify_id} is not being watched."
+ },
)
fetched_tracks_details = []
+ client = get_client()
for track_id in track_ids:
try:
- track_detail = get_spotify_info(track_id, "track")
+ track_detail = get_track(client, track_id)
if track_detail and track_detail.get("id"):
fetched_tracks_details.append(track_detail)
else:
@@ -443,11 +462,18 @@ async def mark_tracks_as_known(playlist_spotify_id: str, request: Request, curre
f"Error marking tracks as known for playlist {playlist_spotify_id}: {e}",
exc_info=True,
)
- raise HTTPException(status_code=500, detail={"error": f"Could not mark tracks as known: {str(e)}"})
+ raise HTTPException(
+ status_code=500,
+ detail={"error": f"Could not mark tracks as known: {str(e)}"},
+ )
@router.delete("/watch/{playlist_spotify_id}/tracks")
-async def mark_tracks_as_missing_locally(playlist_spotify_id: str, request: Request, current_user: User = Depends(require_auth_from_state)):
+async def mark_tracks_as_missing_locally(
+ playlist_spotify_id: str,
+ request: Request,
+ current_user: User = Depends(require_auth_from_state),
+):
"""Removes specified tracks from the playlist's local DB table."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
@@ -455,7 +481,7 @@ async def mark_tracks_as_missing_locally(playlist_spotify_id: str, request: Requ
status_code=403,
detail={
"error": "Watch feature is currently disabled globally. Cannot mark tracks."
- }
+ },
)
logger.info(
@@ -470,13 +496,15 @@ async def mark_tracks_as_missing_locally(playlist_spotify_id: str, request: Requ
status_code=400,
detail={
"error": "Invalid request body. Expecting a JSON array of track Spotify IDs."
- }
+ },
)
if not get_watched_playlist(playlist_spotify_id):
raise HTTPException(
status_code=404,
- detail={"error": f"Playlist {playlist_spotify_id} is not being watched."}
+ detail={
+ "error": f"Playlist {playlist_spotify_id} is not being watched."
+ },
)
deleted_count = remove_specific_tracks_from_playlist_table(
@@ -495,22 +523,32 @@ async def mark_tracks_as_missing_locally(playlist_spotify_id: str, request: Requ
f"Error marking tracks as missing (deleting locally) for playlist {playlist_spotify_id}: {e}",
exc_info=True,
)
- raise HTTPException(status_code=500, detail={"error": f"Could not mark tracks as missing: {str(e)}"})
+ raise HTTPException(
+ status_code=500,
+ detail={"error": f"Could not mark tracks as missing: {str(e)}"},
+ )
@router.get("/watch/list")
-async def list_watched_playlists_endpoint(current_user: User = Depends(require_auth_from_state)):
+async def list_watched_playlists_endpoint(
+ current_user: User = Depends(require_auth_from_state),
+):
"""Lists all playlists currently in the watchlist."""
try:
playlists = get_watched_playlists()
return playlists
except Exception as e:
logger.error(f"Error listing watched playlists: {e}", exc_info=True)
- raise HTTPException(status_code=500, detail={"error": f"Could not list watched playlists: {str(e)}"})
+ raise HTTPException(
+ status_code=500,
+ detail={"error": f"Could not list watched playlists: {str(e)}"},
+ )
@router.post("/watch/trigger_check")
-async def trigger_playlist_check_endpoint(current_user: User = Depends(require_auth_from_state)):
+async def trigger_playlist_check_endpoint(
+ current_user: User = Depends(require_auth_from_state),
+):
"""Manually triggers the playlist checking mechanism for all watched playlists."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
@@ -518,7 +556,7 @@ async def trigger_playlist_check_endpoint(current_user: User = Depends(require_a
status_code=403,
detail={
"error": "Watch feature is currently disabled globally. Cannot trigger check."
- }
+ },
)
logger.info("Manual trigger for playlist check received for all playlists.")
@@ -535,12 +573,14 @@ async def trigger_playlist_check_endpoint(current_user: User = Depends(require_a
)
raise HTTPException(
status_code=500,
- detail={"error": f"Could not trigger playlist check for all: {str(e)}"}
+ detail={"error": f"Could not trigger playlist check for all: {str(e)}"},
)
@router.post("/watch/trigger_check/{playlist_spotify_id}")
-async def trigger_specific_playlist_check_endpoint(playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)):
+async def trigger_specific_playlist_check_endpoint(
+ playlist_spotify_id: str, current_user: User = Depends(require_auth_from_state)
+):
"""Manually triggers the playlist checking mechanism for a specific playlist."""
watch_config = get_watch_config()
if not watch_config.get("enabled", False):
@@ -548,7 +588,7 @@ async def trigger_specific_playlist_check_endpoint(playlist_spotify_id: str, cur
status_code=403,
detail={
"error": "Watch feature is currently disabled globally. Cannot trigger check."
- }
+ },
)
logger.info(
@@ -565,7 +605,7 @@ async def trigger_specific_playlist_check_endpoint(playlist_spotify_id: str, cur
status_code=404,
detail={
"error": f"Playlist {playlist_spotify_id} is not in the watchlist. Add it first."
- }
+ },
)
# Run check_watched_playlists with the specific ID
@@ -590,5 +630,5 @@ async def trigger_specific_playlist_check_endpoint(playlist_spotify_id: str, cur
status_code=500,
detail={
"error": f"Could not trigger playlist check for {playlist_spotify_id}: {str(e)}"
- }
+ },
)
diff --git a/routes/content/track.py b/routes/content/track.py
index b3d6d3c..f3f1213 100755
--- a/routes/content/track.py
+++ b/routes/content/track.py
@@ -1,12 +1,11 @@
-from fastapi import APIRouter, HTTPException, Request, Depends
+from fastapi import APIRouter, Request, Depends
from fastapi.responses import JSONResponse
-import json
import traceback
import uuid
import time
from routes.utils.celery_queue_manager import download_queue_manager
from routes.utils.celery_tasks import store_task_info, store_task_status, ProgressState
-from routes.utils.get_info import get_spotify_info
+from routes.utils.get_info import get_client, get_track
from routes.utils.errors import DuplicateDownloadError
# Import authentication dependencies
@@ -21,7 +20,11 @@ def construct_spotify_url(item_id: str, item_type: str = "track") -> str:
@router.get("/download/{track_id}")
-async def handle_download(track_id: str, request: Request, current_user: User = Depends(require_auth_from_state)):
+async def handle_download(
+ track_id: str,
+ request: Request,
+ current_user: User = Depends(require_auth_from_state),
+):
# Retrieve essential parameters from the request.
# name = request.args.get('name') # Removed
# artist = request.args.get('artist') # Removed
@@ -31,15 +34,18 @@ async def handle_download(track_id: str, request: Request, current_user: User =
# Fetch metadata from Spotify
try:
- track_info = get_spotify_info(track_id, "track")
+ client = get_client()
+ track_info = get_track(client, track_id)
if (
not track_info
or not track_info.get("name")
or not track_info.get("artists")
):
return JSONResponse(
- content={"error": f"Could not retrieve metadata for track ID: {track_id}"},
- status_code=404
+ content={
+ "error": f"Could not retrieve metadata for track ID: {track_id}"
+ },
+ status_code=404,
)
name_from_spotify = track_info.get("name")
@@ -51,15 +57,16 @@ async def handle_download(track_id: str, request: Request, current_user: User =
except Exception as e:
return JSONResponse(
- content={"error": f"Failed to fetch metadata for track {track_id}: {str(e)}"},
- status_code=500
+ content={
+ "error": f"Failed to fetch metadata for track {track_id}: {str(e)}"
+ },
+ status_code=500,
)
# Validate required parameters
if not url:
return JSONResponse(
- content={"error": "Missing required parameter: url"},
- status_code=400
+ content={"error": "Missing required parameter: url"}, status_code=400
)
# Add the task to the queue with only essential parameters
@@ -84,7 +91,7 @@ async def handle_download(track_id: str, request: Request, current_user: User =
"error": "Duplicate download detected.",
"existing_task": e.existing_task,
},
- status_code=409
+ status_code=409,
)
except Exception as e:
# Generic error handling for other issues during task submission
@@ -116,25 +123,23 @@ async def handle_download(track_id: str, request: Request, current_user: User =
"error": f"Failed to queue track download: {str(e)}",
"task_id": error_task_id,
},
- status_code=500
+ status_code=500,
)
- return JSONResponse(
- content={"task_id": task_id},
- status_code=202
- )
+ return JSONResponse(content={"task_id": task_id}, status_code=202)
@router.get("/download/cancel")
-async def cancel_download(request: Request, current_user: User = Depends(require_auth_from_state)):
+async def cancel_download(
+ request: Request, current_user: User = Depends(require_auth_from_state)
+):
"""
Cancel a running download process by its task id.
"""
task_id = request.query_params.get("task_id")
if not task_id:
return JSONResponse(
- content={"error": "Missing process id (task_id) parameter"},
- status_code=400
+ content={"error": "Missing process id (task_id) parameter"}, status_code=400
)
# Use the queue manager's cancellation method.
@@ -145,7 +150,9 @@ async def cancel_download(request: Request, current_user: User = Depends(require
@router.get("/info")
-async def get_track_info(request: Request, current_user: User = Depends(require_auth_from_state)):
+async def get_track_info(
+ request: Request, current_user: User = Depends(require_auth_from_state)
+):
"""
Retrieve Spotify track metadata given a Spotify track ID.
Expects a query parameter 'id' that contains the Spotify track ID.
@@ -153,14 +160,11 @@ async def get_track_info(request: Request, current_user: User = Depends(require_
spotify_id = request.query_params.get("id")
if not spotify_id:
- return JSONResponse(
- content={"error": "Missing parameter: id"},
- status_code=400
- )
+ return JSONResponse(content={"error": "Missing parameter: id"}, status_code=400)
try:
- # Use the get_spotify_info function (already imported at top)
- track_info = get_spotify_info(spotify_id, "track")
+ client = get_client()
+ track_info = get_track(client, spotify_id)
return JSONResponse(content=track_info, status_code=200)
except Exception as e:
error_data = {"error": str(e), "traceback": traceback.format_exc()}
diff --git a/routes/migrations/runner.py b/routes/migrations/runner.py
index 4981ac9..6a12991 100644
--- a/routes/migrations/runner.py
+++ b/routes/migrations/runner.py
@@ -3,16 +3,11 @@ import sqlite3
from pathlib import Path
from typing import Optional
-from .v3_2_0 import MigrationV3_2_0
-from .v3_2_1 import log_noop_migration_detected
+from .v3_3_0 import MigrationV3_3_0
logger = logging.getLogger(__name__)
DATA_DIR = Path("./data")
-HISTORY_DB = DATA_DIR / "history" / "download_history.db"
-WATCH_DIR = DATA_DIR / "watch"
-PLAYLISTS_DB = WATCH_DIR / "playlists.db"
-ARTISTS_DB = WATCH_DIR / "artists.db"
# Credentials
CREDS_DIR = DATA_DIR / "creds"
@@ -20,89 +15,6 @@ ACCOUNTS_DB = CREDS_DIR / "accounts.db"
BLOBS_DIR = CREDS_DIR / "blobs"
SEARCH_JSON = CREDS_DIR / "search.json"
-# Expected children table columns for history (album_/playlist_)
-CHILDREN_EXPECTED_COLUMNS: dict[str, str] = {
- "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
- "title": "TEXT NOT NULL",
- "artists": "TEXT",
- "album_title": "TEXT",
- "duration_ms": "INTEGER",
- "track_number": "INTEGER",
- "disc_number": "INTEGER",
- "explicit": "BOOLEAN",
- "status": "TEXT NOT NULL",
- "external_ids": "TEXT",
- "genres": "TEXT",
- "isrc": "TEXT",
- "timestamp": "REAL NOT NULL",
- "position": "INTEGER",
- "metadata": "TEXT",
-}
-
-# 3.2.0 expected schemas for Watch DBs (kept here to avoid importing modules with side-effects)
-EXPECTED_WATCHED_PLAYLISTS_COLUMNS: dict[str, str] = {
- "spotify_id": "TEXT PRIMARY KEY",
- "name": "TEXT",
- "owner_id": "TEXT",
- "owner_name": "TEXT",
- "total_tracks": "INTEGER",
- "link": "TEXT",
- "snapshot_id": "TEXT",
- "last_checked": "INTEGER",
- "added_at": "INTEGER",
- "is_active": "INTEGER DEFAULT 1",
-}
-
-EXPECTED_PLAYLIST_TRACKS_COLUMNS: dict[str, str] = {
- "spotify_track_id": "TEXT PRIMARY KEY",
- "title": "TEXT",
- "artist_names": "TEXT",
- "album_name": "TEXT",
- "album_artist_names": "TEXT",
- "track_number": "INTEGER",
- "album_spotify_id": "TEXT",
- "duration_ms": "INTEGER",
- "added_at_playlist": "TEXT",
- "added_to_db": "INTEGER",
- "is_present_in_spotify": "INTEGER DEFAULT 1",
- "last_seen_in_spotify": "INTEGER",
- "snapshot_id": "TEXT",
- "final_path": "TEXT",
-}
-
-EXPECTED_WATCHED_ARTISTS_COLUMNS: dict[str, str] = {
- "spotify_id": "TEXT PRIMARY KEY",
- "name": "TEXT",
- "link": "TEXT",
- "total_albums_on_spotify": "INTEGER",
- "last_checked": "INTEGER",
- "added_at": "INTEGER",
- "is_active": "INTEGER DEFAULT 1",
- "genres": "TEXT",
- "popularity": "INTEGER",
- "image_url": "TEXT",
-}
-
-EXPECTED_ARTIST_ALBUMS_COLUMNS: dict[str, str] = {
- "album_spotify_id": "TEXT PRIMARY KEY",
- "artist_spotify_id": "TEXT",
- "name": "TEXT",
- "album_group": "TEXT",
- "album_type": "TEXT",
- "release_date": "TEXT",
- "release_date_precision": "TEXT",
- "total_tracks": "INTEGER",
- "link": "TEXT",
- "image_url": "TEXT",
- "added_to_db": "INTEGER",
- "last_seen_on_spotify": "INTEGER",
- "download_task_id": "TEXT",
- "download_status": "INTEGER DEFAULT 0",
- "is_fully_downloaded_managed_by_app": "INTEGER DEFAULT 0",
-}
-
-m320 = MigrationV3_2_0()
-
def _safe_connect(path: Path) -> Optional[sqlite3.Connection]:
try:
@@ -115,245 +27,6 @@ def _safe_connect(path: Path) -> Optional[sqlite3.Connection]:
return None
-def _ensure_table_schema(
- conn: sqlite3.Connection,
- table_name: str,
- expected_columns: dict[str, str],
- table_description: str,
-) -> None:
- try:
- cur = conn.execute(f"PRAGMA table_info({table_name})")
- existing_info = cur.fetchall()
- existing_names = {row[1] for row in existing_info}
- for col_name, col_type in expected_columns.items():
- if col_name in existing_names:
- continue
- col_type_for_add = (
- col_type.replace("PRIMARY KEY", "")
- .replace("AUTOINCREMENT", "")
- .replace("NOT NULL", "")
- .strip()
- )
- try:
- conn.execute(
- f"ALTER TABLE {table_name} ADD COLUMN {col_name} {col_type_for_add}"
- )
- logger.info(
- f"Added missing column '{col_name} {col_type_for_add}' to {table_description} table '{table_name}'."
- )
- except sqlite3.OperationalError as e:
- logger.warning(
- f"Could not add column '{col_name}' to {table_description} table '{table_name}': {e}"
- )
- except Exception as e:
- logger.error(
- f"Error ensuring schema for {table_description} table '{table_name}': {e}",
- exc_info=True,
- )
-
-
-def _create_or_update_children_table(conn: sqlite3.Connection, table_name: str) -> None:
- conn.execute(
- f"""
- CREATE TABLE IF NOT EXISTS {table_name} (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- title TEXT NOT NULL,
- artists TEXT,
- album_title TEXT,
- duration_ms INTEGER,
- track_number INTEGER,
- disc_number INTEGER,
- explicit BOOLEAN,
- status TEXT NOT NULL,
- external_ids TEXT,
- genres TEXT,
- isrc TEXT,
- timestamp REAL NOT NULL,
- position INTEGER,
- metadata TEXT
- )
- """
- )
- _ensure_table_schema(
- conn, table_name, CHILDREN_EXPECTED_COLUMNS, "children history"
- )
-
-
-# --- Helper to validate instance is at least 3.1.2 on history DB ---
-
-
-def _history_children_tables(conn: sqlite3.Connection) -> list[str]:
- tables: set[str] = set()
- try:
- cur = conn.execute(
- "SELECT name FROM sqlite_master WHERE type='table' AND (name LIKE 'album_%' OR name LIKE 'playlist_%') AND name != 'download_history'"
- )
- for row in cur.fetchall():
- if row and row[0]:
- tables.add(row[0])
- except sqlite3.Error as e:
- logger.warning(f"Failed to scan sqlite_master for children tables: {e}")
-
- try:
- cur = conn.execute(
- "SELECT DISTINCT children_table FROM download_history WHERE children_table IS NOT NULL AND TRIM(children_table) != ''"
- )
- for row in cur.fetchall():
- t = row[0]
- if t:
- tables.add(t)
- except sqlite3.Error as e:
- logger.warning(f"Failed to scan download_history for children tables: {e}")
-
- return sorted(tables)
-
-
-def _is_history_at_least_3_2_0(conn: sqlite3.Connection) -> bool:
- required_cols = {"service", "quality_format", "quality_bitrate"}
- tables = _history_children_tables(conn)
- if not tables:
- # Nothing to migrate implies OK
- return True
- for t in tables:
- try:
- cur = conn.execute(f"PRAGMA table_info({t})")
- cols = {row[1] for row in cur.fetchall()}
- if not required_cols.issubset(cols):
- return False
- except sqlite3.OperationalError:
- return False
- return True
-
-
-# --- 3.2.0 verification helpers for Watch DBs ---
-
-
-def _update_watch_playlists_db(conn: sqlite3.Connection) -> None:
- try:
- # Ensure core watched_playlists table exists and has expected schema
- conn.execute(
- """
- CREATE TABLE IF NOT EXISTS watched_playlists (
- spotify_id TEXT PRIMARY KEY,
- name TEXT,
- owner_id TEXT,
- owner_name TEXT,
- total_tracks INTEGER,
- link TEXT,
- snapshot_id TEXT,
- last_checked INTEGER,
- added_at INTEGER,
- is_active INTEGER DEFAULT 1
- )
- """
- )
- _ensure_table_schema(
- conn,
- "watched_playlists",
- EXPECTED_WATCHED_PLAYLISTS_COLUMNS,
- "watched playlists",
- )
-
- # Upgrade all dynamic playlist_ tables
- cur = conn.execute(
- "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'playlist_%'"
- )
- for row in cur.fetchall():
- table_name = row[0]
- conn.execute(
- f"""
- CREATE TABLE IF NOT EXISTS {table_name} (
- spotify_track_id TEXT PRIMARY KEY,
- title TEXT,
- artist_names TEXT,
- album_name TEXT,
- album_artist_names TEXT,
- track_number INTEGER,
- album_spotify_id TEXT,
- duration_ms INTEGER,
- added_at_playlist TEXT,
- added_to_db INTEGER,
- is_present_in_spotify INTEGER DEFAULT 1,
- last_seen_in_spotify INTEGER,
- snapshot_id TEXT,
- final_path TEXT
- )
- """
- )
- _ensure_table_schema(
- conn,
- table_name,
- EXPECTED_PLAYLIST_TRACKS_COLUMNS,
- f"playlist tracks ({table_name})",
- )
- except Exception:
- logger.error(
- "Failed to upgrade watch playlists DB to 3.2.0 base schema", exc_info=True
- )
-
-
-def _update_watch_artists_db(conn: sqlite3.Connection) -> None:
- try:
- # Ensure core watched_artists table exists and has expected schema
- conn.execute(
- """
- CREATE TABLE IF NOT EXISTS watched_artists (
- spotify_id TEXT PRIMARY KEY,
- name TEXT,
- link TEXT,
- total_albums_on_spotify INTEGER,
- last_checked INTEGER,
- added_at INTEGER,
- is_active INTEGER DEFAULT 1,
- genres TEXT,
- popularity INTEGER,
- image_url TEXT
- )
- """
- )
- _ensure_table_schema(
- conn, "watched_artists", EXPECTED_WATCHED_ARTISTS_COLUMNS, "watched artists"
- )
-
- # Upgrade all dynamic artist_ tables
- cur = conn.execute(
- "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'artist_%'"
- )
- for row in cur.fetchall():
- table_name = row[0]
- conn.execute(
- f"""
- CREATE TABLE IF NOT EXISTS {table_name} (
- album_spotify_id TEXT PRIMARY KEY,
- artist_spotify_id TEXT,
- name TEXT,
- album_group TEXT,
- album_type TEXT,
- release_date TEXT,
- release_date_precision TEXT,
- total_tracks INTEGER,
- link TEXT,
- image_url TEXT,
- added_to_db INTEGER,
- last_seen_on_spotify INTEGER,
- download_task_id TEXT,
- download_status INTEGER DEFAULT 0,
- is_fully_downloaded_managed_by_app INTEGER DEFAULT 0
- )
- """
- )
- _ensure_table_schema(
- conn,
- table_name,
- EXPECTED_ARTIST_ALBUMS_COLUMNS,
- f"artist albums ({table_name})",
- )
- except Exception:
- logger.error(
- "Failed to upgrade watch artists DB to 3.2.0 base schema", exc_info=True
- )
-
-
def _ensure_creds_filesystem() -> None:
try:
BLOBS_DIR.mkdir(parents=True, exist_ok=True)
@@ -374,35 +47,10 @@ def run_migrations_if_needed():
return
try:
- # Require instance to be at least 3.2.0 on history DB; otherwise abort
- with _safe_connect(HISTORY_DB) as history_conn:
- if history_conn and not _is_history_at_least_3_2_0(history_conn):
- logger.error(
- "Instance is not at schema version 3.2.0. Please upgrade to 3.2.0 before applying 3.3.0."
- )
- raise RuntimeError(
- "Instance is not at schema version 3.2.0. Please upgrade to 3.2.0 before applying 3.3.0."
- )
+ # Validate configuration version strictly at 3.3.0
+ MigrationV3_3_0.assert_config_version_is_3_3_0()
- # Watch playlists DB
- with _safe_connect(PLAYLISTS_DB) as conn:
- if conn:
- _update_watch_playlists_db(conn)
- # Apply 3.2.0 additions (batch progress columns)
- if not m320.check_watch_playlists(conn):
- m320.update_watch_playlists(conn)
- conn.commit()
-
- # Watch artists DB (if exists)
- if ARTISTS_DB.exists():
- with _safe_connect(ARTISTS_DB) as conn:
- if conn:
- _update_watch_artists_db(conn)
- if not m320.check_watch_artists(conn):
- m320.update_watch_artists(conn)
- conn.commit()
-
- # Accounts DB (no changes for this migration path)
+ # No schema changes in 3.3.0 path; just ensure Accounts DB can be opened
with _safe_connect(ACCOUNTS_DB) as conn:
if conn:
conn.commit()
@@ -412,5 +60,4 @@ def run_migrations_if_needed():
raise
else:
_ensure_creds_filesystem()
- log_noop_migration_detected()
- logger.info("Database migrations check completed (3.2.0 -> 3.3.0 path)")
+ logger.info("Migration validation completed (3.3.0 gate)")
diff --git a/routes/migrations/v3_2_0.py b/routes/migrations/v3_2_0.py
deleted file mode 100644
index 3849210..0000000
--- a/routes/migrations/v3_2_0.py
+++ /dev/null
@@ -1,100 +0,0 @@
-import sqlite3
-import logging
-
-logger = logging.getLogger(__name__)
-
-
-class MigrationV3_2_0:
- """
- Migration for version 3.2.0 (upgrade path 3.2.0 -> 3.3.0).
- - Adds per-item batch progress columns to Watch DBs to support page-by-interval processing.
- - Enforces prerequisite: previous instance version must be 3.1.2 (validated by runner).
- """
-
- # New columns to add to watched tables
- PLAYLISTS_ADDED_COLUMNS: dict[str, str] = {
- "batch_next_offset": "INTEGER DEFAULT 0",
- "batch_processing_snapshot_id": "TEXT",
- }
-
- ARTISTS_ADDED_COLUMNS: dict[str, str] = {
- "batch_next_offset": "INTEGER DEFAULT 0",
- }
-
- # --- No-op for history/accounts in 3.3.0 ---
-
- def check_history(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_history(self, conn: sqlite3.Connection) -> None:
- pass
-
- def check_accounts(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_accounts(self, conn: sqlite3.Connection) -> None:
- pass
-
- # --- Watch: playlists ---
-
- def check_watch_playlists(self, conn: sqlite3.Connection) -> bool:
- try:
- cur = conn.execute("PRAGMA table_info(watched_playlists)")
- cols = {row[1] for row in cur.fetchall()}
- return set(self.PLAYLISTS_ADDED_COLUMNS.keys()).issubset(cols)
- except sqlite3.OperationalError:
- # Table missing means not ready
- return False
-
- def update_watch_playlists(self, conn: sqlite3.Connection) -> None:
- # Add new columns if missing
- try:
- cur = conn.execute("PRAGMA table_info(watched_playlists)")
- existing = {row[1] for row in cur.fetchall()}
- for col_name, col_type in self.PLAYLISTS_ADDED_COLUMNS.items():
- if col_name in existing:
- continue
- try:
- conn.execute(
- f"ALTER TABLE watched_playlists ADD COLUMN {col_name} {col_type}"
- )
- logger.info(
- f"Added column '{col_name} {col_type}' to watched_playlists for 3.3.0 batch progress."
- )
- except sqlite3.OperationalError as e:
- logger.warning(
- f"Could not add column '{col_name}' to watched_playlists: {e}"
- )
- except Exception:
- logger.error("Failed to update watched_playlists for 3.3.0", exc_info=True)
-
- # --- Watch: artists ---
-
- def check_watch_artists(self, conn: sqlite3.Connection) -> bool:
- try:
- cur = conn.execute("PRAGMA table_info(watched_artists)")
- cols = {row[1] for row in cur.fetchall()}
- return set(self.ARTISTS_ADDED_COLUMNS.keys()).issubset(cols)
- except sqlite3.OperationalError:
- return False
-
- def update_watch_artists(self, conn: sqlite3.Connection) -> None:
- try:
- cur = conn.execute("PRAGMA table_info(watched_artists)")
- existing = {row[1] for row in cur.fetchall()}
- for col_name, col_type in self.ARTISTS_ADDED_COLUMNS.items():
- if col_name in existing:
- continue
- try:
- conn.execute(
- f"ALTER TABLE watched_artists ADD COLUMN {col_name} {col_type}"
- )
- logger.info(
- f"Added column '{col_name} {col_type}' to watched_artists for 3.3.0 batch progress."
- )
- except sqlite3.OperationalError as e:
- logger.warning(
- f"Could not add column '{col_name}' to watched_artists: {e}"
- )
- except Exception:
- logger.error("Failed to update watched_artists for 3.3.0", exc_info=True)
diff --git a/routes/migrations/v3_2_1.py b/routes/migrations/v3_2_1.py
deleted file mode 100644
index d8cad20..0000000
--- a/routes/migrations/v3_2_1.py
+++ /dev/null
@@ -1,41 +0,0 @@
-import logging
-import sqlite3
-
-logger = logging.getLogger(__name__)
-
-
-class MigrationV3_2_1:
- """
- No-op migration for version 3.2.1 (upgrade path 3.2.1 -> 3.3.0).
- No database schema changes are required.
- """
-
- def check_history(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_history(self, conn: sqlite3.Connection) -> None:
- pass
-
- def check_accounts(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_accounts(self, conn: sqlite3.Connection) -> None:
- pass
-
- def check_watch_playlists(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_watch_playlists(self, conn: sqlite3.Connection) -> None:
- pass
-
- def check_watch_artists(self, conn: sqlite3.Connection) -> bool:
- return True
-
- def update_watch_artists(self, conn: sqlite3.Connection) -> None:
- pass
-
-
-def log_noop_migration_detected() -> None:
- logger.info(
- "No migration performed: detected schema for 3.2.1; no changes needed for 3.2.1 -> 3.3.0."
- )
diff --git a/routes/migrations/v3_3_0.py b/routes/migrations/v3_3_0.py
new file mode 100644
index 0000000..b36a4b0
--- /dev/null
+++ b/routes/migrations/v3_3_0.py
@@ -0,0 +1,69 @@
+import json
+import logging
+from pathlib import Path
+from typing import Optional
+
+logger = logging.getLogger(__name__)
+
+
+CONFIG_PATH = Path("./data/config/main.json")
+REQUIRED_VERSION = "3.3.0"
+TARGET_VERSION = "3.3.1"
+
+
+def _load_config(config_path: Path) -> Optional[dict]:
+ try:
+ if not config_path.exists():
+ logger.error(f"Configuration file not found at {config_path}")
+ return None
+ content = config_path.read_text(encoding="utf-8")
+ return json.loads(content)
+ except Exception:
+ logger.error("Failed to read configuration file for migration", exc_info=True)
+ return None
+
+
+def _save_config(config_path: Path, cfg: dict) -> None:
+ config_path.parent.mkdir(parents=True, exist_ok=True)
+ config_path.write_text(json.dumps(cfg, indent=4) + "\n", encoding="utf-8")
+
+
+class MigrationV3_3_0:
+ """
+ 3.3.0 migration gate. This migration verifies the configuration indicates
+ version 3.3.0, then bumps it to 3.3.1.
+
+ If the `version` key is missing or not equal to 3.3.0, execution aborts and
+ prompts the user to update their instance to 3.3.0.
+ """
+
+ @staticmethod
+ def assert_config_version_is_3_3_0() -> None:
+ cfg = _load_config(CONFIG_PATH)
+ if not cfg or "version" not in cfg:
+ raise RuntimeError(
+ "Missing 'version' in data/config/main.json. Please update your configuration to 3.3.0."
+ )
+ version = str(cfg.get("version", "")).strip()
+ # Case 1: exactly 3.3.0 -> bump to 3.3.1
+ if version == REQUIRED_VERSION:
+ cfg["version"] = TARGET_VERSION
+ try:
+ _save_config(CONFIG_PATH, cfg)
+ logger.info(
+ f"Configuration version bumped from {REQUIRED_VERSION} to {TARGET_VERSION}."
+ )
+ except Exception:
+ logger.error(
+ "Failed to bump configuration version to 3.3.1", exc_info=True
+ )
+ raise
+ return
+ # Case 2: already 3.3.1 -> OK
+ if version == TARGET_VERSION:
+ logger.info("Configuration version 3.3.1 detected. Proceeding.")
+ return
+ # Case 3: anything else -> abort and instruct to update to 3.3.0 first
+ raise RuntimeError(
+ f"Unsupported configuration version '{version}'. Please update to {REQUIRED_VERSION}."
+ )
diff --git a/routes/system/config.py b/routes/system/config.py
index 95fead3..e86a2b1 100644
--- a/routes/system/config.py
+++ b/routes/system/config.py
@@ -40,6 +40,90 @@ NOTIFY_PARAMETERS = [
]
+# Helper functions to get final merged configs (simulate save without actually saving)
+def get_final_main_config(new_config_data: dict) -> dict:
+ """Returns the final main config that will be saved after merging with new_config_data."""
+ try:
+ # Load current or default config
+ existing_config = {}
+ if MAIN_CONFIG_FILE_PATH.exists():
+ with open(MAIN_CONFIG_FILE_PATH, "r") as f_read:
+ existing_config = json.load(f_read)
+ else:
+ existing_config = DEFAULT_MAIN_CONFIG.copy()
+
+ # Update with new data
+ for key, value in new_config_data.items():
+ existing_config[key] = value
+
+ # Migration: unify legacy keys to camelCase
+ _migrate_legacy_keys_inplace(existing_config)
+
+ # Ensure all default keys are still there
+ for default_key, default_value in DEFAULT_MAIN_CONFIG.items():
+ if default_key not in existing_config:
+ existing_config[default_key] = default_value
+
+ return existing_config
+ except Exception as e:
+ logger.error(f"Error creating final main config: {e}", exc_info=True)
+ return DEFAULT_MAIN_CONFIG.copy()
+
+
+def get_final_watch_config(new_watch_config_data: dict) -> dict:
+ """Returns the final watch config that will be saved after merging with new_watch_config_data."""
+ try:
+ # Load current main config
+ main_cfg: dict = {}
+ if WATCH_MAIN_CONFIG_FILE_PATH.exists():
+ with open(WATCH_MAIN_CONFIG_FILE_PATH, "r") as f:
+ main_cfg = json.load(f) or {}
+ else:
+ main_cfg = DEFAULT_MAIN_CONFIG.copy()
+
+ # Get and update watch config
+ watch_value = main_cfg.get("watch")
+ current_watch = (
+ watch_value.copy() if isinstance(watch_value, dict) else {}
+ ).copy()
+ current_watch.update(new_watch_config_data or {})
+
+ # Ensure defaults
+ for k, v in DEFAULT_WATCH_CONFIG.items():
+ if k not in current_watch:
+ current_watch[k] = v
+
+ return current_watch
+ except Exception as e:
+ logger.error(f"Error creating final watch config: {e}", exc_info=True)
+ return DEFAULT_WATCH_CONFIG.copy()
+
+
+def get_final_main_config_for_watch(new_watch_config_data: dict) -> dict:
+ """Returns the final main config when updating watch config."""
+ try:
+ # Load current main config
+ main_cfg: dict = {}
+ if WATCH_MAIN_CONFIG_FILE_PATH.exists():
+ with open(WATCH_MAIN_CONFIG_FILE_PATH, "r") as f:
+ main_cfg = json.load(f) or {}
+ else:
+ main_cfg = DEFAULT_MAIN_CONFIG.copy()
+
+ # Migrate legacy keys
+ _migrate_legacy_keys_inplace(main_cfg)
+
+ # Ensure all default keys are still there
+ for default_key, default_value in DEFAULT_MAIN_CONFIG.items():
+ if default_key not in main_cfg:
+ main_cfg[default_key] = default_value
+
+ return main_cfg
+ except Exception as e:
+ logger.error(f"Error creating final main config for watch: {e}", exc_info=True)
+ return DEFAULT_MAIN_CONFIG.copy()
+
+
# Helper function to check if credentials exist for a service
def has_credentials(service: str) -> bool:
"""Check if credentials exist for the specified service (spotify or deezer)."""
@@ -68,9 +152,12 @@ def validate_config(config_data: dict, watch_config: dict = None) -> tuple[bool,
Returns (is_valid, error_message).
"""
try:
- # Get current watch config if not provided
+ # Get final merged watch config for validation
if watch_config is None:
- watch_config = get_watch_config_http()
+ if "watch" in config_data:
+ watch_config = get_final_watch_config(config_data["watch"])
+ else:
+ watch_config = get_watch_config_http()
# Ensure realTimeMultiplier is a valid integer in range 0..10 if provided
if "realTimeMultiplier" in config_data or "real_time_multiplier" in config_data:
@@ -137,9 +224,9 @@ def validate_watch_config(
Returns (is_valid, error_message).
"""
try:
- # Get current main config if not provided
+ # Get final merged main config for validation
if main_config is None:
- main_config = get_config()
+ main_config = get_final_main_config_for_watch(watch_data)
# Check if trying to enable watch without download methods
if watch_data.get("enabled", False):
diff --git a/routes/system/progress.py b/routes/system/progress.py
index 016e3d9..812455a 100755
--- a/routes/system/progress.py
+++ b/routes/system/progress.py
@@ -4,11 +4,11 @@ import logging
import time
import json
import asyncio
-from typing import Set
+from typing import Set, Optional
import redis
import threading
-from routes.utils.celery_config import REDIS_URL
+from routes.utils.celery_config import REDIS_URL, get_config_params
from routes.utils.celery_tasks import (
get_task_info,
@@ -37,56 +37,122 @@ router = APIRouter()
class SSEBroadcaster:
def __init__(self):
self.clients: Set[asyncio.Queue] = set()
+ # Per-task throttling/batching/deduplication state
+ self._task_state = {} # task_id -> dict with last_sent, last_event, last_send_time, scheduled_handle
+ # Load configurable interval
+ config = get_config_params()
+ self.sse_update_interval = float(config.get("sseUpdateIntervalSeconds", 1))
async def add_client(self, queue: asyncio.Queue):
"""Add a new SSE client"""
self.clients.add(queue)
- logger.info(f"SSE: Client connected (total: {len(self.clients)})")
+ logger.debug(f"SSE: Client connected (total: {len(self.clients)})")
async def remove_client(self, queue: asyncio.Queue):
"""Remove an SSE client"""
self.clients.discard(queue)
- logger.info(f"SSE: Client disconnected (total: {len(self.clients)})")
+ logger.debug(f"SSE: Client disconnected (total: {len(self.clients)})")
async def broadcast_event(self, event_data: dict):
- """Broadcast an event to all connected clients"""
- logger.debug(
- f"SSE Broadcaster: Attempting to broadcast to {len(self.clients)} clients"
- )
-
+ """
+ Throttle, batch, and deduplicate SSE events per task.
+ Only emit at most 1 update/sec per task, aggregate within window, suppress redundant updates.
+ """
if not self.clients:
logger.debug("SSE Broadcaster: No clients connected, skipping broadcast")
return
+ # Defensive: always work with a list of tasks
+ tasks = event_data.get("tasks", [])
+ if not isinstance(tasks, list):
+ tasks = [tasks]
- # Add global task counts right before broadcasting - this is the single source of truth
+ # For each task, throttle/batch/dedupe
+ for task in tasks:
+ task_id = task.get("task_id")
+ if not task_id:
+ continue
+
+ now = time.time()
+ state = self._task_state.setdefault(task_id, {
+ "last_sent": None,
+ "last_event": None,
+ "last_send_time": 0,
+ "scheduled_handle": None,
+ })
+
+ # Deduplication: if event is identical to last sent, skip
+ if state["last_sent"] is not None and self._events_equal(state["last_sent"], task):
+ logger.debug(f"SSE: Deduped event for task {task_id}")
+ continue
+
+ # Throttling: if within interval, batch (store as last_event, schedule send)
+ elapsed = now - state["last_send_time"]
+ if elapsed < self.sse_update_interval:
+ state["last_event"] = task
+ if state["scheduled_handle"] is None:
+ delay = self.sse_update_interval - elapsed
+ loop = asyncio.get_event_loop()
+ state["scheduled_handle"] = loop.call_later(
+ delay, lambda: asyncio.create_task(self._send_batched_event(task_id))
+ )
+ continue
+
+ # Otherwise, send immediately
+ await self._send_event(task_id, task)
+ state["last_send_time"] = now
+ state["last_sent"] = task
+ state["last_event"] = None
+ if state["scheduled_handle"]:
+ state["scheduled_handle"].cancel()
+ state["scheduled_handle"] = None
+
+ async def _send_batched_event(self, task_id):
+ state = self._task_state.get(task_id)
+ if not state or not state["last_event"]:
+ return
+ await self._send_event(task_id, state["last_event"])
+ state["last_send_time"] = time.time()
+ state["last_sent"] = state["last_event"]
+ state["last_event"] = None
+ state["scheduled_handle"] = None
+
+ async def _send_event(self, task_id, task):
+ # Compose event_data for this task
+ event_data = {
+ "tasks": [task],
+ "current_timestamp": time.time(),
+ "change_type": "update",
+ }
enhanced_event_data = add_global_task_counts_to_event(event_data.copy())
+
event_json = json.dumps(enhanced_event_data)
sse_data = f"data: {event_json}\n\n"
- logger.debug(
- f"SSE Broadcaster: Broadcasting event: {enhanced_event_data.get('change_type', 'unknown')} with {enhanced_event_data.get('active_tasks', 0)} active tasks"
- )
-
- # Send to all clients, remove disconnected ones
disconnected = set()
sent_count = 0
for client_queue in self.clients.copy():
try:
await client_queue.put(sse_data)
sent_count += 1
- logger.debug("SSE: Successfully sent to client queue")
except Exception as e:
logger.error(f"SSE: Failed to send to client: {e}")
disconnected.add(client_queue)
-
- # Clean up disconnected clients
for client in disconnected:
self.clients.discard(client)
-
- logger.info(
- f"SSE Broadcaster: Successfully sent to {sent_count} clients, removed {len(disconnected)} disconnected clients"
+ logger.debug(
+ f"SSE Broadcaster: Sent throttled/batched event for task {task_id} to {sent_count} clients"
)
+ def _events_equal(self, a, b):
+ # Compare two task dicts for deduplication (ignore timestamps)
+ if not isinstance(a, dict) or not isinstance(b, dict):
+ return False
+ a_copy = dict(a)
+ b_copy = dict(b)
+ a_copy.pop("timestamp", None)
+ b_copy.pop("timestamp", None)
+ return a_copy == b_copy
+
# Global broadcaster instance
sse_broadcaster = SSEBroadcaster()
@@ -106,6 +172,10 @@ def start_sse_redis_subscriber():
pubsub.subscribe("sse_events")
logger.info("SSE Redis Subscriber: Started listening for events")
+ # Create a single event loop for this thread and reuse it
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+
for message in pubsub.listen():
if message["type"] == "message":
try:
@@ -119,52 +189,47 @@ def start_sse_redis_subscriber():
# Handle different event types
if event_type == "progress_update":
- # Transform callback data into task format expected by frontend
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- try:
- broadcast_data = loop.run_until_complete(
- transform_callback_to_task_format(
- task_id, event_data
- )
+ # Transform callback data into standardized update format expected by frontend
+ standardized = standardize_incoming_event(event_data)
+ if standardized:
+ loop.run_until_complete(
+ sse_broadcaster.broadcast_event(standardized)
)
- if broadcast_data:
+ logger.debug(
+ f"SSE Redis Subscriber: Broadcasted standardized progress update to {len(sse_broadcaster.clients)} clients"
+ )
+ elif event_type == "summary_update":
+ # Task summary update - use standardized trigger
+ # Short-circuit if task no longer exists to avoid expensive processing
+ try:
+ if not get_task_info(task_id):
+ logger.debug(
+ f"SSE Redis Subscriber: summary_update for missing task {task_id}, skipping"
+ )
+ else:
loop.run_until_complete(
- sse_broadcaster.broadcast_event(broadcast_data)
+ trigger_sse_update(
+ task_id, event_data.get("reason", "update")
+ )
)
logger.debug(
- f"SSE Redis Subscriber: Broadcasted callback to {len(sse_broadcaster.clients)} clients"
- )
- finally:
- loop.close()
- elif event_type == "summary_update":
- # Task summary update - use existing trigger_sse_update logic
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- try:
- loop.run_until_complete(
- trigger_sse_update(
- task_id, event_data.get("reason", "update")
+ f"SSE Redis Subscriber: Processed summary update for {task_id}"
)
+ except Exception as _e:
+ logger.error(
+ f"SSE Redis Subscriber: Error handling summary_update for {task_id}: {_e}",
+ exc_info=True,
)
- logger.debug(
- f"SSE Redis Subscriber: Processed summary update for {task_id}"
- )
- finally:
- loop.close()
else:
- # Unknown event type - broadcast as-is
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- try:
+ # Unknown event type - attempt to standardize and broadcast
+ standardized = standardize_incoming_event(event_data)
+ if standardized:
loop.run_until_complete(
- sse_broadcaster.broadcast_event(event_data)
+ sse_broadcaster.broadcast_event(standardized)
)
logger.debug(
- f"SSE Redis Subscriber: Broadcasted {event_type} to {len(sse_broadcaster.clients)} clients"
+ f"SSE Redis Subscriber: Broadcasted standardized {event_type} to {len(sse_broadcaster.clients)} clients"
)
- finally:
- loop.close()
except Exception as e:
logger.error(
@@ -178,7 +243,86 @@ def start_sse_redis_subscriber():
# Start Redis subscriber in background thread
thread = threading.Thread(target=redis_subscriber_thread, daemon=True)
thread.start()
- logger.info("SSE Redis Subscriber: Background thread started")
+ logger.debug("SSE Redis Subscriber: Background thread started")
+
+
+def build_task_object_from_callback(
+ task_id: str, callback_data: dict
+) -> Optional[dict]:
+ """Build a standardized task object from callback payload and task info."""
+ try:
+ task_info = get_task_info(task_id)
+ if not task_info:
+ return None
+ return {
+ "task_id": task_id,
+ "original_url": f"http://localhost:7171/api/{task_info.get('download_type', 'track')}/download/{task_info.get('url', '').split('/')[-1] if task_info.get('url') else ''}",
+ "last_line": callback_data,
+ "timestamp": time.time(),
+ "download_type": task_info.get("download_type", "track"),
+ "type": task_info.get("type", task_info.get("download_type", "track")),
+ "name": task_info.get("name", "Unknown"),
+ "artist": task_info.get("artist", ""),
+ "created_at": task_info.get("created_at"),
+ }
+ except Exception as e:
+ logger.error(
+ f"Error building task object from callback for {task_id}: {e}",
+ exc_info=True,
+ )
+ return None
+
+
+def standardize_incoming_event(event_data: dict) -> Optional[dict]:
+ """
+ Convert various incoming event shapes into a standardized SSE payload:
+ {
+ 'change_type': 'update' | 'heartbeat',
+ 'tasks': [...],
+ 'current_timestamp': float,
+ 'trigger_reason': str (optional)
+ }
+ """
+ try:
+ # Heartbeat passthrough (ensure tasks array exists)
+ if event_data.get("change_type") == "heartbeat":
+ return {
+ "change_type": "heartbeat",
+ "tasks": [],
+ "current_timestamp": time.time(),
+ }
+
+ # If already has tasks, just coerce change_type
+ if isinstance(event_data.get("tasks"), list):
+ return {
+ "change_type": event_data.get("change_type", "update"),
+ "tasks": event_data["tasks"],
+ "current_timestamp": time.time(),
+ "trigger_reason": event_data.get("trigger_reason"),
+ }
+
+ # If it's a callback-shaped event
+ callback_data = event_data.get("callback_data")
+ task_id = event_data.get("task_id")
+ if callback_data and task_id:
+ task_obj = build_task_object_from_callback(task_id, callback_data)
+ if task_obj:
+ return {
+ "change_type": "update",
+ "tasks": [task_obj],
+ "current_timestamp": time.time(),
+ "trigger_reason": event_data.get("event_type", "callback_update"),
+ }
+
+ # Fallback to empty update
+ return {
+ "change_type": "update",
+ "tasks": [],
+ "current_timestamp": time.time(),
+ }
+ except Exception as e:
+ logger.error(f"Failed to standardize incoming event: {e}", exc_info=True)
+ return None
async def transform_callback_to_task_format(task_id: str, event_data: dict) -> dict:
@@ -211,7 +355,7 @@ async def transform_callback_to_task_format(task_id: str, event_data: dict) -> d
# Build minimal event data - global counts will be added at broadcast time
return {
- "change_type": "update", # Use "update" so it gets processed by existing frontend logic
+ "change_type": "update",
"tasks": [task_object], # Frontend expects tasks array
"current_timestamp": time.time(),
"updated_count": 1,
@@ -239,7 +383,7 @@ async def trigger_sse_update(task_id: str, reason: str = "task_update"):
# Find the specific task that changed
task_info = get_task_info(task_id)
if not task_info:
- logger.warning(f"SSE: Task {task_id} not found for update")
+ logger.debug(f"SSE: Task {task_id} not found for update")
return
last_status = get_last_task_status(task_id)
@@ -254,12 +398,12 @@ async def trigger_sse_update(task_id: str, reason: str = "task_update"):
task_info, last_status, task_id, current_time, dummy_request
)
- # Create minimal event data - global counts will be added at broadcast time
+ # Create standardized event data - global counts will be added at broadcast time
event_data = {
"tasks": [task_response],
"current_timestamp": current_time,
"since_timestamp": current_time,
- "change_type": "realtime",
+ "change_type": "update",
"trigger_reason": reason,
}
@@ -420,6 +564,14 @@ def add_global_task_counts_to_event(event_data):
event_data["active_tasks"] = global_task_counts["active"]
event_data["all_tasks_count"] = sum(global_task_counts.values())
+ # Ensure tasks array is present for schema consistency
+ if "tasks" not in event_data:
+ event_data["tasks"] = []
+
+ # Ensure change_type is present
+ if "change_type" not in event_data:
+ event_data["change_type"] = "update"
+
return event_data
except Exception as e:
@@ -496,7 +648,11 @@ def _build_task_response(
try:
item_id = item_url.split("/")[-1]
if item_id:
- base_url = str(request.base_url).rstrip("/")
+ base_url = (
+ str(request.base_url).rstrip("/")
+ if request
+ else "http://localhost:7171"
+ )
dynamic_original_url = (
f"{base_url}/api/{download_type}/download/{item_id}"
)
@@ -575,7 +731,7 @@ def _build_task_response(
async def get_paginated_tasks(
- page=1, limit=20, active_only=False, request: Request = None
+ page=1, limit=20, active_only=False, request: Optional[Request] = None
):
"""
Get paginated list of tasks.
@@ -1069,51 +1225,18 @@ async def stream_task_updates(
try:
# Register this client with the broadcaster
- logger.info("SSE Stream: New client connecting...")
+ logger.debug("SSE Stream: New client connecting...")
await sse_broadcaster.add_client(client_queue)
- logger.info(
+ logger.debug(
f"SSE Stream: Client registered successfully, total clients: {len(sse_broadcaster.clients)}"
)
- # Send initial data immediately upon connection
+ # Send initial data immediately upon connection (standardized 'update')
initial_data = await generate_task_update_event(
time.time(), active_only, request
)
yield initial_data
- # Also send any active tasks as callback-style events to newly connected clients
- all_tasks = get_all_tasks()
- for task_summary in all_tasks:
- task_id = task_summary.get("task_id")
- if not task_id:
- continue
-
- task_info = get_task_info(task_id)
- if not task_info:
- continue
-
- last_status = get_last_task_status(task_id)
- task_status = get_task_status_from_last_status(last_status)
-
- # Send recent callback data for active or recently completed tasks
- if is_task_active(task_status) or (
- last_status and last_status.get("timestamp", 0) > time.time() - 30
- ):
- if last_status and "raw_callback" in last_status:
- callback_event = {
- "task_id": task_id,
- "callback_data": last_status["raw_callback"],
- "timestamp": last_status.get("timestamp", time.time()),
- "change_type": "callback",
- "event_type": "progress_update",
- "replay": True, # Mark as replay for client
- }
- event_json = json.dumps(callback_event)
- yield f"data: {event_json}\n\n"
- logger.info(
- f"SSE Stream: Sent replay callback for task {task_id}"
- )
-
# Send periodic heartbeats and listen for real-time events
last_heartbeat = time.time()
heartbeat_interval = 30.0
@@ -1180,6 +1303,7 @@ async def stream_task_updates(
+ task_counts["retrying"],
"task_counts": task_counts,
"change_type": "heartbeat",
+ "tasks": [],
}
event_json = json.dumps(heartbeat_data)
@@ -1194,13 +1318,14 @@ async def stream_task_updates(
"error": "Internal server error",
"timestamp": time.time(),
"change_type": "error",
+ "tasks": [],
}
)
yield f"data: {error_data}\n\n"
await asyncio.sleep(1)
except asyncio.CancelledError:
- logger.info("SSE client disconnected")
+ logger.debug("SSE client disconnected")
return
except Exception as e:
logger.error(f"SSE connection error: {e}", exc_info=True)
@@ -1296,6 +1421,7 @@ async def generate_task_update_event(
"current_timestamp": current_time,
"updated_count": len(updated_tasks),
"since_timestamp": since_timestamp,
+ "change_type": "update",
"initial": True, # Mark as initial load
}
@@ -1308,7 +1434,12 @@ async def generate_task_update_event(
except Exception as e:
logger.error(f"Error generating initial SSE event: {e}", exc_info=True)
error_data = json.dumps(
- {"error": "Failed to load initial data", "timestamp": time.time()}
+ {
+ "error": "Failed to load initial data",
+ "timestamp": time.time(),
+ "tasks": [],
+ "change_type": "error",
+ }
)
return f"data: {error_data}\n\n"
diff --git a/routes/utils/album.py b/routes/utils/album.py
index b6fb6e5..1e51966 100755
--- a/routes/utils/album.py
+++ b/routes/utils/album.py
@@ -8,6 +8,7 @@ from routes.utils.credentials import (
)
from routes.utils.celery_queue_manager import get_existing_task_id
from routes.utils.errors import DuplicateDownloadError
+from routes.utils.celery_config import get_config_params
def download_album(
@@ -98,10 +99,11 @@ def download_album(
spotify_client_id=global_spotify_client_id,
spotify_client_secret=global_spotify_client_secret,
progress_callback=progress_callback,
+ spotify_credentials_path=str(get_spotify_blob_path(main)),
)
dl.download_albumspo(
link_album=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality, # Deezer quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -159,7 +161,7 @@ def download_album(
)
spo.download_album(
link_album=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=fall_quality, # Spotify quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -216,7 +218,7 @@ def download_album(
)
spo.download_album(
link_album=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality,
recursive_download=False,
@@ -257,10 +259,15 @@ def download_album(
spotify_client_id=global_spotify_client_id, # Global Spotify keys
spotify_client_secret=global_spotify_client_secret, # Global Spotify keys
progress_callback=progress_callback,
+ spotify_credentials_path=(
+ str(get_spotify_blob_path(get_config_params().get("spotify")))
+ if get_config_params().get("spotify")
+ else None
+ ),
)
dl.download_albumdee( # Deezer URL, download via Deezer
link_album=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality,
recursive_download=False,
diff --git a/routes/utils/artist.py b/routes/utils/artist.py
index e08474e..c5e107b 100644
--- a/routes/utils/artist.py
+++ b/routes/utils/artist.py
@@ -2,9 +2,9 @@ import json
from routes.utils.watch.manager import get_watch_config
import logging
from routes.utils.celery_queue_manager import download_queue_manager
-from routes.utils.get_info import get_spotify_info
from routes.utils.credentials import get_credential, _get_global_spotify_api_creds
from routes.utils.errors import DuplicateDownloadError
+from routes.utils.get_info import get_client, get_artist
from deezspot.libutils.utils import get_ids, link_is_valid
@@ -77,10 +77,26 @@ def get_artist_discography(
log_json({"status": "error", "message": msg})
raise ValueError(msg)
+ # Fetch artist once and return grouped arrays without pagination
try:
- # Use the optimized get_spotify_info function
- discography = get_spotify_info(artist_id, "artist_discography")
- return discography
+ client = get_client()
+ artist_obj = get_artist(client, artist_id)
+
+ # Normalize groups as arrays of IDs; tolerate dict shape from some sources
+ def normalize_group(val):
+ if isinstance(val, list):
+ return val
+ if isinstance(val, dict):
+ items = val.get("items") or val.get("releases") or []
+ return items if isinstance(items, list) else []
+ return []
+
+ return {
+ "album_group": normalize_group(artist_obj.get("album_group")),
+ "single_group": normalize_group(artist_obj.get("single_group")),
+ "compilation_group": normalize_group(artist_obj.get("compilation_group")),
+ "appears_on_group": normalize_group(artist_obj.get("appears_on_group")),
+ }
except Exception as fetch_error:
msg = f"An error occurred while fetching the discography: {fetch_error}"
log_json({"status": "error", "message": msg})
@@ -120,61 +136,55 @@ def download_artist_albums(url, album_type=None, request_args=None, username=Non
raise ValueError(error_msg)
# Get watch config to determine which album groups to download
- watch_config = get_watch_config()
- allowed_groups = [
- g.lower()
- for g in watch_config.get("watchedArtistAlbumGroup", ["album", "single"])
- ]
+ valid_groups = {"album", "single", "compilation", "appears_on"}
+ if album_type and isinstance(album_type, str):
+ requested = [g.strip().lower() for g in album_type.split(",") if g.strip()]
+ allowed_groups = [g for g in requested if g in valid_groups]
+ if not allowed_groups:
+ logger.warning(
+ f"album_type query provided but no valid groups found in {requested}; falling back to watch config."
+ )
+ if not album_type or not isinstance(album_type, str) or not allowed_groups:
+ watch_config = get_watch_config()
+ allowed_groups = [
+ g.lower()
+ for g in watch_config.get("watchedArtistAlbumGroup", ["album", "single"])
+ if g.lower() in valid_groups
+ ]
logger.info(
- f"Filtering albums by watchedArtistAlbumGroup setting (exact album_group match): {allowed_groups}"
+ f"Filtering albums by album_type/watch setting (exact album_group match): {allowed_groups}"
)
- # Fetch all artist albums with pagination
+ # Fetch artist and aggregate group arrays without pagination
+ client = get_client()
+ artist_obj = get_artist(client, artist_id)
+
+ def normalize_group(val):
+ if isinstance(val, list):
+ return val
+ if isinstance(val, dict):
+ items = val.get("items") or val.get("releases") or []
+ return items if isinstance(items, list) else []
+ return []
+
+ group_key_to_type = [
+ ("album_group", "album"),
+ ("single_group", "single"),
+ ("compilation_group", "compilation"),
+ ("appears_on_group", "appears_on"),
+ ]
+
all_artist_albums = []
- offset = 0
- limit = 50 # Spotify API limit for artist albums
-
- logger.info(f"Fetching all albums for artist ID: {artist_id} with pagination")
-
- while True:
- logger.debug(
- f"Fetching albums for {artist_id}. Limit: {limit}, Offset: {offset}"
- )
- artist_data_page = get_spotify_info(
- artist_id, "artist_discography", limit=limit, offset=offset
- )
-
- if not artist_data_page or not isinstance(artist_data_page.get("items"), list):
- logger.warning(
- f"No album items found or invalid format for artist {artist_id} at offset {offset}. Response: {artist_data_page}"
+ for key, group_type in group_key_to_type:
+ ids = normalize_group(artist_obj.get(key))
+ # transform to minimal album objects with album_group tagging for filtering parity
+ for album_id in ids:
+ all_artist_albums.append(
+ {
+ "id": album_id,
+ "album_group": group_type,
+ }
)
- break
-
- current_page_albums = artist_data_page.get("items", [])
- if not current_page_albums:
- logger.info(
- f"No more albums on page for artist {artist_id} at offset {offset}. Total fetched so far: {len(all_artist_albums)}."
- )
- break
-
- logger.debug(
- f"Fetched {len(current_page_albums)} albums on current page for artist {artist_id}."
- )
- all_artist_albums.extend(current_page_albums)
-
- # Check if Spotify indicates a next page URL
- if artist_data_page.get("next"):
- offset += limit # Increment offset by the limit used for the request
- else:
- logger.info(
- f"No next page URL for artist {artist_id}. Pagination complete. Total albums fetched: {len(all_artist_albums)}."
- )
- break
-
- if not all_artist_albums:
- raise ValueError(
- f"Failed to retrieve artist data or no albums found for artist ID {artist_id}"
- )
# Filter albums based on the allowed types using album_group field (like in manager.py)
filtered_albums = []
@@ -201,13 +211,23 @@ def download_artist_albums(url, album_type=None, request_args=None, username=Non
duplicate_albums = []
for album in filtered_albums:
- album_url = album.get("external_urls", {}).get("spotify", "")
- album_name = album.get("name", "Unknown Album")
- album_artists = album.get("artists", [])
+ album_id = album.get("id")
+ if not album_id:
+ logger.warning("Skipping album without ID in filtered list.")
+ continue
+ # fetch album details to construct URL and names
+ try:
+ album_obj = download_queue_manager.client.get_album(
+ album_id, include_tracks=False
+ ) # type: ignore[attr-defined]
+ except AttributeError:
+ # If download_queue_manager lacks a client, fallback to shared client
+ album_obj = get_client().get_album(album_id, include_tracks=False)
+ album_url = album_obj.get("external_urls", {}).get("spotify", "")
+ album_name = album_obj.get("name", "Unknown Album")
+ artists = album_obj.get("artists", []) or []
album_artist = (
- album_artists[0].get("name", "Unknown Artist")
- if album_artists
- else "Unknown Artist"
+ artists[0].get("name", "Unknown Artist") if artists else "Unknown Artist"
)
if not album_url:
diff --git a/routes/utils/celery_config.py b/routes/utils/celery_config.py
index 7cd852b..d6e9f21 100644
--- a/routes/utils/celery_config.py
+++ b/routes/utils/celery_config.py
@@ -28,7 +28,7 @@ CONFIG_FILE_PATH = Path("./data/config/main.json")
DEFAULT_MAIN_CONFIG = {
"service": "spotify",
- "version": "3.3.0",
+ "version": "3.3.1",
"spotify": "",
"deezer": "",
"fallback": False,
@@ -40,6 +40,8 @@ DEFAULT_MAIN_CONFIG = {
"tracknumPadding": True,
"saveCover": True,
"maxConcurrentDownloads": 3,
+ "utilityConcurrency": 1,
+ "librespotConcurrency": 2,
"maxRetries": 3,
"retryDelaySeconds": 5,
"retryDelayIncrease": 5,
@@ -52,6 +54,7 @@ DEFAULT_MAIN_CONFIG = {
"watch": {},
"realTimeMultiplier": 0,
"padNumberWidth": 3,
+ "sseUpdateIntervalSeconds": 1, # Configurable SSE update interval (default: 1s)
}
@@ -188,7 +191,7 @@ task_annotations = {
"rate_limit": f"{MAX_CONCURRENT_DL}/m",
},
"routes.utils.celery_tasks.trigger_sse_update_task": {
- "rate_limit": "500/m", # Allow high rate for real-time SSE updates
+ "rate_limit": "60/m", # Throttle to 1 update/sec per task (matches SSE throttle)
"default_retry_delay": 1, # Quick retry for SSE updates
"max_retries": 1, # Limited retries for best-effort delivery
"ignore_result": True, # Don't store results for SSE tasks
diff --git a/routes/utils/celery_manager.py b/routes/utils/celery_manager.py
index 08b9727..d4489d1 100644
--- a/routes/utils/celery_manager.py
+++ b/routes/utils/celery_manager.py
@@ -2,10 +2,15 @@ import subprocess
import logging
import time
import threading
+import os
import sys
+from dotenv import load_dotenv
+
+load_dotenv()
+
# Import Celery task utilities
-from .celery_config import get_config_params, MAX_CONCURRENT_DL
+from .celery_config import get_config_params, MAX_CONCURRENT_DL # noqa: E402
# Configure logging
logger = logging.getLogger(__name__)
@@ -36,13 +41,22 @@ class CeleryManager:
self.concurrency = get_config_params().get(
"maxConcurrentDownloads", MAX_CONCURRENT_DL
)
+ self.utility_concurrency = max(
+ 1, int(get_config_params().get("utilityConcurrency", 1))
+ )
logger.info(
- f"CeleryManager initialized. Download concurrency set to: {self.concurrency}"
+ f"CeleryManager initialized. Download concurrency set to: {self.concurrency} | Utility concurrency: {self.utility_concurrency}"
)
def _get_worker_command(
- self, queues, concurrency, worker_name_suffix, log_level="INFO"
+ self, queues, concurrency, worker_name_suffix, log_level_env=None
):
+ # Use LOG_LEVEL from environment if provided, otherwise default to INFO
+ log_level = (
+ log_level_env
+ if log_level_env
+ else os.getenv("LOG_LEVEL", "WARNING").upper()
+ )
# Use a unique worker name to avoid conflicts.
# %h is replaced by celery with the actual hostname.
hostname = f"worker_{worker_name_suffix}@%h"
@@ -67,6 +81,12 @@ class CeleryManager:
logger.debug(f"Generated Celery command: {' '.join(command)}")
return command
+ def _get_worker_env(self):
+ # Inherit current environment, but set NO_CONSOLE_LOG=1 for subprocess
+ env = os.environ.copy()
+ env["NO_CONSOLE_LOG"] = "1"
+ return env
+
def _process_output_reader(self, stream, log_prefix, error=False):
logger.debug(f"Log reader thread started for {log_prefix}")
try:
@@ -123,6 +143,7 @@ class CeleryManager:
queues="downloads",
concurrency=self.concurrency,
worker_name_suffix="dlw", # Download Worker
+ log_level_env=os.getenv("LOG_LEVEL", "WARNING").upper(),
)
logger.info(
f"Starting Celery Download Worker with command: {' '.join(download_cmd)}"
@@ -134,6 +155,7 @@ class CeleryManager:
text=True,
bufsize=1,
universal_newlines=True,
+ env=self._get_worker_env(),
)
self.download_log_thread_stdout = threading.Thread(
target=self._process_output_reader,
@@ -153,11 +175,19 @@ class CeleryManager:
if self.utility_worker_process and self.utility_worker_process.poll() is None:
logger.info("Celery Utility Worker is already running.")
else:
+ self.utility_concurrency = max(
+ 1,
+ int(
+ get_config_params().get(
+ "utilityConcurrency", self.utility_concurrency
+ )
+ ),
+ )
utility_cmd = self._get_worker_command(
queues="utility_tasks,default", # Listen to utility and default
- concurrency=5, # Increased concurrency for SSE updates and utility tasks
+ concurrency=self.utility_concurrency,
worker_name_suffix="utw", # Utility Worker
- log_level="ERROR", # Reduce log verbosity for utility worker (only errors)
+ log_level_env=os.getenv("LOG_LEVEL", "WARNING").upper(),
)
logger.info(
f"Starting Celery Utility Worker with command: {' '.join(utility_cmd)}"
@@ -169,6 +199,7 @@ class CeleryManager:
text=True,
bufsize=1,
universal_newlines=True,
+ env=self._get_worker_env(),
)
self.utility_log_thread_stdout = threading.Thread(
target=self._process_output_reader,
@@ -181,7 +212,7 @@ class CeleryManager:
self.utility_log_thread_stdout.start()
self.utility_log_thread_stderr.start()
logger.info(
- f"Celery Utility Worker (PID: {self.utility_worker_process.pid}) started with concurrency 5."
+ f"Celery Utility Worker (PID: {self.utility_worker_process.pid}) started with concurrency {self.utility_concurrency}."
)
if (
@@ -205,7 +236,9 @@ class CeleryManager:
)
while not self.stop_event.is_set():
try:
- time.sleep(10) # Check every 10 seconds
+ # Wait using stop_event to be responsive to shutdown and respect interval
+ if self.stop_event.wait(CONFIG_CHECK_INTERVAL):
+ break
if self.stop_event.is_set():
break
@@ -213,6 +246,14 @@ class CeleryManager:
new_max_concurrent_downloads = current_config.get(
"maxConcurrentDownloads", self.concurrency
)
+ new_utility_concurrency = max(
+ 1,
+ int(
+ current_config.get(
+ "utilityConcurrency", self.utility_concurrency
+ )
+ ),
+ )
if new_max_concurrent_downloads != self.concurrency:
logger.info(
@@ -256,7 +297,10 @@ class CeleryManager:
# Restart only the download worker
download_cmd = self._get_worker_command(
- "downloads", self.concurrency, "dlw"
+ "downloads",
+ self.concurrency,
+ "dlw",
+ log_level_env=os.getenv("LOG_LEVEL", "WARNING").upper(),
)
logger.info(
f"Restarting Celery Download Worker with command: {' '.join(download_cmd)}"
@@ -287,6 +331,82 @@ class CeleryManager:
f"Celery Download Worker (PID: {self.download_worker_process.pid}) restarted with new concurrency {self.concurrency}."
)
+ # Handle utility worker concurrency changes
+ if new_utility_concurrency != self.utility_concurrency:
+ logger.info(
+ f"CeleryManager: Detected change in utilityConcurrency from {self.utility_concurrency} to {new_utility_concurrency}. Restarting utility worker only."
+ )
+
+ if (
+ self.utility_worker_process
+ and self.utility_worker_process.poll() is None
+ ):
+ logger.info(
+ f"Stopping Celery Utility Worker (PID: {self.utility_worker_process.pid}) for config update..."
+ )
+ self.utility_worker_process.terminate()
+ try:
+ self.utility_worker_process.wait(timeout=10)
+ logger.info(
+ f"Celery Utility Worker (PID: {self.utility_worker_process.pid}) terminated."
+ )
+ except subprocess.TimeoutExpired:
+ logger.warning(
+ f"Celery Utility Worker (PID: {self.utility_worker_process.pid}) did not terminate gracefully, killing."
+ )
+ self.utility_worker_process.kill()
+ self.utility_worker_process = None
+
+ # Wait for log threads of utility worker to finish
+ if (
+ self.utility_log_thread_stdout
+ and self.utility_log_thread_stdout.is_alive()
+ ):
+ self.utility_log_thread_stdout.join(timeout=5)
+ if (
+ self.utility_log_thread_stderr
+ and self.utility_log_thread_stderr.is_alive()
+ ):
+ self.utility_log_thread_stderr.join(timeout=5)
+
+ self.utility_concurrency = new_utility_concurrency
+
+ # Restart only the utility worker
+ utility_cmd = self._get_worker_command(
+ "utility_tasks,default",
+ self.utility_concurrency,
+ "utw",
+ log_level_env=os.getenv("LOG_LEVEL", "WARNING").upper(),
+ )
+ logger.info(
+ f"Restarting Celery Utility Worker with command: {' '.join(utility_cmd)}"
+ )
+ self.utility_worker_process = subprocess.Popen(
+ utility_cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ bufsize=1,
+ universal_newlines=True,
+ )
+ self.utility_log_thread_stdout = threading.Thread(
+ target=self._process_output_reader,
+ args=(self.utility_worker_process.stdout, "Celery[UW-STDOUT]"),
+ )
+ self.utility_log_thread_stderr = threading.Thread(
+ target=self._process_output_reader,
+ args=(
+ self.utility_worker_process.stderr,
+ "Celery[UW-STDERR]",
+ True,
+ ),
+ )
+ self.utility_log_thread_stdout.start()
+ self.utility_log_thread_stderr.start()
+ logger.info(
+ f"Celery Utility Worker (PID: {self.utility_worker_process.pid}) restarted with new concurrency {self.utility_concurrency}."
+ )
+
except Exception as e:
logger.error(
f"CeleryManager: Error in config monitor thread: {e}", exc_info=True
@@ -372,10 +492,7 @@ celery_manager = CeleryManager()
# Example of how to use the manager (typically called from your main app script)
if __name__ == "__main__":
- logging.basicConfig(
- level=logging.INFO,
- format="%(message)s",
- )
+ # Removed logging.basicConfig as it's handled by the main app's setup_logging
logger.info("Starting Celery Manager example...")
celery_manager.start()
try:
diff --git a/routes/utils/celery_queue_manager.py b/routes/utils/celery_queue_manager.py
index 10b47f1..4d0a378 100644
--- a/routes/utils/celery_queue_manager.py
+++ b/routes/utils/celery_queue_manager.py
@@ -246,7 +246,7 @@ class CeleryDownloadQueueManager:
"""Initialize the Celery-based download queue manager"""
self.max_concurrent = MAX_CONCURRENT_DL
self.paused = False
- print(
+ logger.info(
f"Celery Download Queue Manager initialized with max_concurrent={self.max_concurrent}"
)
diff --git a/routes/utils/celery_tasks.py b/routes/utils/celery_tasks.py
index 52853f5..9bec287 100644
--- a/routes/utils/celery_tasks.py
+++ b/routes/utils/celery_tasks.py
@@ -285,9 +285,16 @@ def setup_celery_logging(**kwargs):
"""
This handler ensures Celery uses our application logging settings
instead of its own. Prevents duplicate log configurations.
+ Also disables console logging if NO_CONSOLE_LOG=1 is set in the environment.
"""
- # Using the root logger's handlers and level preserves our config
- return logging.getLogger()
+ root_logger = logging.getLogger()
+ import os
+ if os.environ.get("NO_CONSOLE_LOG") == "1":
+ # Remove all StreamHandlers (console handlers) from the root logger
+ handlers_to_remove = [h for h in root_logger.handlers if isinstance(h, logging.StreamHandler)]
+ for h in handlers_to_remove:
+ root_logger.removeHandler(h)
+ return root_logger
# The initialization of a worker will log the worker configuration
diff --git a/routes/utils/get_info.py b/routes/utils/get_info.py
index df1384d..24e9ed0 100644
--- a/routes/utils/get_info.py
+++ b/routes/utils/get_info.py
@@ -1,422 +1,107 @@
-import spotipy
-from spotipy.oauth2 import SpotifyClientCredentials
-from routes.utils.credentials import _get_global_spotify_api_creds
-import logging
-import time
-from typing import Dict, Optional, Any
+import os
+from typing import Any, Dict, Optional
+import threading
-# Import Deezer API and logging
-from deezspot.deezloader.dee_api import API as DeezerAPI
+from deezspot.libutils import LibrespotClient
-# Initialize logger
-logger = logging.getLogger(__name__)
-
-# Global Spotify client instance for reuse
-_spotify_client = None
-_last_client_init = 0
-_client_init_interval = 3600 # Reinitialize client every hour
+# Config helpers to resolve active credentials
+from routes.utils.celery_config import get_config_params
+from routes.utils.credentials import get_spotify_blob_path
-def _get_spotify_client():
- """
- Get or create a Spotify client with global credentials.
- Implements client reuse and periodic reinitialization.
- """
- global _spotify_client, _last_client_init
+# -------- Shared Librespot client (process-wide) --------
- current_time = time.time()
+_shared_client: Optional[LibrespotClient] = None
+_shared_blob_path: Optional[str] = None
+_client_lock = threading.RLock()
- # Reinitialize client if it's been more than an hour or if client doesn't exist
- if (
- _spotify_client is None
- or current_time - _last_client_init > _client_init_interval
- ):
- client_id, client_secret = _get_global_spotify_api_creds()
- if not client_id or not client_secret:
- raise ValueError(
- "Global Spotify API client_id or client_secret not configured in ./data/creds/search.json."
- )
-
- # Create new client
- _spotify_client = spotipy.Spotify(
- client_credentials_manager=SpotifyClientCredentials(
- client_id=client_id, client_secret=client_secret
- )
+def _resolve_blob_path() -> str:
+ cfg = get_config_params() or {}
+ active_account = cfg.get("spotify")
+ if not active_account:
+ raise RuntimeError("Active Spotify account not set in configuration.")
+ blob_path = get_spotify_blob_path(active_account)
+ abs_path = os.path.abspath(str(blob_path))
+ if not os.path.isfile(abs_path):
+ raise FileNotFoundError(
+ f"Spotify credentials blob not found for account '{active_account}' at {abs_path}"
)
- _last_client_init = current_time
- logger.info("Spotify client initialized/reinitialized")
-
- return _spotify_client
+ return abs_path
-def _rate_limit_handler(func):
+def get_client() -> LibrespotClient:
"""
- Decorator to handle rate limiting with exponential backoff.
+ Return a shared LibrespotClient instance initialized from the active account blob.
+ Re-initializes if the active account changes.
"""
-
- def wrapper(*args, **kwargs):
- max_retries = 3
- base_delay = 1
-
- for attempt in range(max_retries):
+ global _shared_client, _shared_blob_path
+ with _client_lock:
+ desired_blob = _resolve_blob_path()
+ if _shared_client is None or _shared_blob_path != desired_blob:
try:
- return func(*args, **kwargs)
- except Exception as e:
- if "429" in str(e) or "rate limit" in str(e).lower():
- if attempt < max_retries - 1:
- delay = base_delay * (2**attempt)
- logger.warning(f"Rate limited, retrying in {delay} seconds...")
- time.sleep(delay)
- continue
- raise e
- return func(*args, **kwargs)
-
- return wrapper
+ if _shared_client is not None:
+ _shared_client.close()
+ except Exception:
+ pass
+ cfg = get_config_params() or {}
+ max_workers = int(cfg.get("librespotConcurrency", 2) or 2)
+ _shared_client = LibrespotClient(
+ stored_credentials_path=desired_blob, max_workers=max_workers
+ )
+ _shared_blob_path = desired_blob
+ return _shared_client
+
+
+# -------- Thin wrapper API (programmatic use) --------
+
+
+def create_client(credentials_path: str) -> LibrespotClient:
+ """
+ Create a LibrespotClient from a librespot-generated credentials.json file.
+ """
+ abs_path = os.path.abspath(credentials_path)
+ if not os.path.isfile(abs_path):
+ raise FileNotFoundError(f"Credentials file not found: {abs_path}")
+ cfg = get_config_params() or {}
+ max_workers = int(cfg.get("librespotConcurrency", 2) or 2)
+ return LibrespotClient(stored_credentials_path=abs_path, max_workers=max_workers)
+
+
+def close_client(client: LibrespotClient) -> None:
+ """
+ Dispose a LibrespotClient instance.
+ """
+ client.close()
+
+
+def get_track(client: LibrespotClient, track_in: str) -> Dict[str, Any]:
+ """Fetch a track object."""
+ return client.get_track(track_in)
+
+
+def get_album(
+ client: LibrespotClient, album_in: str, include_tracks: bool = False
+) -> Dict[str, Any]:
+ """Fetch an album object; optionally include expanded tracks."""
+ return client.get_album(album_in, include_tracks=include_tracks)
+
+
+def get_artist(client: LibrespotClient, artist_in: str) -> Dict[str, Any]:
+ """Fetch an artist object."""
+ return client.get_artist(artist_in)
+
+
+def get_playlist(
+ client: LibrespotClient, playlist_in: str, expand_items: bool = False
+) -> Dict[str, Any]:
+ """Fetch a playlist object; optionally expand track items to full track objects."""
+ return client.get_playlist(playlist_in, expand_items=expand_items)
-@_rate_limit_handler
def get_playlist_metadata(playlist_id: str) -> Dict[str, Any]:
"""
- Get playlist metadata only (no tracks) to avoid rate limiting.
-
- Args:
- playlist_id: The Spotify playlist ID
-
- Returns:
- Dictionary with playlist metadata (name, description, owner, etc.)
+ Fetch playlist metadata using the shared client without expanding items.
"""
- client = _get_spotify_client()
-
- try:
- # Get basic playlist info without tracks
- playlist = client.playlist(
- playlist_id,
- fields="id,name,description,owner,images,snapshot_id,public,followers,tracks.total",
- )
-
- # Add a flag to indicate this is metadata only
- playlist["_metadata_only"] = True
- playlist["_tracks_loaded"] = False
-
- logger.debug(
- f"Retrieved playlist metadata for {playlist_id}: {playlist.get('name', 'Unknown')}"
- )
- return playlist
-
- except Exception as e:
- logger.error(f"Error fetching playlist metadata for {playlist_id}: {e}")
- raise
-
-
-@_rate_limit_handler
-def get_playlist_tracks(
- playlist_id: str, limit: int = 100, offset: int = 0
-) -> Dict[str, Any]:
- """
- Get playlist tracks with pagination support to handle large playlists efficiently.
-
- Args:
- playlist_id: The Spotify playlist ID
- limit: Number of tracks to fetch per request (max 100)
- offset: Starting position for pagination
-
- Returns:
- Dictionary with tracks data
- """
- client = _get_spotify_client()
-
- try:
- # Get tracks with specified limit and offset
- tracks_data = client.playlist_tracks(
- playlist_id,
- limit=min(limit, 100), # Spotify API max is 100
- offset=offset,
- fields="items(track(id,name,artists,album,external_urls,preview_url,duration_ms,explicit,popularity)),total,limit,offset",
- )
-
- logger.debug(
- f"Retrieved {len(tracks_data.get('items', []))} tracks for playlist {playlist_id} (offset: {offset})"
- )
- return tracks_data
-
- except Exception as e:
- logger.error(f"Error fetching playlist tracks for {playlist_id}: {e}")
- raise
-
-
-@_rate_limit_handler
-def get_playlist_full(playlist_id: str, batch_size: int = 100) -> Dict[str, Any]:
- """
- Get complete playlist data with all tracks, using batched requests to avoid rate limiting.
-
- Args:
- playlist_id: The Spotify playlist ID
- batch_size: Number of tracks to fetch per batch (max 100)
-
- Returns:
- Complete playlist data with all tracks
- """
- try:
- # First get metadata
- playlist = get_playlist_metadata(playlist_id)
-
- # Get total track count
- total_tracks = playlist.get("tracks", {}).get("total", 0)
-
- if total_tracks == 0:
- playlist["tracks"] = {"items": [], "total": 0}
- return playlist
-
- # Fetch all tracks in batches
- all_tracks = []
- offset = 0
-
- while offset < total_tracks:
- batch = get_playlist_tracks(playlist_id, limit=batch_size, offset=offset)
- batch_items = batch.get("items", [])
- all_tracks.extend(batch_items)
-
- offset += len(batch_items)
-
- # Add small delay between batches to be respectful to API
- if offset < total_tracks:
- time.sleep(0.1)
-
- # Update playlist with complete tracks data
- playlist["tracks"] = {
- "items": all_tracks,
- "total": total_tracks,
- "limit": batch_size,
- "offset": 0,
- }
- playlist["_metadata_only"] = False
- playlist["_tracks_loaded"] = True
-
- logger.info(
- f"Retrieved complete playlist {playlist_id} with {total_tracks} tracks"
- )
- return playlist
-
- except Exception as e:
- logger.error(f"Error fetching complete playlist {playlist_id}: {e}")
- raise
-
-
-def check_playlist_updated(playlist_id: str, last_snapshot_id: str) -> bool:
- """
- Check if playlist has been updated by comparing snapshot_id.
- This is much more efficient than fetching all tracks.
-
- Args:
- playlist_id: The Spotify playlist ID
- last_snapshot_id: The last known snapshot_id
-
- Returns:
- True if playlist has been updated, False otherwise
- """
- try:
- metadata = get_playlist_metadata(playlist_id)
- current_snapshot_id = metadata.get("snapshot_id")
-
- return current_snapshot_id != last_snapshot_id
-
- except Exception as e:
- logger.error(f"Error checking playlist update status for {playlist_id}: {e}")
- raise
-
-
-@_rate_limit_handler
-def get_spotify_info(
- spotify_id: str,
- spotify_type: str,
- limit: Optional[int] = None,
- offset: Optional[int] = None,
-) -> Dict[str, Any]:
- """
- Get info from Spotify API using Spotipy directly.
- Optimized to prevent rate limiting by using appropriate endpoints.
-
- Args:
- spotify_id: The Spotify ID of the entity
- spotify_type: The type of entity (track, album, playlist, artist, artist_discography, episode, album_tracks)
- limit (int, optional): The maximum number of items to return. Used for pagination.
- offset (int, optional): The index of the first item to return. Used for pagination.
-
- Returns:
- Dictionary with the entity information
- """
- client = _get_spotify_client()
-
- try:
- if spotify_type == "track":
- return client.track(spotify_id)
-
- elif spotify_type == "album":
- return client.album(spotify_id)
-
- elif spotify_type == "album_tracks":
- # Fetch album's tracks with pagination support
- return client.album_tracks(
- spotify_id, limit=limit or 20, offset=offset or 0
- )
-
- elif spotify_type == "playlist":
- # Use optimized playlist fetching
- return get_playlist_full(spotify_id)
-
- elif spotify_type == "playlist_metadata":
- # Get only metadata for playlists
- return get_playlist_metadata(spotify_id)
-
- elif spotify_type == "artist":
- return client.artist(spotify_id)
-
- elif spotify_type == "artist_discography":
- # Get artist's albums with pagination
- albums = client.artist_albums(
- spotify_id,
- limit=limit or 20,
- offset=offset or 0,
- include_groups="single,album,appears_on",
- )
- return albums
-
- elif spotify_type == "episode":
- return client.episode(spotify_id)
-
- else:
- raise ValueError(f"Unsupported Spotify type: {spotify_type}")
-
- except Exception as e:
- logger.error(f"Error fetching {spotify_type} {spotify_id}: {e}")
- raise
-
-
-# Cache for playlist metadata to reduce API calls
-_playlist_metadata_cache: Dict[str, tuple[Dict[str, Any], float]] = {}
-_cache_ttl = 300 # 5 minutes cache
-
-
-def get_cached_playlist_metadata(playlist_id: str) -> Optional[Dict[str, Any]]:
- """
- Get playlist metadata from cache if available and not expired.
-
- Args:
- playlist_id: The Spotify playlist ID
-
- Returns:
- Cached metadata or None if not available/expired
- """
- if playlist_id in _playlist_metadata_cache:
- cached_data, timestamp = _playlist_metadata_cache[playlist_id]
- if time.time() - timestamp < _cache_ttl:
- return cached_data
-
- return None
-
-
-def cache_playlist_metadata(playlist_id: str, metadata: Dict[str, Any]):
- """
- Cache playlist metadata with timestamp.
-
- Args:
- playlist_id: The Spotify playlist ID
- metadata: The metadata to cache
- """
- _playlist_metadata_cache[playlist_id] = (metadata, time.time())
-
-
-def get_playlist_info_optimized(
- playlist_id: str, include_tracks: bool = False
-) -> Dict[str, Any]:
- """
- Optimized playlist info function that uses caching and selective loading.
-
- Args:
- playlist_id: The Spotify playlist ID
- include_tracks: Whether to include track data (default: False to save API calls)
-
- Returns:
- Playlist data with or without tracks
- """
- # Check cache first
- cached_metadata = get_cached_playlist_metadata(playlist_id)
-
- if cached_metadata and not include_tracks:
- logger.debug(f"Returning cached metadata for playlist {playlist_id}")
- return cached_metadata
-
- if include_tracks:
- # Get complete playlist data
- playlist_data = get_playlist_full(playlist_id)
- # Cache the metadata portion
- metadata_only = {k: v for k, v in playlist_data.items() if k != "tracks"}
- metadata_only["_metadata_only"] = True
- metadata_only["_tracks_loaded"] = False
- cache_playlist_metadata(playlist_id, metadata_only)
- return playlist_data
- else:
- # Get metadata only
- metadata = get_playlist_metadata(playlist_id)
- cache_playlist_metadata(playlist_id, metadata)
- return metadata
-
-
-# Keep the existing Deezer functions unchanged
-def get_deezer_info(deezer_id, deezer_type, limit=None):
- """
- Get info from Deezer API.
-
- Args:
- deezer_id: The Deezer ID of the entity.
- deezer_type: The type of entity (track, album, playlist, artist, episode,
- artist_top_tracks, artist_albums, artist_related,
- artist_radio, artist_playlists).
- limit (int, optional): The maximum number of items to return. Used for
- artist_top_tracks, artist_albums, artist_playlists.
- Deezer API methods usually have their own defaults (e.g., 25)
- if limit is not provided or None is passed to them.
-
- Returns:
- Dictionary with the entity information.
- Raises:
- ValueError: If deezer_type is unsupported.
- Various exceptions from DeezerAPI (NoDataApi, QuotaExceeded, requests.exceptions.RequestException, etc.)
- """
- logger.debug(
- f"Fetching Deezer info for ID {deezer_id}, type {deezer_type}, limit {limit}"
- )
-
- # DeezerAPI uses class methods; its @classmethod __init__ handles setup.
- # No specific ARL or account handling here as DeezerAPI seems to use general endpoints.
-
- if deezer_type == "track":
- return DeezerAPI.get_track(deezer_id)
- elif deezer_type == "album":
- return DeezerAPI.get_album(deezer_id)
- elif deezer_type == "playlist":
- return DeezerAPI.get_playlist(deezer_id)
- elif deezer_type == "artist":
- return DeezerAPI.get_artist(deezer_id)
- elif deezer_type == "episode":
- return DeezerAPI.get_episode(deezer_id)
- elif deezer_type == "artist_top_tracks":
- if limit is not None:
- return DeezerAPI.get_artist_top_tracks(deezer_id, limit=limit)
- return DeezerAPI.get_artist_top_tracks(deezer_id) # Use API default limit
- elif deezer_type == "artist_albums": # Maps to get_artist_top_albums
- if limit is not None:
- return DeezerAPI.get_artist_top_albums(deezer_id, limit=limit)
- return DeezerAPI.get_artist_top_albums(deezer_id) # Use API default limit
- elif deezer_type == "artist_related":
- return DeezerAPI.get_artist_related(deezer_id)
- elif deezer_type == "artist_radio":
- return DeezerAPI.get_artist_radio(deezer_id)
- elif deezer_type == "artist_playlists":
- if limit is not None:
- return DeezerAPI.get_artist_top_playlists(deezer_id, limit=limit)
- return DeezerAPI.get_artist_top_playlists(deezer_id) # Use API default limit
- else:
- logger.error(f"Unsupported Deezer type: {deezer_type}")
- raise ValueError(f"Unsupported Deezer type: {deezer_type}")
+ client = get_client()
+ return get_playlist(client, playlist_id, expand_items=False)
diff --git a/routes/utils/playlist.py b/routes/utils/playlist.py
index b19bd7c..4410b22 100755
--- a/routes/utils/playlist.py
+++ b/routes/utils/playlist.py
@@ -3,6 +3,8 @@ from deezspot.spotloader import SpoLogin
from deezspot.deezloader import DeeLogin
from pathlib import Path
from routes.utils.credentials import get_credential, _get_global_spotify_api_creds
+from routes.utils.credentials import get_spotify_blob_path
+from routes.utils.celery_config import get_config_params
from routes.utils.celery_queue_manager import get_existing_task_id
from routes.utils.errors import DuplicateDownloadError
@@ -95,10 +97,11 @@ def download_playlist(
spotify_client_id=global_spotify_client_id,
spotify_client_secret=global_spotify_client_secret,
progress_callback=progress_callback,
+ spotify_credentials_path=str(get_spotify_blob_path(main)),
)
dl.download_playlistspo(
link_playlist=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality, # Deezer quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -161,7 +164,7 @@ def download_playlist(
)
spo.download_playlist(
link_playlist=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=fall_quality, # Spotify quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -224,7 +227,7 @@ def download_playlist(
)
spo.download_playlist(
link_playlist=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality,
recursive_download=False,
@@ -265,10 +268,15 @@ def download_playlist(
spotify_client_id=global_spotify_client_id, # Global Spotify keys
spotify_client_secret=global_spotify_client_secret, # Global Spotify keys
progress_callback=progress_callback,
+ spotify_credentials_path=(
+ str(get_spotify_blob_path(get_config_params().get("spotify")))
+ if get_config_params().get("spotify")
+ else None
+ ),
)
dl.download_playlistdee( # Deezer URL, download via Deezer
link_playlist=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality, # Usually False for playlists to get individual track qualities
recursive_download=False,
diff --git a/routes/utils/track.py b/routes/utils/track.py
index 7499d31..68b7569 100755
--- a/routes/utils/track.py
+++ b/routes/utils/track.py
@@ -6,6 +6,7 @@ from routes.utils.credentials import (
_get_global_spotify_api_creds,
get_spotify_blob_path,
)
+from routes.utils.celery_config import get_config_params
def download_track(
@@ -90,11 +91,12 @@ def download_track(
spotify_client_id=global_spotify_client_id, # Global creds
spotify_client_secret=global_spotify_client_secret, # Global creds
progress_callback=progress_callback,
+ spotify_credentials_path=str(get_spotify_blob_path(main)),
)
# download_trackspo means: Spotify URL, download via Deezer
dl.download_trackspo(
link_track=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality, # Deezer quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -153,7 +155,7 @@ def download_track(
)
spo.download_track(
link_track=url, # Spotify URL
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=fall_quality, # Spotify quality
recursive_quality=recursive_quality,
recursive_download=False,
@@ -169,7 +171,6 @@ def download_track(
convert_to=convert_to,
bitrate=bitrate,
artist_separator=artist_separator,
- spotify_metadata=spotify_metadata,
pad_number_width=pad_number_width,
)
print(
@@ -211,7 +212,7 @@ def download_track(
)
spo.download_track(
link_track=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality,
recursive_download=False,
@@ -251,10 +252,15 @@ def download_track(
spotify_client_id=global_spotify_client_id, # Global Spotify keys for internal Spo use by DeeLogin
spotify_client_secret=global_spotify_client_secret, # Global Spotify keys
progress_callback=progress_callback,
+ spotify_credentials_path=(
+ str(get_spotify_blob_path(get_config_params().get("spotify")))
+ if get_config_params().get("spotify")
+ else None
+ ),
)
dl.download_trackdee( # Deezer URL, download via Deezer
link_track=url,
- output_dir="/app/downloads",
+ output_dir="./downloads",
quality_download=quality,
recursive_quality=recursive_quality,
recursive_download=False,
diff --git a/routes/utils/watch/manager.py b/routes/utils/watch/manager.py
index a4e7aa6..f441487 100644
--- a/routes/utils/watch/manager.py
+++ b/routes/utils/watch/manager.py
@@ -27,15 +27,9 @@ from routes.utils.watch.db import (
get_artist_batch_next_offset,
set_artist_batch_next_offset,
)
-from routes.utils.get_info import (
- get_spotify_info,
- get_playlist_metadata,
- get_playlist_tracks,
-) # To fetch playlist, track, artist, and album details
-from routes.utils.celery_queue_manager import download_queue_manager
-# Added import to fetch base formatting config
-from routes.utils.celery_queue_manager import get_config_params
+from routes.utils.celery_queue_manager import download_queue_manager, get_config_params
+from routes.utils.get_info import get_client
logger = logging.getLogger(__name__)
MAIN_CONFIG_FILE_PATH = Path("./data/config/main.json")
@@ -173,6 +167,46 @@ def get_watch_config():
watch_cfg["maxItemsPerRun"] = clamped_value
migrated = True
+ # Enforce sane ranges and types for poll/delay intervals to prevent tight loops
+ def _safe_int(value, default):
+ try:
+ return int(value)
+ except Exception:
+ return default
+
+ # Clamp poll interval to at least 1 second
+ poll_val = _safe_int(
+ watch_cfg.get(
+ "watchPollIntervalSeconds",
+ DEFAULT_WATCH_CONFIG["watchPollIntervalSeconds"],
+ ),
+ DEFAULT_WATCH_CONFIG["watchPollIntervalSeconds"],
+ )
+ if poll_val < 1:
+ watch_cfg["watchPollIntervalSeconds"] = 1
+ migrated = True
+ # Clamp per-item delays to at least 1 second
+ delay_pl = _safe_int(
+ watch_cfg.get(
+ "delayBetweenPlaylistsSeconds",
+ DEFAULT_WATCH_CONFIG["delayBetweenPlaylistsSeconds"],
+ ),
+ DEFAULT_WATCH_CONFIG["delayBetweenPlaylistsSeconds"],
+ )
+ if delay_pl < 1:
+ watch_cfg["delayBetweenPlaylistsSeconds"] = 1
+ migrated = True
+ delay_ar = _safe_int(
+ watch_cfg.get(
+ "delayBetweenArtistsSeconds",
+ DEFAULT_WATCH_CONFIG["delayBetweenArtistsSeconds"],
+ ),
+ DEFAULT_WATCH_CONFIG["delayBetweenArtistsSeconds"],
+ )
+ if delay_ar < 1:
+ watch_cfg["delayBetweenArtistsSeconds"] = 1
+ migrated = True
+
if migrated or legacy_file_found:
# Persist migration back to main.json
main_cfg["watch"] = watch_cfg
@@ -358,7 +392,7 @@ def find_tracks_in_playlist(
while not_found_tracks and offset < 10000: # Safety limit
try:
- tracks_batch = get_playlist_tracks(
+ tracks_batch = _fetch_playlist_tracks_page(
playlist_spotify_id, limit=limit, offset=offset
)
@@ -459,7 +493,9 @@ def check_watched_playlists(specific_playlist_id: str = None):
ensure_playlist_table_schema(playlist_spotify_id)
# First, get playlist metadata to check if it has changed
- current_playlist_metadata = get_playlist_metadata(playlist_spotify_id)
+ current_playlist_metadata = _fetch_playlist_metadata(
+ playlist_spotify_id
+ )
if not current_playlist_metadata:
logger.error(
f"Playlist Watch Manager: Failed to fetch metadata from Spotify for playlist {playlist_spotify_id}."
@@ -507,7 +543,7 @@ def check_watched_playlists(specific_playlist_id: str = None):
progress_offset, _ = get_playlist_batch_progress(
playlist_spotify_id
)
- tracks_batch = get_playlist_tracks(
+ tracks_batch = _fetch_playlist_tracks_page(
playlist_spotify_id,
limit=batch_limit,
offset=progress_offset,
@@ -573,7 +609,7 @@ def check_watched_playlists(specific_playlist_id: str = None):
logger.info(
f"Playlist Watch Manager: Fetching one batch (limit={batch_limit}, offset={progress_offset}) for playlist '{playlist_name}'."
)
- tracks_batch = get_playlist_tracks(
+ tracks_batch = _fetch_playlist_tracks_page(
playlist_spotify_id, limit=batch_limit, offset=progress_offset
)
batch_items = tracks_batch.get("items", []) if tracks_batch else []
@@ -674,7 +710,9 @@ def check_watched_playlists(specific_playlist_id: str = None):
# Only sleep between items when running a batch (no specific ID)
if not specific_playlist_id:
- time.sleep(max(1, config.get("delayBetweenPlaylistsSeconds", 2)))
+ time.sleep(
+ max(1, _safe_to_int(config.get("delayBetweenPlaylistsSeconds"), 2))
+ )
logger.info("Playlist Watch Manager: Finished checking all watched playlists.")
@@ -734,8 +772,8 @@ def check_watched_artists(specific_artist_id: str = None):
logger.debug(
f"Artist Watch Manager: Fetching albums for {artist_spotify_id}. Limit: {limit}, Offset: {offset}"
)
- artist_albums_page = get_spotify_info(
- artist_spotify_id, "artist_discography", limit=limit, offset=offset
+ artist_albums_page = _fetch_artist_discography_page(
+ artist_spotify_id, limit=limit, offset=offset
)
current_page_albums = (
@@ -821,7 +859,9 @@ def check_watched_artists(specific_artist_id: str = None):
# Only sleep between items when running a batch (no specific ID)
if not specific_artist_id:
- time.sleep(max(1, config.get("delayBetweenArtistsSeconds", 5)))
+ time.sleep(
+ max(1, _safe_to_int(config.get("delayBetweenArtistsSeconds"), 5))
+ )
logger.info("Artist Watch Manager: Finished checking all watched artists.")
@@ -836,6 +876,14 @@ def playlist_watch_scheduler():
interval = current_config.get("watchPollIntervalSeconds", 3600)
watch_enabled = current_config.get("enabled", False) # Get enabled status
+ # Ensure interval is a positive integer to avoid tight loops
+ try:
+ interval = int(interval)
+ except Exception:
+ interval = 3600
+ if interval < 1:
+ interval = 1
+
if not watch_enabled:
logger.info(
"Watch Scheduler: Watch feature is disabled in config. Skipping checks."
@@ -911,7 +959,15 @@ def run_playlist_check_over_intervals(playlist_spotify_id: str) -> None:
# Determine if we are done: no active processing snapshot and no pending sync
cfg = get_watch_config()
interval = cfg.get("watchPollIntervalSeconds", 3600)
- metadata = get_playlist_metadata(playlist_spotify_id)
+ # Ensure interval is a positive integer
+ try:
+ interval = int(interval)
+ except Exception:
+ interval = 3600
+ if interval < 1:
+ interval = 1
+ # Use local helper that leverages Librespot client
+ metadata = _fetch_playlist_metadata(playlist_spotify_id)
if not metadata:
logger.warning(
f"Manual Playlist Runner: Could not load metadata for {playlist_spotify_id}. Stopping."
@@ -1098,7 +1154,7 @@ def update_playlist_m3u_file(playlist_spotify_id: str):
# Get configuration settings
output_dir = (
- "/app/downloads" # This matches the output_dir used in download functions
+ "./downloads" # This matches the output_dir used in download functions
)
# Get all tracks for the playlist
@@ -1125,14 +1181,14 @@ def update_playlist_m3u_file(playlist_spotify_id: str):
skipped_missing_final_path = 0
for track in tracks:
- # Use final_path from deezspot summary and convert from /app/downloads to ../ relative path
+ # Use final_path from deezspot summary and convert from ./downloads to ../ relative path
final_path = track.get("final_path")
if not final_path:
skipped_missing_final_path += 1
continue
normalized = str(final_path).replace("\\", "/")
- if normalized.startswith("/app/downloads/"):
- relative_path = normalized.replace("/app/downloads/", "../", 1)
+ if normalized.startswith("./downloads/"):
+ relative_path = normalized.replace("./downloads/", "../", 1)
elif "/downloads/" in normalized.lower():
idx = normalized.lower().rfind("/downloads/")
relative_path = "../" + normalized[idx + len("/downloads/") :]
@@ -1167,3 +1223,119 @@ def update_playlist_m3u_file(playlist_spotify_id: str):
f"Error updating m3u file for playlist {playlist_spotify_id}: {e}",
exc_info=True,
)
+
+
+# Helper to build a Librespot client from active account
+
+
+# Add a small internal helper for safe int conversion
+_def_safe_int_added = True
+
+
+def _safe_to_int(value, default):
+ try:
+ return int(value)
+ except Exception:
+ return default
+
+
+def _build_librespot_client():
+ try:
+ # Reuse shared client managed in routes.utils.get_info
+ return get_client()
+ except Exception as e:
+ raise RuntimeError(f"Failed to initialize Librespot client: {e}")
+
+
+def _fetch_playlist_metadata(playlist_id: str) -> dict:
+ client = _build_librespot_client()
+ return client.get_playlist(playlist_id, expand_items=False)
+
+
+def _fetch_playlist_tracks_page(playlist_id: str, limit: int, offset: int) -> dict:
+ client = _build_librespot_client()
+ # Fetch playlist with minimal items to avoid expanding all tracks unnecessarily
+ pl = client.get_playlist(playlist_id, expand_items=False)
+ items = (pl.get("tracks", {}) or {}).get("items", [])
+ total = (pl.get("tracks", {}) or {}).get("total", len(items))
+ start = max(0, offset or 0)
+ end = start + max(1, limit or 50)
+ page_items_minimal = items[start:end]
+
+ # Expand only the tracks in this page using client cache for efficiency
+ page_items_expanded = []
+ for item in page_items_minimal:
+ track_stub = (item or {}).get("track") or {}
+ track_id = track_stub.get("id")
+ expanded_track = None
+ if track_id:
+ try:
+ expanded_track = client.get_track(track_id)
+ except Exception:
+ expanded_track = None
+ if expanded_track is None:
+ # Keep stub as fallback; ensure structure
+ expanded_track = {
+ k: v
+ for k, v in track_stub.items()
+ if k in ("id", "uri", "type", "external_urls")
+ }
+ # Propagate local flag onto track for downstream checks
+ if item and isinstance(item, dict) and item.get("is_local"):
+ expanded_track["is_local"] = True
+ # Rebuild item with expanded track
+ new_item = dict(item)
+ new_item["track"] = expanded_track
+ page_items_expanded.append(new_item)
+
+ return {
+ "items": page_items_expanded,
+ "total": total,
+ "limit": end - start,
+ "offset": start,
+ }
+
+
+def _fetch_artist_discography_page(artist_id: str, limit: int, offset: int) -> dict:
+ # LibrespotClient.get_artist returns a pruned mapping; flatten common discography groups
+ client = _build_librespot_client()
+ artist = client.get_artist(artist_id)
+ all_items = []
+ # Collect from known groups; also support nested structures if present
+ for key in ("album_group", "single_group", "compilation_group", "appears_on_group"):
+ grp = artist.get(key)
+ if isinstance(grp, list):
+ # Check if items are strings (IDs) or dictionaries (metadata)
+ if grp and isinstance(grp[0], str):
+ # Items are album IDs as strings, fetch metadata for each
+ for album_id in grp:
+ try:
+ album_data = client.get_album(album_id, include_tracks=False)
+ if album_data:
+ # Add the album_group type for filtering
+ album_data["album_group"] = key.replace("_group", "")
+ all_items.append(album_data)
+ except Exception as e:
+ logger.warning(f"Failed to fetch album {album_id}: {e}")
+ else:
+ # Items are already dictionaries (album metadata)
+ for item in grp:
+ if isinstance(item, dict):
+ # Ensure album_group is set for filtering
+ if "album_group" not in item:
+ item["album_group"] = key.replace("_group", "")
+ all_items.append(item)
+ elif isinstance(grp, dict):
+ items = grp.get("items") or grp.get("releases") or []
+ if isinstance(items, list):
+ for item in items:
+ if isinstance(item, dict):
+ # Ensure album_group is set for filtering
+ if "album_group" not in item:
+ item["album_group"] = key.replace("_group", "")
+ all_items.append(item)
+ total = len(all_items)
+ start = max(0, offset or 0)
+ end = start + max(1, limit or 50)
+ page_items = all_items[start:end]
+ return {"items": page_items, "total": total, "limit": limit, "offset": start}
diff --git a/spotizerr-ui/package.json b/spotizerr-ui/package.json
index 66fe2e1..93ccbb8 100644
--- a/spotizerr-ui/package.json
+++ b/spotizerr-ui/package.json
@@ -1,7 +1,7 @@
{
"name": "spotizerr-ui",
"private": true,
- "version": "3.3.0",
+ "version": "4.0.0",
"type": "module",
"scripts": {
"dev": "vite",
diff --git a/spotizerr-ui/public/save.svg b/spotizerr-ui/public/save.svg
new file mode 100644
index 0000000..583bdb5
--- /dev/null
+++ b/spotizerr-ui/public/save.svg
@@ -0,0 +1,4 @@
+
+
\ No newline at end of file
diff --git a/spotizerr-ui/public/spinner.svg b/spotizerr-ui/public/spinner.svg
new file mode 100644
index 0000000..93f03e9
--- /dev/null
+++ b/spotizerr-ui/public/spinner.svg
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/spotizerr-ui/src/components/AlbumCard.tsx b/spotizerr-ui/src/components/AlbumCard.tsx
index 00026e9..a97c8aa 100644
--- a/spotizerr-ui/src/components/AlbumCard.tsx
+++ b/spotizerr-ui/src/components/AlbumCard.tsx
@@ -2,10 +2,10 @@ import { Link } from "@tanstack/react-router";
import { useContext, useEffect } from "react";
import { toast } from "sonner";
import { QueueContext, getStatus } from "../contexts/queue-context";
-import type { AlbumType } from "../types/spotify";
+import type { LibrespotAlbumType } from "@/types/librespot";
interface AlbumCardProps {
- album: AlbumType;
+ album: LibrespotAlbumType;
onDownload?: () => void;
}
@@ -38,7 +38,7 @@ export const AlbumCard = ({ album, onDownload }: AlbumCardProps) => {
onDownload();
}}
disabled={!!status && status !== "error"}
- className="absolute bottom-2 right-2 p-2 bg-button-success hover:bg-button-success-hover text-button-success-text rounded-full transition-opacity shadow-lg opacity-0 group-hover:opacity-100 duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
+ className="absolute bottom-2 right-2 p-2 bg-button-success hover:bg-button-success-hover text-button-success-text rounded-full transition-opacity shadow-lg opacity-100 sm:opacity-0 sm:group-hover:opacity-100 duration-300 z-10 disabled:opacity-50 disabled:cursor-not-allowed"
title={
status
? status === "queued"
@@ -53,9 +53,9 @@ export const AlbumCard = ({ album, onDownload }: AlbumCardProps) => {
? status === "queued"
? "Queued."
: status === "error"
- ?
- : "Downloading..."
- :
+ ?
+ :
+ :
}
)}
diff --git a/spotizerr-ui/src/components/Queue.tsx b/spotizerr-ui/src/components/Queue.tsx
index 19cbb1f..b62f7c2 100644
--- a/spotizerr-ui/src/components/Queue.tsx
+++ b/spotizerr-ui/src/components/Queue.tsx
@@ -772,7 +772,7 @@ export const Queue = () => {
const priorities = {
"real-time": 1, downloading: 2, processing: 3, initializing: 4,
retrying: 5, queued: 6, done: 7, completed: 7, error: 8, cancelled: 9, skipped: 10
- };
+ } as Record
- : "Downloading..."
+ :
:
}
diff --git a/spotizerr-ui/src/components/config/AccountsTab.tsx b/spotizerr-ui/src/components/config/AccountsTab.tsx
index 62feec6..f0aa3c3 100644
--- a/spotizerr-ui/src/components/config/AccountsTab.tsx
+++ b/spotizerr-ui/src/components/config/AccountsTab.tsx
@@ -53,6 +53,19 @@ function extractApiErrorMessage(error: unknown): string {
if (typeof data?.detail === "string") return data.detail;
if (typeof data?.message === "string") return data.message;
if (typeof data?.error === "string") return data.error;
+ // If data.error is an object, try to extract a message from it
+ if (typeof data?.error === "object" && data.error !== null && typeof data.error.message === "string") {
+ return data.error.message;
+ }
+ // If data is an object but none of the above matched, try JSON stringifying it
+ if (typeof data === "object" && data !== null) {
+ try {
+ return JSON.stringify(data);
+ } catch (e) {
+ // Fallback if stringify fails
+ return fallback;
+ }
+ }
}
if (typeof anyErr?.message === "string") return anyErr.message;
return fallback;
@@ -66,7 +79,6 @@ export function AccountsTab() {
const queryClient = useQueryClient();
const [activeService, setActiveService] = useState
Provide your own API credentials to avoid rate-limiting issues.
-Tune background utility worker concurrency for low-powered systems.
+Adjust Librespot client worker threads.
+This album has been filtered based on your settings.
-{new Date(album.release_date).getFullYear()} • {album.total_tracks} songs
-{album.label}
+ {album.label &&{album.label}
} @@ -205,7 +234,7 @@ export const Album = () => { ? "Queued." : albumStatus === "error" ? "Download Album" - : "Downloading..." + :