Files
minstrel/internal/library/mbids.go
T
bvandeusenandClaude Opus 5.5 5c19a916ba
release / govulncheck (push) Successful in 22s
release / web (push) Successful in 1m14s
release / go (push) Successful in 1m33s
release / integration (push) Successful in 4m36s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 5m24s
release / Build signed APK (releases and dev) (push) Canceled after 5m29s
feat(library): store each album's MusicBrainz release-group id (M483 #5242)
albums.mbid is the release id (Picard's musicbrainz_albumid, one edition).
Lidarr names albums by release group, so re-acquisition and request
completion need that id too (#5241).

Migration 0070 adds albums.release_group_mbid (nullable, non-unique index:
several releases share a group). The scanner reads musicbrainz_releasegroupid
through extractReleaseGroupMBID, writes it on insert and heals it onto
existing rows when NULL. tagReadVersion goes to 3 so the next scan fills
it for the library already indexed, bound by tag reads (no ffprobe, no
decode).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 11:33:26 -04:00

65 lines
2.5 KiB
Go

package library
import (
"strings"
"github.com/dhowden/tag"
"github.com/dhowden/tag/mbz"
)
// extractMBIDs reads MusicBrainz album and artist IDs from a tag.Metadata.
//
// Delegates to dhowden/tag/mbz, which knows the per-format Raw() conventions:
// - Vorbis (FLAC/OGG) keys are lowercased by the parser.
// - ID3v2 (MP3) TXXX frames are stored under "TXXX"/"TXXX_N" with
// a *tag.Comm value whose Description holds the Picard tag name.
// - MP4 (M4A) freeform iTunes atoms are stored under their bare sub-name.
//
// Then sanitizes: dhowden's readTextWithDescrFrame (used for ID3v2 TXXX)
// does NOT strip the trailing/embedded NUL bytes that ID3v2.4 uses as
// frame terminator and multi-value separator — so the Text often comes
// out as "uuid\x00" or "uuid1\x00uuid2". Postgres rejects NULs in text
// columns (SQLSTATE 22021). We split on NUL and take the first non-empty
// segment — Minstrel uses the primary release artist for MBCAA lookup,
// so first-wins matches the original semantics.
func extractMBIDs(m tag.Metadata) (albumMBID, artistMBID string) {
info := mbz.Extract(m)
return cleanMBID(info.Get(mbz.Album)), cleanMBID(info.Get(mbz.Artist))
}
// extractRecordingMBID reads the MusicBrainz *recording* ID — Picard's
// musicbrainz_recordingid, surfaced by dhowden/tag as mbz.Recording
// (tag display name "MusicBrainz Track Id"). This is the id the
// ListenBrainz Labs similar-recordings API keys on and what tracks.mbid
// stores.
//
// Deliberately NOT mbz.Track ("MusicBrainz Release Track Id"): that is
// per-release-track, whereas similarity is per-recording. Kept separate
// from extractMBIDs so its existing (album, artist) signature and unit
// tests stay untouched.
func extractRecordingMBID(m tag.Metadata) string {
return cleanMBID(mbz.Extract(m).Get(mbz.Recording))
}
// extractReleaseGroupMBID reads the MusicBrainz *release-group* ID —
// Picard's musicbrainz_releasegroupid, surfaced by dhowden/tag as
// mbz.ReleaseGroup. mbz.Album (what albums.mbid stores) is the release: one
// edition. Lidarr names albums by release group, so re-acquisition and
// request completion key on this one (#5241). Separate from extractMBIDs for
// the same reason extractRecordingMBID is.
func extractReleaseGroupMBID(m tag.Metadata) string {
return cleanMBID(mbz.Extract(m).Get(mbz.ReleaseGroup))
}
func cleanMBID(s string) string {
if s == "" {
return ""
}
for _, part := range strings.Split(s, "\x00") {
if t := strings.TrimSpace(part); t != "" {
return t
}
}
return ""
}