94 lines
1.8 KiB
Go
94 lines
1.8 KiB
Go
package media
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
u "net/url"
|
|
|
|
h "codeberg.org/danjones000/strip-beats/utils/http"
|
|
)
|
|
|
|
type MbRecording struct {
|
|
Id string
|
|
Isrcs []string
|
|
FirstReleaseDate string `json:"first-release-date"`
|
|
Length int
|
|
Title string
|
|
Video bool
|
|
Releases []MbRelease
|
|
Genres []MbGenre
|
|
}
|
|
|
|
type MbGenre struct {
|
|
Id string
|
|
Name string
|
|
}
|
|
|
|
type MbRelease struct {
|
|
Id string
|
|
Country string
|
|
Date string
|
|
Media []MbMedia
|
|
Status string
|
|
StatusId string `json:"status-id"`
|
|
ArtistCredit []MbArtistCredit `json:"artist-credit"`
|
|
Title string
|
|
Genres []MbGenre
|
|
// ReleaseEvents []MbReleaseEvent `json:"release-events"`
|
|
}
|
|
|
|
type MbArtistCredit struct {
|
|
Name string
|
|
Artist MbArtist
|
|
}
|
|
|
|
type MbArtist struct {
|
|
Id string
|
|
Name string
|
|
TypeId string `json:"type-id"`
|
|
Type string
|
|
SortName string `json:"sort-name"`
|
|
Genres []MbGenre
|
|
}
|
|
|
|
type MbMedia struct {
|
|
FormatId string `json:"format-id"`
|
|
Position int
|
|
TrackOffset int `json:"track-offset"`
|
|
Format string
|
|
TrackCount int `json:"track-count"`
|
|
Tracks []MbTrack
|
|
}
|
|
|
|
type MbTrack struct {
|
|
Id string
|
|
Number string
|
|
Title string
|
|
Position int
|
|
Length int
|
|
}
|
|
|
|
func GetMbRecording(id string) (MbRecording, error) {
|
|
rec := MbRecording{Id: id}
|
|
err := FillMbRecording(&rec)
|
|
return rec, err
|
|
}
|
|
|
|
func FillMbRecording(rec *MbRecording) error {
|
|
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"}})
|
|
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
|
|
}
|