78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package media
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
//"codeberg.org/danjones000/strip-beats/config"
|
|
)
|
|
|
|
type MbRecording struct {
|
|
Id string
|
|
FirstReleaseDate string `json:"first-release-date"`
|
|
Length int
|
|
Title string
|
|
Video bool
|
|
Releases []MbRelease
|
|
}
|
|
|
|
type MbRelease struct {
|
|
Id string
|
|
Country string
|
|
Date string
|
|
Media []MbMedia
|
|
Status string
|
|
StatusId string `json:"status-id"`
|
|
Title string
|
|
// ReleaseEvents []MbReleaseEvent `json:"release-events"`
|
|
}
|
|
|
|
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)
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
q := req.URL.Query()
|
|
q.Add("fmt", "json")
|
|
q.Add("inc", "releases+media")
|
|
req.URL.RawQuery = q.Encode()
|
|
req.Header.Set("User-Agent", "strip-beats/0.1.0 (https://codeberg.org/danjones000/strip-beats/)")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
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
|
|
}
|