diff --git a/app/run.go b/app/run.go index 0b29c57..6fabf95 100644 --- a/app/run.go +++ b/app/run.go @@ -38,8 +38,18 @@ func testFp() { quit() } +func testMb() { + id := "497f2f22-809b-4c9e-a692-72f7d8dcaaa2" + mb, err := media.GetMbRecording(id) + if err != nil { + panic(err) + } + fmt.Printf("%+v\n", mb) + quit() +} + func Run(step AppStep) { - testFp() + testMb() for step < Quit { switch step { case Pick: diff --git a/media/brainz.go b/media/brainz.go new file mode 100644 index 0000000..87fb818 --- /dev/null +++ b/media/brainz.go @@ -0,0 +1,78 @@ +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 +} diff --git a/media/fingerprint.go b/media/fingerprint.go index 9954110..f293b05 100644 --- a/media/fingerprint.go +++ b/media/fingerprint.go @@ -54,10 +54,6 @@ type IdError struct { Message string } -type MbRecording struct { - Id string -} - func LookupFingerprint(print FPrint) (IdResults, error) { res := IdResults{} key := config.GetConfig().AcousticIdKey