Files
alembic/app/routers/playlists.py
T
andrew 97672ffdcd 0.5.2: Run now for unscheduled playlists, Spotify /items migration, friendlier 403
- Run now (jobs.run_now): playlists without a daily sync time are never
  registered as scheduler jobs, so the old registered-job check 404'd. Fall
  back to running the playlist directly via pipeline_runner for any
  playlist:<name> key that isn't registered.
- Spotify removed GET /playlists/{id}/tracks in its February 2026 API changes
  in favor of /items (identical response shape). New apps are already 403'd on
  the old endpoint. Migrate both callers (app/services/spotify_client.py and
  pipeline/lib/spotify-retag.py) to /items. (The vendored sldl downloader uses
  its own bundled Spotify library and would need an upstream update when
  /tracks is fully removed.)
- Friendlier on-screen error for a Spotify 403 (check credentials + playlist
  must be public) and a README troubleshooting entry covering the 403, the
  public-playlist requirement, and the org-only extended-quota reality.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 10:12:42 -06:00

110 lines
3.5 KiB
Python

import httpx
from fastapi import APIRouter, Depends, Form, HTTPException, Request
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates
from app.db import get_db
from app.security.deps import require_auth
from app.services import playlist_service, status_service
router = APIRouter(prefix="/playlists", tags=["playlists"])
templates = Jinja2Templates(directory="app/templates")
def _friendly_status_error(exc: Exception) -> str:
"""Turn a raw Spotify API error into something a non-technical user can act
on. 403 here almost always means the Spotify app can't read the playlist:
either the credentials are wrong or the playlist isn't public."""
if isinstance(exc, httpx.HTTPStatusError) and exc.response.status_code == 403:
return (
"Spotify denied access (403). Check your Spotify credentials under "
"Settings then Credentials, and make sure the playlist is set to public "
"on Spotify -- alembic can't read private playlists."
)
return str(exc)
@router.get("")
async def playlists_index(request: Request, user: dict = Depends(require_auth), db=Depends(get_db)):
playlists = playlist_service.list_all(db)
sync_times = {p.id: playlist_service.cron_to_time(p.cron_expr) for p in playlists}
return templates.TemplateResponse(
request, "playlists/index.html", {"playlists": playlists, "sync_times": sync_times}
)
@router.post("")
async def create_playlist(
name: str = Form(...),
spotify_url: str = Form(...),
sync_time: str = Form(""),
no_m3u: bool = Form(False),
user: dict = Depends(require_auth),
db=Depends(get_db),
):
try:
playlist_service.create(
db,
name=name,
spotify_url=spotify_url,
cron_expr=playlist_service.time_to_cron(sync_time),
no_m3u=no_m3u,
)
except ValueError as exc:
raise HTTPException(400, str(exc))
return RedirectResponse(url="/playlists", status_code=303)
@router.get("/{playlist_id}")
async def playlist_detail(
request: Request, playlist_id: int, user: dict = Depends(require_auth), db=Depends(get_db)
):
playlist = playlist_service.get(db, playlist_id)
if playlist is None:
raise HTTPException(404, "no such playlist")
status = None
error = None
try:
status = status_service.playlist_status(db, playlist.name, playlist.spotify_url)
except Exception as exc:
error = _friendly_status_error(exc)
return templates.TemplateResponse(
request,
"playlists/detail.html",
{
"playlist": playlist,
"sync_time": playlist_service.cron_to_time(playlist.cron_expr),
"status": status,
"error": error,
},
)
@router.post("/{playlist_id}")
async def update_playlist(
playlist_id: int,
active: bool = Form(False),
sync_time: str = Form(""),
no_m3u: bool = Form(False),
notes: str = Form(""),
user: dict = Depends(require_auth),
db=Depends(get_db),
):
playlist_service.update(
db,
playlist_id,
active=active,
cron_expr=playlist_service.time_to_cron(sync_time),
no_m3u=no_m3u,
notes=notes or None,
)
return RedirectResponse(url=f"/playlists/{playlist_id}", status_code=303)
@router.post("/{playlist_id}/delete")
async def delete_playlist(playlist_id: int, user: dict = Depends(require_auth), db=Depends(get_db)):
playlist_service.delete(db, playlist_id)
return RedirectResponse(url="/playlists", status_code=303)