Commit 54534bce authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Fix missing album art for AcoustID matches with multiple MusicBrainz releases

Cover art lookup only checked release-list[0]'s MBID against Cover Art
Archive. MusicBrainz doesn't order releases by "has art archived", so
whenever the first release happened to lack an uploaded cover the song
was stuck with coverart=null permanently (cached by audio fingerprint,
so even re-uploading the same file kept returning the missing art).
Now tries each candidate release in turn until one actually has art.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent fc4b4352
Pipeline #1305 failed with stage
...@@ -108,8 +108,21 @@ async def identify_audio(file_path: str) -> list: ...@@ -108,8 +108,21 @@ async def identify_audio(file_path: str) -> list:
album = rel.get('title', album) album = rel.get('title', album)
year = rel.get('date', year)[:4] if rel.get('date') else year year = rel.get('date', year)[:4] if rel.get('date') else year
track_number = f"{rel.get('medium-list', [{}])[0].get('track-list', [{}])[0].get('number', '1')}/{rel.get('medium-list', [{}])[0].get('track-count', '1')}" track_number = f"{rel.get('medium-list', [{}])[0].get('track-list', [{}])[0].get('number', '1')}/{rel.get('medium-list', [{}])[0].get('track-count', '1')}"
if rel.get('id'): # MusicBrainz doesn't order release-list by "most likely to have
coverart = await _fetch_cover_art_archive_url(rel['id']) # archived art" - it's often just release-date order, so the very
# first release (used above for album/year text) frequently turns
# out to be a pressing nobody ever uploaded a cover for, while a
# later release of the exact same recording has one. Album/year text
# stays tied to releases[0] (still the most representative edition),
# but the art lookup tries each release in turn rather than giving up
# after just the one - confirmed via a real case where this exact
# recording had art on dev (different release picked) but not prod.
for candidate_rel in releases[:5]:
if not candidate_rel.get('id'):
continue
coverart = await _fetch_cover_art_archive_url(candidate_rel['id'])
if coverart:
break
except Exception as e: except Exception as e:
logger.error(f"MusicBrainz release lookup failed for recording {recording_id}: {e}") logger.error(f"MusicBrainz release lookup failed for recording {recording_id}: {e}")
......
...@@ -201,3 +201,43 @@ def test_identify_audio_leaves_coverart_null_when_archive_has_none(mocker): ...@@ -201,3 +201,43 @@ def test_identify_audio_leaves_coverart_null_when_archive_has_none(mocker):
candidates = asyncio.run(identify_audio("/tmp/does-not-matter.mp3")) candidates = asyncio.run(identify_audio("/tmp/does-not-matter.mp3"))
assert candidates[0]["coverart"] is None assert candidates[0]["coverart"] is None
def test_identify_audio_falls_back_to_a_later_release_for_cover_art(mocker):
"""Regression: a real recording had art archived for a second pressing but not the first -
release-list isn't ordered by "has art", so only ever checking releases[0] (the one used for
album/year text) missed art that genuinely exists for the exact same recording elsewhere."""
shazam_instance = mocker.Mock()
shazam_instance.recognize = mocker.AsyncMock(return_value={})
mocker.patch("src.services.music_identification.Shazam", return_value=shazam_instance)
mocker.patch(
"src.services.music_identification.acoustid.match",
return_value=[(0.95, "rec-id-1", "House in the Woods", "Low Roar")],
)
mocker.patch(
"src.services.music_identification.musicbrainzngs.get_recording_by_id",
return_value={
"recording": {
"release-list": [
{"id": "release-no-art", "title": "House in the Woods (Single)"},
{"id": "release-has-art", "title": "House in the Woods"},
]
}
},
)
fetch_cover_art = mocker.patch(
"src.services.music_identification._fetch_cover_art_archive_url",
new=mocker.AsyncMock(
side_effect=lambda release_id: "https://coverartarchive.org/release/release-has-art/front-500"
if release_id == "release-has-art"
else None
),
)
candidates = asyncio.run(identify_audio("/tmp/does-not-matter.mp3"))
# Album/year text still comes from releases[0] (the single, not the album) - only the art
# lookup itself falls through to the next release.
assert candidates[0]["album"] == "House in the Woods (Single)"
assert candidates[0]["coverart"] == "https://coverartarchive.org/release/release-has-art/front-500"
assert fetch_cover_art.call_args_list == [mocker.call("release-no-art"), mocker.call("release-has-art")]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment