strip-beats/media/brainz/brainz.go

195 lines
4.1 KiB
Go
Raw Normal View History

2023-09-24 16:40:24 -05:00
package brainz
2023-09-09 21:02:43 -05:00
import (
"encoding/json"
"fmt"
"io"
u "net/url"
2023-09-09 22:34:03 -05:00
h "codeberg.org/danjones000/strip-beats/utils/http"
2023-09-24 16:40:24 -05:00
"github.com/google/uuid"
2023-09-09 21:02:43 -05:00
)
2023-09-24 16:40:24 -05:00
type Recording struct {
Id uuid.UUID
2023-11-11 17:01:01 -06:00
AcousticId uuid.UUID
2023-09-23 17:17:27 -05:00
Isrcs []string
2023-09-09 21:02:43 -05:00
FirstReleaseDate string `json:"first-release-date"`
Length int
Title string
Video bool
2023-09-24 16:40:24 -05:00
Releases []Release
Genres []Genre
2023-09-24 22:34:30 -05:00
ArtistCredit []ArtistCredit `json:"artist-credit"`
}
func (r Recording) FirstArtist() Artist {
var a Artist
for _, ac := range r.ArtistCredit {
if ac.Artist.Name != "" {
return ac.Artist
}
}
for _, rel := range r.Releases {
for _, ac := range rel.ArtistCredit {
if ac.Artist.Name != "" {
return ac.Artist
}
}
}
if a.Name == "" {
a.Name = "Unknown Artist"
}
return a
}
func (r Recording) FirstGenre() Genre {
var g Genre
for _, g = range r.Genres {
if g.Name != "" {
return g
}
}
for _, rel := range r.Releases {
for _, g = range rel.Genres {
if g.Name != "" {
return g
}
}
}
return g
2023-09-23 17:17:27 -05:00
}
2023-09-24 16:40:24 -05:00
type Genre struct {
Id uuid.UUID
2023-09-23 17:17:27 -05:00
Name string
2023-09-09 21:02:43 -05:00
}
2023-09-24 16:40:24 -05:00
type Release struct {
2023-11-11 17:01:01 -06:00
Id uuid.UUID
Asin string
Barcode string
Country string
Date string
Disambiguation string
Media []Media
Packaging string
PackagingId uuid.UUID `json:"packaging-id"`
Quality string
Status string
StatusId uuid.UUID `json:"status-id"`
ArtistCredit []ArtistCredit `json:"artist-credit"`
Title string
Genres []Genre
ReleaseGroup ReleaseGroup `json:"release-group"`
LabelInfo []LabelInfo `json:"label-info"`
2023-09-24 16:40:24 -05:00
// ReleaseEvents []ReleaseEvent `json:"release-events"`
2023-09-09 21:02:43 -05:00
}
2023-11-11 17:01:01 -06:00
type LabelInfo struct {
CatalogNumber string `json:"catalog-number"`
Label Label
}
type Label struct {
Id uuid.UUID
Name string
SortName string `json:"sort-name"`
Disambiguation string
TypeId string `json:"type-id"`
Type string
LabelCode int `json:"label-code"`
}
type ReleaseGroup struct {
Id uuid.UUID
Title string
ArtistCredit []ArtistCredit `json:"artist-credit"`
Disambiguation string
FirstReleaseDate string `json:"first-release-date"`
PrimaryType string `json:"primary-type"`
PrimaryTypeId uuid.UUID `json:"primary-type-id"`
SecondaryTypes []string `json:"secondary-types"`
SecondaryTypeIds []uuid.UUID `json:"secondary-type-ids"`
}
2023-09-24 16:40:24 -05:00
type ArtistCredit struct {
2023-11-11 17:01:01 -06:00
Name string
Artist Artist
JoinPhrase string
2023-09-23 17:17:27 -05:00
}
2023-09-24 16:40:24 -05:00
type Artist struct {
Id uuid.UUID
2023-09-23 17:17:27 -05:00
Name string
TypeId string `json:"type-id"`
Type string
SortName string `json:"sort-name"`
2023-09-24 16:40:24 -05:00
Genres []Genre
2023-09-23 17:17:27 -05:00
}
2023-09-24 16:40:24 -05:00
type Media struct {
FormatId uuid.UUID `json:"format-id"`
2023-09-09 21:02:43 -05:00
Position int
2023-11-11 17:01:01 -06:00
Title string
2023-09-09 21:02:43 -05:00
TrackOffset int `json:"track-offset"`
Format string
TrackCount int `json:"track-count"`
2023-09-24 16:40:24 -05:00
Tracks []Track
2023-09-09 21:02:43 -05:00
}
2023-09-24 16:40:24 -05:00
type Track struct {
Id uuid.UUID
2023-09-09 21:02:43 -05:00
Number string
Title string
Position int
Length int
}
2023-11-11 17:01:01 -06:00
func GetReleaseWithMedia(id uuid.UUID) (Release, error) {
rel := Release{Id: id}
url := fmt.Sprintf("https://musicbrainz.org/ws/2/release/%s", id)
resp, err := h.GetWithQuery(url, u.Values{
"fmt": []string{"json"},
"inc": []string{"artist-credits+discids+labels+release-groups"}})
if err != nil {
return rel, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return rel, err
}
err = json.Unmarshal(body, &rel)
return rel, err
}
2023-09-24 16:40:24 -05:00
func GetRecording(id string) (Recording, error) {
u, err := uuid.Parse(id)
rec := Recording{Id: u}
if err != nil {
return rec, err
}
err = FillRecording(&rec)
2023-09-09 21:02:43 -05:00
return rec, err
}
2023-09-24 16:40:24 -05:00
func FillRecording(rec *Recording) error {
2023-09-09 21:02:43 -05:00
url := fmt.Sprintf("https://musicbrainz.org/ws/2/recording/%s", rec.Id)
resp, err := h.GetWithQuery(url, u.Values{
"fmt": []string{"json"},
"inc": []string{"releases+media+artist-credits+isrcs+genres"}})
2023-09-09 21:02:43 -05:00
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, rec)
return err
}