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:
+1
-1
@@ -5,7 +5,7 @@
|
||||
*.so
|
||||
*.dylib
|
||||
*.pkg
|
||||
/monstersirenfetch
|
||||
/msrfetch
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
|
||||
"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 mustEnrich(ctx context.Context) {
|
||||
var (
|
||||
albumsResponse monstersirenfetch.AlbumsResponse
|
||||
songsResponse monstersirenfetch.SongsResponse
|
||||
)
|
||||
mustReadJSON(flagAlbumsPath, &albumsResponse)
|
||||
mustReadJSON(flagSongsPath, &songsResponse)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.gensokyo.uk/yonah/monstersirenfetch"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
log.SetPrefix("msrfetch: ")
|
||||
flag.Parse()
|
||||
|
||||
if flag.NArg() < 1 {
|
||||
log.Fatal("no subcommand specified")
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
switch flag.Args()[0] {
|
||||
case "enrich":
|
||||
mustEnrich(ctx)
|
||||
|
||||
default:
|
||||
log.Fatalf("%q is not a valid command", flag.Args()[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Vendored
+1
File diff suppressed because one or more lines are too long
Vendored
+1006
File diff suppressed because it is too large
Load Diff
Vendored
+1154
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user