diff --git a/cmd/horsebot/toradl.go b/cmd/horsebot/toradl.go index af5587a..da678fd 100644 --- a/cmd/horsebot/toradl.go +++ b/cmd/horsebot/toradl.go @@ -15,7 +15,6 @@ import ( "github.com/gen2brain/avif" "golang.org/x/image/draw" - "golang.org/x/sync/errgroup" "zombiezen.com/go/sqlite" "zombiezen.com/go/sqlite/sqlitex" ) @@ -38,16 +37,16 @@ func saveImgs(ctx context.Context, db *sqlitex.Pool, out string) { slog.Error("supports", slog.Any("err", err)) } - g, ctx := errgroup.WithContext(ctx) - g.SetLimit(4) tick := time.NewTicker(250 * time.Millisecond) - idouts(ctx, g, tick, skill, filepath.Join(out, "skill"), "https://media.gametora.com/umamusume/skills/icon/%d.png", image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)) - idouts(ctx, g, tick, chara, filepath.Join(out, "chara"), "https://gametora.com/images/umamusume/characters/icons/chr_icon_%d.png", image.Pt(256, 256), image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)) - idouts(ctx, g, tick, rank, filepath.Join(out, "rank"), "https://media.gametora.com/umamusume/ui/rank/%02d.png", image.Pt(128, 128), image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)) - idouts(ctx, g, tick, rank, filepath.Join(out, "rank", "simple"), "https://media.gametora.com/umamusume/ui/rank/simple/%02d.png", image.Pt(50, 50), image.Pt(32, 32), image.Pt(16, 16)) - idouts(ctx, g, tick, supps, filepath.Join(out, "support"), "https://gametora.com/images/umamusume/supports/support_card_s_%d.png", image.Pt(128, 128), image.Pt(64, 64), image.Pt(32, 32)) - idouts(ctx, g, tick, []int32{0, 1, 2, 3, 4, 5, 6}, filepath.Join(out, "support", "type"), "https://gametora.com/images/umamusume/icons/utx_ico_obtain_%02d.png", image.Pt(70, 70), image.Pt(35, 35), image.Pt(16, 16)) - if err := g.Wait(); err != nil { + err = errors.Join( + idouts(ctx, tick, skill, filepath.Join(out, "skill"), "https://media.gametora.com/umamusume/skills/icon/%d.png", image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)), + idouts(ctx, tick, chara, filepath.Join(out, "chara"), "https://gametora.com/images/umamusume/characters/icons/chr_icon_%d.png", image.Pt(256, 256), image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)), + idouts(ctx, tick, rank, filepath.Join(out, "rank"), "https://media.gametora.com/umamusume/ui/rank/%02d.png", image.Pt(128, 128), image.Pt(64, 64), image.Pt(32, 32), image.Pt(16, 16)), + idouts(ctx, tick, rank, filepath.Join(out, "rank", "simple"), "https://media.gametora.com/umamusume/ui/rank/simple/%02d.png", image.Pt(50, 50), image.Pt(32, 32), image.Pt(16, 16)), + idouts(ctx, tick, supps, filepath.Join(out, "support"), "https://gametora.com/images/umamusume/supports/support_card_s_%d.png", image.Pt(128, 128), image.Pt(64, 64), image.Pt(32, 32)), + idouts(ctx, tick, []int32{0, 1, 2, 3, 4, 5, 6}, filepath.Join(out, "support", "type"), "https://gametora.com/images/umamusume/icons/utx_ico_obtain_%02d.png", image.Pt(70, 70), image.Pt(35, 35), image.Pt(16, 16)), + ) + if err != nil { slog.Error("output", slog.Any("err", err)) } } @@ -103,58 +102,61 @@ func suppIDs(ctx context.Context, db *sqlitex.Pool) ([]int32, error) { var pngenc = &png.Encoder{CompressionLevel: png.BestCompression} -func idouts(ctx context.Context, g *errgroup.Group, tick *time.Ticker, ids []int32, p, urlf string, sizes ...image.Point) { +func idouts(ctx context.Context, tick *time.Ticker, ids []int32, p, urlf string, sizes ...image.Point) error { for _, id := range ids { - g.Go(func() error { - n := strconv.Itoa(int(id)) - p := filepath.Join(p, n) - _, err := os.Stat(p) - if !errors.Is(err, os.ErrNotExist) { - slog.DebugContext(ctx, "exists", slog.String("path", p)) - return nil - } - if err := os.MkdirAll(p, 0777); err != nil { - return err - } - url := fmt.Sprintf(urlf, id) - select { - case <-ctx.Done(): - return ctx.Err() - case <-tick.C: // continue on - } - img, err := dlimg(ctx, url) - if err != nil { - return err - } - slog.InfoContext(ctx, "resize", slog.String("url", url)) - rs := resize(img, sizes...) - for _, r := range rs { - g.Go(func() error { - m := r.Bounds().Max - o := filepath.Join(p, fmt.Sprintf("%dx%d.png", m.X, m.Y)) - slog.InfoContext(ctx, "write", slog.String("path", o)) - f, err := os.Create(o) - if err != nil { - return err - } - defer f.Close() - return pngenc.Encode(f, r) - }) - g.Go(func() error { - m := r.Bounds().Max - o := filepath.Join(p, fmt.Sprintf("%dx%d.avif", m.X, m.Y)) - slog.InfoContext(ctx, "write", slog.String("path", o)) - f, err := os.Create(o) - if err != nil { - return err - } - defer f.Close() - return avif.Encode(f, r) - }) - } + n := strconv.Itoa(int(id)) + p := filepath.Join(p, n) + _, err := os.Stat(p) + if !errors.Is(err, os.ErrNotExist) { + slog.DebugContext(ctx, "exists", slog.String("path", p)) return nil - }) + } + if err := os.MkdirAll(p, 0777); err != nil { + return err + } + url := fmt.Sprintf(urlf, id) + select { + case <-ctx.Done(): + return ctx.Err() + case <-tick.C: // continue on + } + img, err := dlimg(ctx, url) + if err != nil { + return err + } + slog.InfoContext(ctx, "resize", slog.String("url", url)) + for _, sz := range sizes { + r := image.NewNRGBA(image.Rectangle{Min: image.Point{}, Max: sz}) + draw.CatmullRom.Scale(r, r.Bounds(), img, img.Bounds(), draw.Src, nil) + { + m := r.Bounds().Max + o := filepath.Join(p, fmt.Sprintf("%dx%d.png", m.X, m.Y)) + slog.InfoContext(ctx, "write", slog.String("path", o)) + f, err := os.Create(o) + if err != nil { + return err + } + defer f.Close() + if err := pngenc.Encode(f, r); err != nil { + return err + } + } + { + m := r.Bounds().Max + o := filepath.Join(p, fmt.Sprintf("%dx%d.avif", m.X, m.Y)) + slog.InfoContext(ctx, "write", slog.String("path", o)) + f, err := os.Create(o) + if err != nil { + return err + } + defer f.Close() + if err := avif.Encode(f, r); err != nil { + return err + } + } + } } + return nil } func dlimg(ctx context.Context, url string) (image.Image, error) { @@ -174,13 +176,3 @@ func dlimg(ctx context.Context, url string) (image.Image, error) { img, _, err := image.Decode(resp.Body) return img, err } - -func resize(img image.Image, sizes ...image.Point) []image.Image { - rs := make([]image.Image, 0, len(sizes)) - for _, sz := range sizes { - d := image.NewNRGBA(image.Rectangle{Min: image.Point{}, Max: sz}) - draw.CatmullRom.Scale(d, d.Bounds(), img, img.Bounds(), draw.Src, nil) - rs = append(rs, d) - } - return rs -}