feat(server): action dispatch with structured result and messages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 20:02:18 -07:00
parent 09a0d7e682
commit 9912eb48a4
2 changed files with 119 additions and 0 deletions

59
server/actions.py Normal file
View File

@@ -0,0 +1,59 @@
"""Glue between a chosen Hit and a side-effecting download. Mirrors musicfetch's
main() dispatch but returns a structured result dict and speakable messages."""
import os
from typing import Optional
from . import mf
def _source_label(hit) -> str:
return "YouTube Music" if hit.source == "youtube" else "Lidarr"
def _title(hit) -> str:
return hit.album or hit.title or hit.artist
def started_message(hit) -> str:
return f"Found '{_title(hit)}' by {hit.artist or 'unknown artist'} on {_source_label(hit)}. Downloading now."
def done_message(hit) -> str:
return f"Finished downloading '{_title(hit)}' by {hit.artist or 'unknown artist'}."
def failed_message(hit) -> str:
return f"Failed to download '{_title(hit)}' by {hit.artist or 'unknown artist'}."
def _yt_path(hit, root: str) -> str:
artist_dir = (hit.artist.split(",")[0].strip() if hit.artist else "") or "Unknown Artist"
return os.path.join(root, artist_dir, "youtube")
def _download_youtube(hit, quality: str, root: str) -> dict:
mf.act_youtube(hit, root, quality, False)
return {"path": _yt_path(hit, root), "lidarr_album_id": None}
def perform_fetch(chosen, hits: list, quality: str, root: str) -> dict:
"""Run the download for the chosen hit. Returns {"path", "lidarr_album_id"}.
Raises on unrecoverable failure (recorded by the job worker)."""
if chosen.source == "youtube":
return _download_youtube(chosen, quality, root)
if chosen.kind == "album":
handled = mf.act_lidarr_album(chosen, root, False, False)
if handled:
return {"path": None, "lidarr_album_id": chosen.payload.get("album", {}).get("id")}
# No indexer release -> fall through to the top YouTube hit, like the CLI.
yt = next((h for h in hits if h.source == "youtube"), None)
if yt is None:
raise RuntimeError("No Lidarr release and no YouTube fallback available.")
return _download_youtube(yt, quality, root)
# Lidarr artist pick.
ok = mf.act_lidarr_artist(chosen, root, False, False)
if not ok:
raise RuntimeError("Failed to add artist to Lidarr.")
return {"path": None, "lidarr_album_id": None}