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>
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
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)
|
|
}
|
|
}
|