Files
bvandeusen 4fca0e66cb fix(listenbrainz): similarity hits Labs API, not the 404'ing main API
Root cause of zero LB recommendations (every similar-recordings AND
similar-artists call returned HTTP 404, worker logs):

- Wrong host/path: client called
  api.listenbrainz.org/1/explore/similar-{recordings,artists}/{mbid}.
  /explore/... is a WEBSITE route, not an API endpoint — it 308s then
  404s. Similarity datasets live on the separate Labs API.
- Invalid algorithm: the hardcoded
  session_…_session_30_…_limit_100_filter_True_… is not a permitted
  Labs enum member (400s) regardless of host.

Verified against the live Labs API:
  GET labs.api.listenbrainz.org/similar-recordings/json
      ?recording_mbids=<mbid>&algorithm=<algo>
  GET labs.api.listenbrainz.org/similar-artists/json
      ?artist_mbids=<mbid>&algorithm=<algo>
  algorithm=session_based_days_9000_session_300_contribution_5_threshold_15_limit_50_skip_30
  → 200 for both. Response field names (recording_mbid/artist_mbid/
  name/score) already match the existing structs — parsing unchanged.

- Add defaultLabsBaseURL + Client.LabsBaseURL (separate from the main
  BaseURL; scrobble submission still uses api.listenbrainz.org).
- Drop the count/limit query param — result size is encoded in the
  algorithm name (limit_50); caller still applies its own top-K.
- Tests: newTestClient sets LabsBaseURL; the two *_LimitParamSet tests
  become *_MbidParamSet (assert the Labs path + mbid query param).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 15:50:32 -04:00

55 lines
2.0 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))
}
func cleanMBID(s string) string {
if s == "" {
return ""
}
for _, part := range strings.Split(s, "\x00") {
if t := strings.TrimSpace(part); t != "" {
return t
}
}
return ""
}