album: extend struct for /api/album/%d/data
This also includes tests against a sample response from the https://monster-siren.hypergryph.com/api/album/1030/data endpoint. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
"slices"
|
||||
|
||||
"git.gensokyo.uk/yonah/monstersirenfetch"
|
||||
)
|
||||
@@ -36,8 +37,28 @@ func mustEnrich(ctx context.Context) {
|
||||
} else {
|
||||
n := new(netDirect)
|
||||
for _, ca := range c {
|
||||
log.Printf("enriching album %s (%s)", ca.Name, ca.CID.String())
|
||||
if ca.Album == nil {
|
||||
log.Fatal("albums contains nil")
|
||||
}
|
||||
|
||||
if !ca.Album.IsFull() {
|
||||
if err = ca.Album.Enrich(ctx, n); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("enriched album %s (%s) with %d songs", ca.Album.CID.String(), ca.Album.Name, len(ca.Songs))
|
||||
} else {
|
||||
log.Printf("skipped album %s (%s)", ca.Album.CID.String(), ca.Album.Name)
|
||||
}
|
||||
|
||||
// consistency check: for later validating enriched songs against flatten
|
||||
flattenSongs := make([]monstersirenfetch.AlbumSong, 0, len(ca.Songs))
|
||||
|
||||
for _, cs := range ca.Songs {
|
||||
if cs == nil {
|
||||
log.Fatal("songs contains nil")
|
||||
}
|
||||
flattenSongs = append(flattenSongs, monstersirenfetch.AlbumSong{CID: cs.CID, Name: cs.Name, Artists: cs.Artists})
|
||||
|
||||
if !cs.IsFull() {
|
||||
if err = cs.Enrich(ctx, n); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -47,6 +68,19 @@ func mustEnrich(ctx context.Context) {
|
||||
log.Printf("skipped song %s: %s (%s)", cs.CID.String(), cs.SourceURL, cs.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// consistency check: enriched songs match flattened songs
|
||||
slices.SortFunc(flattenSongs, func(a, b monstersirenfetch.AlbumSong) int { return int(a.CID - b.CID) })
|
||||
enrichSongs := make([]monstersirenfetch.AlbumSong, len(ca.Album.Songs))
|
||||
copy(enrichSongs, ca.Album.Songs)
|
||||
slices.SortFunc(enrichSongs, func(a, b monstersirenfetch.AlbumSong) int { return int(a.CID - b.CID) })
|
||||
if !slices.EqualFunc(flattenSongs, enrichSongs, func(a monstersirenfetch.AlbumSong, b monstersirenfetch.AlbumSong) bool {
|
||||
return a.CID == b.CID && a.Name == b.Name && slices.Equal(a.Artists, b.Artists)
|
||||
}) {
|
||||
log.Fatalf("album %s enrichment inconsistent with flatten state", ca.Album.Name)
|
||||
} else {
|
||||
log.Printf("validated %d songs associated with album %s", len(enrichSongs), ca.Album.CID.String())
|
||||
}
|
||||
}
|
||||
mustWriteJSON(flagOutputPath, c)
|
||||
log.Println("composite data written to", flagOutputPath)
|
||||
|
||||
@@ -32,6 +32,7 @@ func mustFetch(ctx context.Context) {
|
||||
|
||||
const (
|
||||
invalidContainsNil = "invalid composite data"
|
||||
invalidNotEnriched = "this composite is not enriched"
|
||||
)
|
||||
|
||||
var urls []string
|
||||
@@ -39,17 +40,23 @@ func mustFetch(ctx context.Context) {
|
||||
if ca.Album == nil {
|
||||
log.Fatal(invalidContainsNil)
|
||||
}
|
||||
if ca.CoverURL == "" {
|
||||
log.Fatalf("album %s missing coverUrl", ca.CID.String())
|
||||
if !ca.Album.IsFull() {
|
||||
log.Fatal(invalidNotEnriched)
|
||||
}
|
||||
if ca.Album.CoverURL == "" {
|
||||
log.Fatalf("album %s missing coverUrl", ca.Album.CID.String())
|
||||
}
|
||||
urls = append(urls, ca.Album.CoverURL)
|
||||
if ca.Album.CoverDeURL != "" {
|
||||
urls = append(urls, ca.Album.CoverDeURL)
|
||||
}
|
||||
urls = append(urls, ca.CoverURL)
|
||||
|
||||
for _, cs := range ca.Songs {
|
||||
if cs == nil {
|
||||
log.Fatal(invalidContainsNil)
|
||||
}
|
||||
if !cs.IsFull() {
|
||||
log.Fatal("this composite is not enriched")
|
||||
log.Fatal(invalidNotEnriched)
|
||||
}
|
||||
|
||||
urls = append(urls, cs.SourceURL)
|
||||
|
||||
Reference in New Issue
Block a user