All checks were successful
Build and push image / deploy (push) Successful in 1m39s
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
|
"gitea.home.4it.me/dilap54/platiparser/healthbeat"
|
|
"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"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
func init() {
|
|
commands = append(commands, platiCommand)
|
|
}
|
|
|
|
var platiCommand = &cli.Command{
|
|
Name: "plati",
|
|
Action: func(c *cli.Context) error {
|
|
timeStart := time.Now()
|
|
|
|
categories := category.OpenCategories("./categories.json").Content
|
|
categories = category.FilterBySubstring("Gift", categories)
|
|
|
|
ctx := context.Background()
|
|
|
|
db := gorm.GetDB()
|
|
platiCli := plati.New(proxies.Default())
|
|
|
|
// printNames(categories)
|
|
|
|
beatUrl := os.Getenv("UPTIMEKUMA_URL")
|
|
|
|
wg, _ := errgroup.WithContext(ctx)
|
|
wg.SetLimit(10)
|
|
|
|
for i, c := range categories {
|
|
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 {
|
|
log.Printf("error: %v", err)
|
|
return fmt.Errorf("getblockgoodscategory: %w", err)
|
|
}
|
|
if len(goods) == 0 {
|
|
return nil
|
|
}
|
|
log.Printf("inserting %d goods to DB", len(goods))
|
|
|
|
gormGoods := convertGoodsToGorm(cat, goods)
|
|
if err := db.Create(gormGoods).Error; err != nil {
|
|
log.Printf("error: %v", err)
|
|
return fmt.Errorf("db Create: %w", err)
|
|
}
|
|
json.NewEncoder(os.Stdout).Encode(goods)
|
|
|
|
return nil
|
|
})
|
|
|
|
}
|
|
|
|
if err := wg.Wait(); err != nil {
|
|
healthbeat.Beat(beatUrl, "down", err.Error(), int(time.Since(timeStart).Milliseconds()))
|
|
return err
|
|
}
|
|
|
|
healthbeat.Beat(beatUrl, "up", "OK", int(time.Since(timeStart).Milliseconds()))
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func convertGoodsToGorm(cat *plati.Category, goods []*plati.Good) []*gorm.Good {
|
|
out := make([]*gorm.Good, 0, len(goods))
|
|
|
|
for _, g := range goods {
|
|
gormGood := gorm.Good{
|
|
ID: uuid.NewV4().String(),
|
|
Name: g.Name,
|
|
IDC: cat.ID,
|
|
IDR: cat.ParentID,
|
|
Goodlink: g.GoodLink,
|
|
Seller: g.Seller,
|
|
Sellerlink: g.SellerLink,
|
|
Sellerrating: g.SellerRating,
|
|
Sold: g.Sold,
|
|
Price: g.Price,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
out = append(out, &gormGood)
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func printNames(categories plati.Categories) {
|
|
for _, c := range categories {
|
|
if len(c.Children) > 0 {
|
|
printNames(c.Children)
|
|
continue
|
|
}
|
|
fmt.Printf("%s\n", c.FlatName)
|
|
}
|
|
}
|