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:
parent
343a8c5f45
commit
00b50dab08
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,7 +5,7 @@
|
||||
*.so
|
||||
*.dylib
|
||||
*.pkg
|
||||
/monstersirenfetch
|
||||
/msrfetch
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
54
cmd/msrfetch/enrich.go
Normal file
54
cmd/msrfetch/enrich.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
55
cmd/msrfetch/main.go
Normal file
55
cmd/msrfetch/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
21
cmd/msrfetch/net.go
Normal file
21
cmd/msrfetch/net.go
Normal 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/output/composite.json
vendored
Normal file
1
testdata/output/composite.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1006
testdata/output/composite.log
vendored
Normal file
1006
testdata/output/composite.log
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1154
testdata/output/urls
vendored
Normal file
1154
testdata/output/urls
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user