fix(lidarr): select metadata/quality profiles by name with env overrides
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
28
musicfetch
28
musicfetch
@@ -444,14 +444,30 @@ def get_existing_artist(name: str) -> Optional[dict]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_default_metadata_profile_id() -> int:
|
def _profile_id_by_name(path: str, env_var: str, default_name: str) -> int:
|
||||||
|
"""Return the id of the profile whose name matches env_var (default
|
||||||
|
default_name, case-insensitive). Fall back to the first profile, then 1."""
|
||||||
|
name = os.environ.get(env_var, default_name)
|
||||||
try:
|
try:
|
||||||
profiles = lidarr_get("/api/v1/metadataprofile", timeout=10)
|
profiles = lidarr_get(path, timeout=10)
|
||||||
if profiles:
|
|
||||||
return profiles[0]["id"]
|
|
||||||
except RequestException as e:
|
except RequestException as e:
|
||||||
dbg(f"metadataprofile fetch failed: {e}")
|
dbg(f"{path} fetch failed: {e}")
|
||||||
return 1
|
return 1
|
||||||
|
if not profiles:
|
||||||
|
return 1
|
||||||
|
for p in profiles:
|
||||||
|
if p.get("name", "").casefold() == name.casefold():
|
||||||
|
return p["id"]
|
||||||
|
dbg(f"profile '{name}' not found at {path}; using first ('{profiles[0].get('name')}')")
|
||||||
|
return profiles[0]["id"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_metadata_profile_id() -> int:
|
||||||
|
return _profile_id_by_name("/api/v1/metadataprofile", "LIDARR_METADATA_PROFILE", "Standard")
|
||||||
|
|
||||||
|
|
||||||
|
def get_quality_profile_id() -> int:
|
||||||
|
return _profile_id_by_name("/api/v1/qualityprofile", "LIDARR_QUALITY_PROFILE", "Any")
|
||||||
|
|
||||||
|
|
||||||
def add_artist(meta: dict, root: str, search_all: bool, dry_run: bool) -> Optional[dict]:
|
def add_artist(meta: dict, root: str, search_all: bool, dry_run: bool) -> Optional[dict]:
|
||||||
@@ -463,7 +479,7 @@ def add_artist(meta: dict, root: str, search_all: bool, dry_run: bool) -> Option
|
|||||||
payload = {
|
payload = {
|
||||||
"foreignArtistId": foreign_id,
|
"foreignArtistId": foreign_id,
|
||||||
"artistName": name,
|
"artistName": name,
|
||||||
"qualityProfileId": 1,
|
"qualityProfileId": get_quality_profile_id(),
|
||||||
"metadataProfileId": get_default_metadata_profile_id(),
|
"metadataProfileId": get_default_metadata_profile_id(),
|
||||||
"rootFolderPath": root,
|
"rootFolderPath": root,
|
||||||
"monitored": True,
|
"monitored": True,
|
||||||
|
|||||||
43
tests/test_profiles.py
Normal file
43
tests/test_profiles.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import server.mf # noqa: F401
|
||||||
|
import musicfetch_core as mf
|
||||||
|
|
||||||
|
META = [{"id": 1, "name": "Standard"}, {"id": 2, "name": "None"}, {"id": 3, "name": "OST"}]
|
||||||
|
QUAL = [{"id": 1, "name": "Any"}, {"id": 2, "name": "Lossless"}]
|
||||||
|
|
||||||
|
|
||||||
|
def test_metadata_profile_default_standard_by_name(monkeypatch):
|
||||||
|
monkeypatch.delenv("LIDARR_METADATA_PROFILE", raising=False)
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", lambda path, timeout=10: META)
|
||||||
|
assert mf.get_default_metadata_profile_id() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_metadata_profile_env_override(monkeypatch):
|
||||||
|
monkeypatch.setenv("LIDARR_METADATA_PROFILE", "OST")
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", lambda path, timeout=10: META)
|
||||||
|
assert mf.get_default_metadata_profile_id() == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_metadata_profile_unknown_name_falls_back_to_first(monkeypatch):
|
||||||
|
monkeypatch.setenv("LIDARR_METADATA_PROFILE", "Nonexistent")
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", lambda path, timeout=10: META)
|
||||||
|
assert mf.get_default_metadata_profile_id() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_quality_profile_default_any_by_name(monkeypatch):
|
||||||
|
monkeypatch.delenv("LIDARR_QUALITY_PROFILE", raising=False)
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", lambda path, timeout=10: QUAL)
|
||||||
|
assert mf.get_quality_profile_id() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_quality_profile_env_override(monkeypatch):
|
||||||
|
monkeypatch.setenv("LIDARR_QUALITY_PROFILE", "Lossless")
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", lambda path, timeout=10: QUAL)
|
||||||
|
assert mf.get_quality_profile_id() == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_profile_fetch_error_returns_one(monkeypatch):
|
||||||
|
def boom(path, timeout=10):
|
||||||
|
raise mf.RequestException("down")
|
||||||
|
monkeypatch.setattr(mf, "lidarr_get", boom)
|
||||||
|
assert mf.get_default_metadata_profile_id() == 1
|
||||||
|
assert mf.get_quality_profile_id() == 1
|
||||||
Reference in New Issue
Block a user