119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
|
"gitea.home.4it.me/dilap54/platiparser/plati"
|
|
"github.com/urfave/cli/v2"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
func init() {
|
|
commands = append(commands, digiCommand)
|
|
}
|
|
|
|
var digiCommand = &cli.Command{
|
|
Name: "digi",
|
|
Action: func(c *cli.Context) error {
|
|
ctx := context.Background()
|
|
|
|
platiCli := plati.New()
|
|
|
|
db := gorm.GetDB()
|
|
|
|
categories, err := platiCli.GetCategories(ctx)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
categories.Content.FlatNames("")
|
|
|
|
filtered := filterCategories("/Ключи и пин-коды/Игры/", categories.Content)
|
|
|
|
//json.NewEncoder(os.Stdout).Encode(categories)
|
|
for i, f := range filtered {
|
|
log.Printf("fetching [%d/%d] %s...\n", i, len(filtered), f.FlatName)
|
|
subCategories, err := platiCli.GetSubCategories(ctx, f.ID)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
f.Children = subCategories.Content
|
|
}
|
|
|
|
filtered.FixParentID(0)
|
|
filtered.FlatNames("")
|
|
|
|
if err := saveCategories(db, filtered); err != nil {
|
|
return fmt.Errorf("saveCategories: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func saveCategories(db *gorm.DB, categories plati.Categories) error {
|
|
if len(categories) == 0 {
|
|
return nil
|
|
}
|
|
|
|
gormCategories := make([]*gorm.Category, 0, len(categories))
|
|
|
|
for _, c := range categories {
|
|
if len(c.Children) > 0 {
|
|
if err := saveCategories(db, c.Children); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
gormCategories = append(gormCategories, &gorm.Category{
|
|
ID: c.ID,
|
|
ParentID: c.ParentID,
|
|
Title: c.Title,
|
|
Name: c.GetName(),
|
|
FlatName: c.FlatName,
|
|
Level: c.Level,
|
|
})
|
|
|
|
}
|
|
|
|
log.Printf("saving %d categories to db", len(gormCategories))
|
|
|
|
if err := db.Clauses(clause.OnConflict{DoNothing: true}).Create(gormCategories).Error; err != nil {
|
|
return fmt.Errorf("db Create: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func filterCategories(prefix string, categories plati.Categories) plati.Categories {
|
|
out := make(plati.Categories, 0)
|
|
for _, c := range categories {
|
|
if len(c.Children) > 0 {
|
|
out = append(out, filterCategories(prefix, c.Children)...)
|
|
continue
|
|
}
|
|
|
|
if strings.HasPrefix(c.FlatName, prefix) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func printCategories(nameOfParent string, categories []*plati.Category) {
|
|
for _, c := range categories {
|
|
name := ""
|
|
if len(c.Name) > 0 {
|
|
name = c.Name[0].Value
|
|
}
|
|
fmt.Printf("%s/%s\n", nameOfParent, name)
|
|
if len(c.Children) > 0 {
|
|
printCategories(nameOfParent+"/"+name, c.Children)
|
|
}
|
|
}
|
|
}
|