mirror of
https://github.com/RustyRaptor/doge_cogs.git
synced 2026-12-03 15:55:21 -07:00
Prefer a studio album when a track resolves to several release groups
importplaylist monitors the release group that contains a requested track. When Lidarr returned more than one match for the title, choose_album gave up and the requester had to pick by hand; when a single was the only match, the grab targeted an uncached single. Uncached singles are what stall against TorBox on a plan-1 account, exceed the proxy timeout and make Lidarr retry with yet another release — the Avicii singles were stuck in that loop. Release groups now rank album > EP > single, with a plain edition ahead of a compilation, live, remix, DJ-mix, mixtape, soundtrack or spoken-word edition. The rank only breaks ties: a lone match is still selected, and two equally ranked candidates stay ambiguous so the requester chooses. Tests came first (tests/test_music_release_preference.py, 8 cases): studio album over single, EP between the two, plain over compilation and live, plain single over a remix edition, ties still ambiguous, typeless lookups keeping the old behaviour, a lone single still selected, and the artist check not being outranked. watch/music.py was changed only after those tests failed. Full suite: 362 passed; ruff clean. Not deployed. Branch is for the Buffy agent to merge and deploy.
This commit is contained in:
1 parent
85eab7bb16
commit
c577a7f369
2 files changed
+162
-3
No files matched your search
@@ -0,0 +1,113 @@
|
||||
"""Prefer cacheable release groups when a track resolves to several albums."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def album(
|
||||
album_id: int,
|
||||
title: str,
|
||||
kind: str | None,
|
||||
secondary: list[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Build a Lidarr album resource as album/lookup returns it."""
|
||||
return {
|
||||
"id": album_id,
|
||||
"foreignAlbumId": f"mbid-{album_id}",
|
||||
"title": title,
|
||||
"albumType": kind,
|
||||
"secondaryTypes": secondary or [],
|
||||
"artist": {"artistName": "Avicii"},
|
||||
}
|
||||
|
||||
|
||||
def choose(
|
||||
track_title: str, candidates: list[dict[str, Any]]
|
||||
) -> dict[str, Any] | None:
|
||||
"""Select an album for a track by the artist above."""
|
||||
from watch.music import choose_album
|
||||
|
||||
source = {"title": track_title, "artist": "Avicii"}
|
||||
return choose_album(source, candidates)
|
||||
|
||||
|
||||
def test_a_studio_album_wins_over_a_single_with_the_same_title() -> None:
|
||||
"""Uncached singles stall downloads; the album is the safe target."""
|
||||
candidates = [
|
||||
album(1, "Wake Me Up", "Single"),
|
||||
album(2, "Wake Me Up", "Album"),
|
||||
]
|
||||
selected = choose("Wake Me Up", candidates)
|
||||
assert selected is not None
|
||||
assert selected["id"] == 2
|
||||
|
||||
|
||||
def test_an_ep_wins_over_a_single_but_not_over_an_album() -> None:
|
||||
"""Rank album, then EP, then single."""
|
||||
candidates = [
|
||||
album(1, "Levels", "Single"),
|
||||
album(2, "Levels", "EP"),
|
||||
]
|
||||
assert choose("Levels", candidates)["id"] == 2
|
||||
|
||||
candidates.append(album(3, "Levels", "Album"))
|
||||
assert choose("Levels", candidates)["id"] == 3
|
||||
|
||||
|
||||
def test_a_plain_album_wins_over_a_compilation_or_live_edition() -> None:
|
||||
"""A studio album imports more reliably than a compilation."""
|
||||
candidates = [
|
||||
album(1, "True", "Album", ["Compilation"]),
|
||||
album(2, "True", "Album", ["Live"]),
|
||||
album(3, "True", "Album"),
|
||||
]
|
||||
assert choose("True", candidates)["id"] == 3
|
||||
|
||||
|
||||
def test_a_single_wins_over_a_remix_edition() -> None:
|
||||
"""A remix collection is worse than the plain single for one track."""
|
||||
candidates = [
|
||||
album(1, "Wake Me Up", "Single", ["Remix"]),
|
||||
album(2, "Wake Me Up", "Single"),
|
||||
]
|
||||
assert choose("Wake Me Up", candidates)["id"] == 2
|
||||
|
||||
|
||||
def test_equally_ranked_albums_stay_ambiguous() -> None:
|
||||
"""Never guess between two genuine candidates."""
|
||||
candidates = [
|
||||
album(1, "True", "Album"),
|
||||
album(2, "True", "Album"),
|
||||
]
|
||||
assert choose("True", candidates) is None
|
||||
|
||||
|
||||
def test_albums_without_a_release_type_stay_ambiguous() -> None:
|
||||
"""Keep the old behaviour for lookups that carry no type."""
|
||||
candidates = [
|
||||
album(1, "Violator", None),
|
||||
album(2, "Violator", None),
|
||||
]
|
||||
assert choose("Violator", candidates) is None
|
||||
|
||||
|
||||
def test_a_lone_single_is_still_selected() -> None:
|
||||
"""Preference is a tie-break, not a filter."""
|
||||
selected = choose("Wake Me Up", [album(1, "Wake Me Up", "Single")])
|
||||
assert selected is not None
|
||||
assert selected["id"] == 1
|
||||
|
||||
|
||||
def test_release_preference_never_overrides_the_artist_check() -> None:
|
||||
"""A better-ranked wrong artist is still rejected."""
|
||||
candidates = [
|
||||
album(1, "True", "Album"),
|
||||
{
|
||||
"id": 2,
|
||||
"foreignAlbumId": "mbid-2",
|
||||
"title": "True",
|
||||
"albumType": "Album",
|
||||
"secondaryTypes": [],
|
||||
"artist": {"artistName": "A Tribute Band"},
|
||||
},
|
||||
]
|
||||
assert choose("True", candidates)["id"] == 1
|
||||
+49
-3
@@ -68,18 +68,64 @@ def appears_on(artist: str, album: dict[str, Any]) -> bool:
|
||||
) in artist_credits(artist)
|
||||
|
||||
|
||||
# A track often resolves to several release groups: the single it was sold as
|
||||
# and the album it also appears on. Uncached singles are the ones that stall in
|
||||
# TorBox and push Lidarr into grabbing other releases, so a studio album is the
|
||||
# safer target and ranks first.
|
||||
RELEASE_TYPE_ORDER = {"album": 0, "ep": 1, "single": 2}
|
||||
DERIVATIVE_RELEASE_TYPES = frozenset(
|
||||
{
|
||||
"compilation",
|
||||
"live",
|
||||
"remix",
|
||||
"djmix",
|
||||
"mixtapestreet",
|
||||
"soundtrack",
|
||||
"spokenword",
|
||||
"interview",
|
||||
"audiobook",
|
||||
"audiodrama",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def release_rank(album: dict[str, Any]) -> tuple[int, int]:
|
||||
"""Rank release groups so a cacheable album beats a single."""
|
||||
kind = normalized(str(album.get("albumType") or ""))
|
||||
secondary = {
|
||||
normalized(str(value))
|
||||
for value in (album.get("secondaryTypes") or [])
|
||||
}
|
||||
derivative = 1 if secondary & DERIVATIVE_RELEASE_TYPES else 0
|
||||
order = RELEASE_TYPE_ORDER.get(kind, len(RELEASE_TYPE_ORDER))
|
||||
return (order, derivative)
|
||||
|
||||
|
||||
def choose_album(
|
||||
source: dict[str, str], candidates: list[dict[str, Any]]
|
||||
) -> dict[str, Any] | None:
|
||||
"""Require one canonical release group by the credited artist."""
|
||||
"""Pick one canonical release group by the credited artist.
|
||||
|
||||
Title and credit must match exactly. When that still leaves several
|
||||
release groups, prefer the most cacheable one: album before EP, EP
|
||||
before single, and a plain edition before a compilation, live or remix
|
||||
edition. A tie between equally good candidates stays ambiguous so the
|
||||
requester chooses.
|
||||
"""
|
||||
matches = [
|
||||
a
|
||||
for a in candidates
|
||||
if appears_on(source["artist"], a)
|
||||
and album_title(a["title"]) == album_title(source["title"])
|
||||
]
|
||||
ids = {a["foreignAlbumId"] for a in matches}
|
||||
return matches[0] if len(ids) == 1 else None
|
||||
unique = {a["foreignAlbumId"]: a for a in matches}
|
||||
if not unique:
|
||||
return None
|
||||
if len(unique) == 1:
|
||||
return matches[0]
|
||||
best = min(release_rank(a) for a in unique.values())
|
||||
winners = [a for a in unique.values() if release_rank(a) == best]
|
||||
return winners[0] if len(winners) == 1 else None
|
||||
|
||||
|
||||
def find_audio(
|
||||
|
||||
Reference in new issue
Block a user