song: struct for /api/song/%d
This also includes tests against a sample response from the https://monster-siren.hypergryph.com/api/song/048794 endpoint. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
parent
c2c4171a3a
commit
ea54772c03
55
song.go
Normal file
55
song.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package monstersirenfetch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SongResponse is the response of /api/song/%d.
|
||||||
|
type SongResponse Response[Song]
|
||||||
|
|
||||||
|
// Song holds the metadata of a song.
|
||||||
|
type Song struct {
|
||||||
|
CID StringInt `json:"cid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
AlbumCID StringInt `json:"albumCid"`
|
||||||
|
SourceURL string `json:"sourceUrl,omitempty"`
|
||||||
|
LyricURL string `json:"lyricUrl,omitempty"`
|
||||||
|
MvURL string `json:"mvUrl,omitempty"`
|
||||||
|
MvCoverURL string `json:"mvCoverUrl,omitempty"`
|
||||||
|
Artists []string `json:"artists"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFull returns whether the metadata held by [Song] is considered full (originating from a [SongResponse]).
|
||||||
|
func (s *Song) IsFull() bool { return s.SourceURL != "" }
|
||||||
|
|
||||||
|
// songDirect is [Song] without its MarshalJSON method.
|
||||||
|
type songDirect Song
|
||||||
|
|
||||||
|
// songNullable is [Song] with corresponding nullable string fields.
|
||||||
|
type songNullable struct {
|
||||||
|
CID StringInt `json:"cid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
AlbumCID StringInt `json:"albumCid"`
|
||||||
|
SourceURL string `json:"sourceUrl"`
|
||||||
|
LyricURL NullableString `json:"lyricUrl"`
|
||||||
|
MvURL NullableString `json:"mvUrl"`
|
||||||
|
MvCoverURL NullableString `json:"mvCoverUrl"`
|
||||||
|
Artists []string `json:"artists"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Song) MarshalJSON() (data []byte, err error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
e := json.NewEncoder(buf)
|
||||||
|
e.SetEscapeHTML(false)
|
||||||
|
|
||||||
|
if !s.IsFull() {
|
||||||
|
err = e.Encode((*songDirect)(s))
|
||||||
|
data = buf.Bytes()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(&songNullable{s.CID, s.Name, s.AlbumCID, s.SourceURL,
|
||||||
|
NullableString(s.LyricURL), NullableString(s.MvURL), NullableString(s.MvCoverURL),
|
||||||
|
s.Artists})
|
||||||
|
}
|
23
song_test.go
Normal file
23
song_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package monstersirenfetch_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "git.gensokyo.uk/yonah/monstersirenfetch"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed testdata/song.json
|
||||||
|
var songJSON []byte
|
||||||
|
|
||||||
|
func TestSong(t *testing.T) {
|
||||||
|
checkJSONRoundTrip(t, SongResponse{Data: Song{
|
||||||
|
CID: 48794,
|
||||||
|
Name: "Warm and Small Light",
|
||||||
|
AlbumCID: 6660,
|
||||||
|
SourceURL: "https://res01.hycdn.cn/04ce5de54bb52eb85008644d541d40fa/68CA0442/siren/audio/20240709/a7f650238eaefc9c30a9627d7f78d819.wav",
|
||||||
|
LyricURL: "https://web.hycdn.cn/siren/lyric/20240709/4a10c70629b68a187fdbef4a27bd32d8.lrc",
|
||||||
|
MvURL: "", MvCoverURL: "",
|
||||||
|
Artists: []string{"塞壬唱片-MSR"},
|
||||||
|
}}, songJSON)
|
||||||
|
}
|
8
songs.go
8
songs.go
@ -8,11 +8,3 @@ type SongsData = struct {
|
|||||||
List []Song `json:"list"`
|
List []Song `json:"list"`
|
||||||
Autoplay string `json:"autoplay"`
|
Autoplay string `json:"autoplay"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Song represents the metadata of a song.
|
|
||||||
type Song struct {
|
|
||||||
CID StringInt `json:"cid"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
AlbumCID StringInt `json:"albumCid"`
|
|
||||||
Artists []string `json:"artists"`
|
|
||||||
}
|
|
||||||
|
1
testdata/song.json
vendored
Normal file
1
testdata/song.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"code":0,"msg":"","data":{"cid":"048794","name":"Warm and Small Light","albumCid":"6660","sourceUrl":"https://res01.hycdn.cn/04ce5de54bb52eb85008644d541d40fa/68CA0442/siren/audio/20240709/a7f650238eaefc9c30a9627d7f78d819.wav","lyricUrl":"https://web.hycdn.cn/siren/lyric/20240709/4a10c70629b68a187fdbef4a27bd32d8.lrc","mvUrl":null,"mvCoverUrl":null,"artists":["塞壬唱片-MSR"]}}
|
Loading…
x
Reference in New Issue
Block a user