This commit is contained in:
2023-12-21 21:32:31 +03:00
parent 0d727a4011
commit a6f51d94a0
3 changed files with 29 additions and 13 deletions

View File

@ -13,6 +13,7 @@ import (
"gitea.home.4it.me/dilap54/platiparser/plati"
uuid "github.com/satori/go.uuid"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
func init() {
@ -32,22 +33,34 @@ var platiCommand = &cli.Command{
// printNames(categories)
wg, _ := errgroup.WithContext(ctx)
wg.SetLimit(30)
for i, c := range categories {
log.Printf("fetching goods [%d/%d] for %s\n", i, len(categories), c.FlatName)
goods, err := platiCli.GetBlockGoodsCategory(ctx, c.ID, c.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU")
if err != nil {
return fmt.Errorf("getblockgoodscategory: %w", err)
}
if len(goods) == 0 {
continue
}
gormGoods := convertGoodsToGorm(c, goods)
if err := db.Create(gormGoods).Error; err != nil {
return fmt.Errorf("db Create: %w", err)
}
json.NewEncoder(os.Stdout).Encode(goods)
l := i
cat := c
wg.Go(func() error {
log.Printf("fetching goods [%d/%d] for %s\n", l, len(categories), c.FlatName)
goods, err := platiCli.GetBlockGoodsCategory(ctx, cat.ID, cat.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU")
if err != nil {
return fmt.Errorf("getblockgoodscategory: %w", err)
}
if len(goods) == 0 {
return nil
}
gormGoods := convertGoodsToGorm(cat, goods)
if err := db.Create(gormGoods).Error; err != nil {
return fmt.Errorf("db Create: %w", err)
}
json.NewEncoder(os.Stdout).Encode(goods)
return nil
})
}
return wg.Wait()
return nil
},
}

1
go.mod
View File

@ -25,5 +25,6 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

2
go.sum
View File

@ -53,6 +53,8 @@ golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=