strip-beats/media/brainz/brainz.go

100 lines
1.9 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-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-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 {
Id uuid.UUID
2023-09-23 17:17:27 -05:00
Country string
Date string
2023-09-24 16:40:24 -05:00
Media []Media
2023-09-23 17:17:27 -05:00
Status string
2023-09-24 16:40:24 -05:00
StatusId uuid.UUID `json:"status-id"`
ArtistCredit []ArtistCredit `json:"artist-credit"`
2023-09-23 17:17:27 -05:00
Title string
2023-09-24 16:40:24 -05:00
Genres []Genre
// ReleaseEvents []ReleaseEvent `json:"release-events"`
2023-09-09 21:02:43 -05:00
}
2023-09-24 16:40:24 -05:00
type ArtistCredit struct {
2023-09-23 17:17:27 -05:00
Name string
2023-09-24 16:40:24 -05:00
Artist Artist
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
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-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
}