225 lines
8.1 KiB
Python
225 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
import urllib.parse
|
|
import urllib.request
|
|
from collections import Counter
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
def _get_json(url: str) -> Any:
|
|
req = urllib.request.Request(url, method="GET")
|
|
with urllib.request.urlopen(req, timeout=120) as resp:
|
|
return json.loads(resp.read().decode("utf-8"))
|
|
|
|
|
|
def _post_json(url: str, payload: dict[str, Any]) -> Any:
|
|
req = urllib.request.Request(
|
|
url,
|
|
method="POST",
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=240) as resp:
|
|
return json.loads(resp.read().decode("utf-8"))
|
|
|
|
|
|
def _fetch_subsonic_songs(base: str, count: int) -> tuple[list[dict[str, Any]], str]:
|
|
# Primary path: wildcard song search.
|
|
search_qs = urllib.parse.urlencode({"q": "*", "count": count, "offset": 0})
|
|
songs: list[dict[str, Any]] = _get_json(f"{base}/api/subsonic/search?{search_qs}")
|
|
if songs:
|
|
return songs, "wildcard-search"
|
|
|
|
# Fallback: album browse + album detail flattening.
|
|
estimated_songs_per_album = 10
|
|
album_size = max(20, min(120, (count + estimated_songs_per_album - 1) // estimated_songs_per_album))
|
|
album_qs = urllib.parse.urlencode({"offset": 0, "size": album_size})
|
|
albums: list[dict[str, Any]] = _get_json(f"{base}/api/subsonic/albums?{album_qs}")
|
|
|
|
dedupe: dict[str, dict[str, Any]] = {}
|
|
for album in albums:
|
|
album_id = str(album.get("id") or "").strip()
|
|
if not album_id:
|
|
continue
|
|
detail = _get_json(f"{base}/api/subsonic/albums/{urllib.parse.quote(album_id)}")
|
|
for song in detail.get("songs") or []:
|
|
song_id = str(song.get("id") or "").strip()
|
|
if not song_id:
|
|
continue
|
|
if song_id not in dedupe:
|
|
dedupe[song_id] = song
|
|
if len(dedupe) >= count:
|
|
break
|
|
if len(dedupe) >= count:
|
|
break
|
|
|
|
return list(dedupe.values())[:count], "album-fallback"
|
|
|
|
|
|
def _derive_songs_from_sek_library(base: str, folder: str | None, count: int) -> list[dict[str, Any]]:
|
|
folder_qs = f"?folder={urllib.parse.quote(folder)}" if folder else ""
|
|
inventory = _get_json(f"{base}/api/karaoke-sek/library{folder_qs}")
|
|
packages = (inventory.get("packages") or [])[:count]
|
|
songs: list[dict[str, Any]] = []
|
|
for idx, pkg in enumerate(packages):
|
|
title = str(pkg.get("meta_title") or pkg.get("title") or "").strip()
|
|
artist = str(pkg.get("meta_artist") or pkg.get("meta_albumartist") or "").strip()
|
|
if not title:
|
|
continue
|
|
songs.append(
|
|
{
|
|
"id": f"sek-probe-{idx}",
|
|
"title": title,
|
|
"artist": artist,
|
|
"songArtist": artist,
|
|
"albumArtist": str(pkg.get("meta_albumartist") or "").strip(),
|
|
"album": str(pkg.get("meta_album") or "").strip(),
|
|
"duration": pkg.get("meta_duration_seconds"),
|
|
}
|
|
)
|
|
return songs
|
|
|
|
|
|
def run(base_url: str, count: int, folder: str | None, allow_unconfigured: bool = False) -> dict[str, Any]:
|
|
base = base_url.rstrip("/")
|
|
|
|
health = _get_json(f"{base}/api/system/health")
|
|
cfg = _get_json(f"{base}/api/subsonic/config")
|
|
|
|
source_mode = "subsonic"
|
|
songs: list[dict[str, Any]] = []
|
|
|
|
fetch_mode = ""
|
|
if bool(cfg.get("configured")):
|
|
songs, fetch_mode = _fetch_subsonic_songs(base=base, count=count)
|
|
elif allow_unconfigured:
|
|
source_mode = "sek-derived-probes"
|
|
songs = _derive_songs_from_sek_library(base=base, folder=folder, count=count)
|
|
fetch_mode = "sek-probe-derived"
|
|
else:
|
|
raise RuntimeError("Subsonic is not configured on this server")
|
|
|
|
match_body = {
|
|
"songs": [
|
|
{
|
|
"id": song.get("id"),
|
|
"title": song.get("title") or "",
|
|
"artist": song.get("artist") or "",
|
|
"songArtist": song.get("songArtist") or "",
|
|
"albumArtist": song.get("albumArtist") or "",
|
|
"album": song.get("album") or "",
|
|
"duration": song.get("duration"),
|
|
}
|
|
for song in songs
|
|
],
|
|
"folder": folder,
|
|
}
|
|
|
|
match = _post_json(f"{base}/api/karaoke-sek/match-subsonic", match_body)
|
|
|
|
matches_map = match.get("matches") or {}
|
|
reason_counts: Counter[str] = Counter()
|
|
strong_unique = 0
|
|
weak_only = 0
|
|
ambiguous_rows: list[dict[str, Any]] = []
|
|
|
|
by_song_id = {str(s.get("id")): s for s in songs}
|
|
|
|
for song_id, candidates in matches_map.items():
|
|
candidates = candidates or []
|
|
for c in candidates:
|
|
for reason in c.get("reasons") or []:
|
|
reason_counts[reason] += 1
|
|
|
|
strong = [c for c in candidates if float(c.get("confidence") or 0.0) >= 0.85]
|
|
if len(strong) == 1:
|
|
strong_unique += 1
|
|
elif len(strong) > 1:
|
|
song = by_song_id.get(str(song_id), {})
|
|
ambiguous_rows.append(
|
|
{
|
|
"song_id": song_id,
|
|
"title": song.get("title") or "",
|
|
"artist": (song.get("songArtist") or song.get("artist") or ""),
|
|
"strong_candidates": [
|
|
{
|
|
"title": c.get("title"),
|
|
"filename": c.get("filename"),
|
|
"confidence": c.get("confidence"),
|
|
"duration_delta_seconds": c.get("duration_delta_seconds"),
|
|
}
|
|
for c in strong[:5]
|
|
],
|
|
}
|
|
)
|
|
elif candidates:
|
|
weak_only += 1
|
|
|
|
report = {
|
|
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
|
|
"health": health,
|
|
"subsonic": {
|
|
"configured": cfg.get("configured"),
|
|
"url": cfg.get("url"),
|
|
"username": cfg.get("username"),
|
|
"source_mode": source_mode,
|
|
"fetch_mode": fetch_mode,
|
|
"songs_requested": count,
|
|
"songs_fetched": len(songs),
|
|
},
|
|
"matcher": {
|
|
"folder": match.get("folder"),
|
|
"scan_root": match.get("scan_root"),
|
|
"scan_count": match.get("scan_count"),
|
|
"scan_ms": match.get("scan_ms"),
|
|
"cache": match.get("cache"),
|
|
"summary": match.get("summary"),
|
|
"strong_unique": strong_unique,
|
|
"weak_only": weak_only,
|
|
"top_reasons": reason_counts.most_common(12),
|
|
"ambiguous_examples": ambiguous_rows[:10],
|
|
},
|
|
}
|
|
return report
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Run Subsonic ↔ SEK matching QA summary")
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8000", help="StemEngine base URL")
|
|
parser.add_argument("--count", type=int, default=250, help="Subsonic wildcard search song count")
|
|
parser.add_argument("--folder", default="/app/media/sekfold", help="SEK folder override")
|
|
parser.add_argument("--allow-unconfigured", action="store_true", help="If Subsonic is not configured, derive probe songs from SEK library metadata")
|
|
parser.add_argument("--out", default="", help="Optional JSON output file path")
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
report = run(
|
|
base_url=args.base_url,
|
|
count=max(1, args.count),
|
|
folder=args.folder or None,
|
|
allow_unconfigured=bool(args.allow_unconfigured),
|
|
)
|
|
except Exception as exc:
|
|
print(f"QA failed: {exc}", file=sys.stderr)
|
|
return 2
|
|
|
|
print(json.dumps(report, indent=2, ensure_ascii=False))
|
|
|
|
if args.out:
|
|
out_path = Path(args.out)
|
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
out_path.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
print(f"\nWrote report: {out_path}")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|