cmd/msrfetch: save enriched compound data

This implementation of cmd/msrfetch is only temporary, used to fetch the
testdata. At this point we have all metadata present in the repo and all
that is left is to fetch the media.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
Yonah 2025-09-18 05:19:40 +09:00
parent 343a8c5f45
commit 33ea80765c
Signed by: yonah
SSH Key Fingerprint: SHA256:vnQvK8+XXH9Tbni2AV1a/8qdVK/zPcXw52GM0ruQvwA
5 changed files with 1113 additions and 1 deletions

2
.gitignore vendored
View File

@ -5,7 +5,7 @@
*.so *.so
*.dylib *.dylib
*.pkg *.pkg
/monstersirenfetch /msrfetch
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

84
cmd/msrfetch/main.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"os/signal"
"syscall"
"git.gensokyo.uk/yonah/monstersirenfetch"
)
var (
flagAlbumsPath string
flagSongsPath string
flagOutputPath string
)
func init() {
flag.StringVar(&flagAlbumsPath, "a", "albums.json",
"Path to file containing the response body of /api/albums")
flag.StringVar(&flagSongsPath, "s", "songs.json",
"Path to file containing the response body of /api/songs")
flag.StringVar(&flagOutputPath, "o", "composite.json",
"Path to write composite data")
}
func main() {
log.SetFlags(0)
log.SetPrefix("msrfetch: ")
var (
albumsResponse monstersirenfetch.AlbumsResponse
songsResponse monstersirenfetch.SongsResponse
)
mustReadJSON(flagAlbumsPath, &albumsResponse)
mustReadJSON(flagSongsPath, &songsResponse)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if c, err := monstersirenfetch.Flatten(albumsResponse.Data, songsResponse.Data); err != nil {
log.Fatal(err)
} else {
n := new(netDirect)
for _, ca := range c {
log.Printf("enriching album %s (%s)", ca.Name, ca.CID.String())
for _, cs := range ca.Songs {
if !cs.IsFull() {
if err = cs.Enrich(ctx, n); err != nil {
log.Fatal(err)
}
log.Printf("enriched song %s: %s (%s)", cs.CID.String(), cs.SourceURL, cs.Name)
} else {
log.Printf("skipped song %s: %s (%s)", cs.CID.String(), cs.SourceURL, cs.Name)
}
}
}
mustWriteJSON(flagOutputPath, c)
log.Println("composite data written to", flagOutputPath)
}
}
func mustWriteJSON(pathname string, v any) {
if w, err := os.Create(pathname); err != nil {
log.Fatal(err)
} else if err = monstersirenfetch.NewEncoder(w).Encode(v); err != nil {
log.Fatal(err)
} else if err = w.Close(); err != nil {
log.Fatal(err)
}
}
func mustReadJSON(pathname string, v any) {
if r, err := os.OpenFile(pathname, os.O_RDONLY, 0); err != nil {
log.Fatal(err)
} else if err = json.NewDecoder(r).Decode(v); err != nil {
log.Fatal(err)
} else if err = r.Close(); err != nil {
log.Fatal(err)
}
}

21
cmd/msrfetch/net.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"context"
"io"
"net/http"
)
type netDirect struct {
c http.Client
}
func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, error) {
var resp *http.Response
if req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil); err != nil {
return nil, err
} else if resp, err = n.c.Do(req); err != nil {
return nil, err
}
return resp.Body, nil
}

1
testdata/composite.json vendored Normal file

File diff suppressed because one or more lines are too long

1006
testdata/composite.log vendored Normal file

File diff suppressed because it is too large Load Diff