All checks were successful
Build and push image / deploy (push) Successful in 1m44s
91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
|
"gitea.home.4it.me/dilap54/platiparser/internal/category"
|
|
"gitea.home.4it.me/dilap54/platiparser/plati"
|
|
"gitea.home.4it.me/dilap54/platiparser/proxies"
|
|
uuid "github.com/satori/go.uuid"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func init() {
|
|
commands = append(commands, sumpayCommand)
|
|
}
|
|
|
|
var sumpayCommand = &cli.Command{
|
|
Name: "sumpay",
|
|
Action: func(c *cli.Context) error {
|
|
categories := category.OpenCategories("./categories.json").Content
|
|
categories = category.FilterBySubstring("Gift", categories)
|
|
|
|
cats := map[int]struct{}{}
|
|
for _, c := range categories {
|
|
cats[c.ParentID] = struct{}{}
|
|
}
|
|
catsArr := make([]int, 0, len(cats))
|
|
for cId := range cats {
|
|
catsArr = append(catsArr, cId)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
db := gorm.GetDB()
|
|
platiCli := plati.New(proxies.Default())
|
|
|
|
// printNames(categories)
|
|
|
|
for i, categoryId := range catsArr {
|
|
for page := 1; page < 100; page++ {
|
|
log.Printf("fetching goods [%d/%d], page %d for %d\n", i, len(catsArr), page, categoryId)
|
|
goods, err := platiCli.GetGoods(ctx, categoryId, "ru-RU", page, 500, "RUR")
|
|
if err != nil {
|
|
return fmt.Errorf("GetGoods: %w", err)
|
|
}
|
|
log.Printf("inserting %d goods to DB", len(goods.Rows.Row))
|
|
|
|
if len(goods.Rows.Row) < 500 {
|
|
break
|
|
}
|
|
|
|
gormGoods := convertDigiGoodsToGorm(goods)
|
|
if err := db.Create(gormGoods).Error; err != nil {
|
|
return fmt.Errorf("db Create: %w", err)
|
|
}
|
|
log.Printf("inserted %d goods to DB", len(gormGoods))
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func convertDigiGoodsToGorm(goods *plati.GoodsResponse) []*gorm.DigiGood {
|
|
out := make([]*gorm.DigiGood, 0, len(goods.Rows.Row))
|
|
|
|
for _, g := range goods.Rows.Row {
|
|
gormGood := gorm.DigiGood{
|
|
ID: uuid.NewV4().String(),
|
|
IDGoods: g.IDGoods,
|
|
Name: g.NameGoods,
|
|
IDSection: goods.IDSection,
|
|
SellerID: g.IDSeller,
|
|
Sellerrating: g.Rating.Float64(),
|
|
Sold: g.Statistics.CntSell,
|
|
Returned: g.Statistics.CntReturn,
|
|
Price: g.Price.Float64(),
|
|
Sumpay: g.Summpay.Float64(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
out = append(out, &gormGood)
|
|
}
|
|
|
|
return out
|
|
}
|