134 lines
2.8 KiB
Go
134 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
|
"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() {
|
|
commands = append(commands, platiCommand)
|
|
}
|
|
|
|
var platiCommand = &cli.Command{
|
|
Name: "plati",
|
|
Action: func(c *cli.Context) error {
|
|
categories := openCategories("./categories.json").Content
|
|
categories = filterBySubstring("Gift", categories)
|
|
|
|
ctx := context.Background()
|
|
|
|
db := gorm.GetDB()
|
|
platiCli := plati.New()
|
|
|
|
// printNames(categories)
|
|
|
|
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 {
|
|
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 {
|
|
return fmt.Errorf("db Create: %w", err)
|
|
}
|
|
json.NewEncoder(os.Stdout).Encode(goods)
|
|
|
|
return nil
|
|
})
|
|
|
|
}
|
|
|
|
return wg.Wait()
|
|
|
|
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 filterBySubstring(substring string, categories plati.Categories) plati.Categories {
|
|
out := make(plati.Categories, 0)
|
|
for _, c := range categories {
|
|
if len(c.Children) > 0 {
|
|
out = append(out, filterBySubstring(substring, c.Children)...)
|
|
continue
|
|
}
|
|
|
|
if strings.Contains(c.FlatName, substring) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func openCategories(fileName string) plati.CategoriesResponse {
|
|
f, err := os.OpenFile(fileName, os.O_RDONLY, 0400)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
out := plati.CategoriesResponse{}
|
|
if err := json.NewDecoder(f).Decode(&out); err != nil {
|
|
panic(err)
|
|
}
|
|
out.Content.FlatNames("")
|
|
out.Content.FixParentID(0)
|
|
return out
|
|
}
|