package musicbrainz import ( "context" "net/url" "strconv" "strings" ) // ReleaseGroup is what a person calls an album: one work, released any // number of times. type ReleaseGroup struct { MBID string Title string PrimaryType string // Album, Single, EP, Broadcast, Other; may be empty SecondaryTypes []string // Compilation, Live, Remix, Soundtrack, … // FirstReleaseDate is MusicBrainz's partial date: "", "1997", // "1997-05" or "1997-05-21". FirstReleaseDate string ArtistCredit string // the credit as printed, joinphrases included ArtistMBIDs []string } // Release is one issue of a release group: a pressing, an edition, a // digital release, each with its own track list. type Release struct { MBID string Title string Status string // Official, Promotion, Bootleg, Pseudo-Release; may be empty Date string // partial, like ReleaseGroup.FirstReleaseDate Country string Disambiguation string Media []Medium } // TrackCount totals the tracks across every medium. func (r Release) TrackCount() int { n := 0 for _, m := range r.Media { n += len(m.Tracks) } return n } // Formats summarises the media, e.g. "CD", "2×CD" or "CD + DVD". func (r Release) Formats() string { var parts []string counts := map[string]int{} for _, m := range r.Media { f := m.Format if f == "" { f = "Unknown" } if counts[f] == 0 { parts = append(parts, f) } counts[f]++ } for i, f := range parts { if counts[f] > 1 { parts[i] = strconv.Itoa(counts[f]) + "×" + f } } return strings.Join(parts, " + ") } // Medium is one disc (or side, or file set) of a release. type Medium struct { Position int Format string Tracks []Track } // Track is a position on a medium, pointing at the recording heard there. type Track struct { Position int Number string // as printed: "1", "A1", … Title string LengthMs int // 0 when unknown RecordingMBID string } // --- wire shapes --- type wireCredit struct { Name string `json:"name"` JoinPhrase string `json:"joinphrase"` Artist struct { ID string `json:"id"` Name string `json:"name"` } `json:"artist"` } func credit(cs []wireCredit) (string, []string) { var b strings.Builder ids := make([]string, 0, len(cs)) for _, c := range cs { name := c.Name if name == "" { name = c.Artist.Name } b.WriteString(name) b.WriteString(c.JoinPhrase) if c.Artist.ID != "" { ids = append(ids, c.Artist.ID) } } return b.String(), ids } type wireReleaseGroup struct { ID string `json:"id"` Title string `json:"title"` PrimaryType string `json:"primary-type"` SecondaryTypes []string `json:"secondary-types"` FirstReleaseDate string `json:"first-release-date"` ArtistCredit []wireCredit `json:"artist-credit"` } func (w wireReleaseGroup) group() ReleaseGroup { name, ids := credit(w.ArtistCredit) return ReleaseGroup{ MBID: w.ID, Title: w.Title, PrimaryType: w.PrimaryType, SecondaryTypes: append([]string{}, w.SecondaryTypes...), FirstReleaseDate: w.FirstReleaseDate, ArtistCredit: name, ArtistMBIDs: ids, } } type wireTrack struct { Position int `json:"position"` Number string `json:"number"` Title string `json:"title"` Length *int `json:"length"` Recording struct { ID string `json:"id"` Length *int `json:"length"` } `json:"recording"` } type wireRelease struct { ID string `json:"id"` Title string `json:"title"` Status string `json:"status"` Date string `json:"date"` Country string `json:"country"` Disambiguation string `json:"disambiguation"` Media []struct { Position int `json:"position"` Format string `json:"format"` Tracks []wireTrack `json:"tracks"` } `json:"media"` } func (w wireRelease) release() Release { r := Release{MBID: w.ID, Title: w.Title, Status: w.Status, Date: w.Date, Country: w.Country, Disambiguation: w.Disambiguation} for _, m := range w.Media { med := Medium{Position: m.Position, Format: m.Format} for _, t := range m.Tracks { length := 0 switch { case t.Length != nil: length = *t.Length case t.Recording.Length != nil: length = *t.Recording.Length } med.Tracks = append(med.Tracks, Track{ Position: t.Position, Number: t.Number, Title: t.Title, LengthMs: length, RecordingMBID: t.Recording.ID, }) } r.Media = append(r.Media, med) } return r } // --- lookups and browses --- // ReleaseGroupReleases lists every release of a release group with its // media and track lists. func (c *Client) ReleaseGroupReleases(ctx context.Context, groupMBID string) ([]Release, error) { var out []Release for page := 0; page < maxPages; page++ { var resp struct { Count int `json:"release-count"` Releases []wireRelease `json:"releases"` } q := url.Values{ "release-group": {groupMBID}, "inc": {"media recordings"}, "limit": {strconv.Itoa(pageSize)}, "offset": {strconv.Itoa(len(out))}, } if err := c.get(ctx, "/release", q, &resp); err != nil { return nil, err } for _, r := range resp.Releases { out = append(out, r.release()) } // Advance by what came back: MusicBrainz may return fewer than the // page size when track lists are large. if len(resp.Releases) == 0 || len(out) >= resp.Count { break } } return out, nil } // ReleaseGroup looks one release group up by MBID, with its artist credit. func (c *Client) ReleaseGroup(ctx context.Context, mbid string) (ReleaseGroup, error) { var w wireReleaseGroup q := url.Values{"inc": {"artist-credits"}} if err := c.get(ctx, "/release-group/"+url.PathEscape(mbid), q, &w); err != nil { return ReleaseGroup{}, err } if w.ID == "" { return ReleaseGroup{}, ErrBadResponse } return w.group(), nil }