From ed1716232a125c8324a5aec27f98a5542493699a Mon Sep 17 00:00:00 2001 From: dilap54 Date: Thu, 21 Dec 2023 20:52:47 +0300 Subject: [PATCH] categories to db --- cmd/cli/digi.go | 118 ++++++++++++++++++++++++++++++++++++++++++++ cmd/digi/digi.go | 72 --------------------------- gorm/category.go | 14 ++++++ plati/categories.go | 14 ++++++ 4 files changed, 146 insertions(+), 72 deletions(-) create mode 100644 cmd/cli/digi.go delete mode 100644 cmd/digi/digi.go create mode 100644 gorm/category.go diff --git a/cmd/cli/digi.go b/cmd/cli/digi.go new file mode 100644 index 0000000..dee04f8 --- /dev/null +++ b/cmd/cli/digi.go @@ -0,0 +1,118 @@ +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" +) + +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 + } + continue + } + + 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.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) + } + } +} diff --git a/cmd/digi/digi.go b/cmd/digi/digi.go deleted file mode 100644 index 88db2bd..0000000 --- a/cmd/digi/digi.go +++ /dev/null @@ -1,72 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "log" - "os" - "strings" - - "gitea.home.4it.me/dilap54/platiparser/plati" -) - -func main() { - // вытащить все подкатегории с названием steamgift - // разное/игры это тоже подкатегория - // вытащить все разделы - // для каждого для каждой игры сохранить никнейм продавца - - ctx := context.Background() - - platiCli := plati.New() - - 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 - } - - json.NewEncoder(os.Stdout).Encode(categories) -} - -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) - } - } -} diff --git a/gorm/category.go b/gorm/category.go new file mode 100644 index 0000000..5d6a1ad --- /dev/null +++ b/gorm/category.go @@ -0,0 +1,14 @@ +package gorm + +type Category struct { + ID int + ParentID int + Title string + Name string + FlatName string + Level int +} + +func (Category) TableName() string { + return "categories" +} diff --git a/plati/categories.go b/plati/categories.go index fc3e96c..bc624e1 100644 --- a/plati/categories.go +++ b/plati/categories.go @@ -48,6 +48,20 @@ type Category struct { CanAdd bool `json:"can_add"` } +func (c *Category) GetName() string { + if len(c.Name) == 0 { + return "" + } + + for _, n := range c.Name { + if n.Locale == "ru-RU" { + return n.Value + } + } + + return c.Name[0].Value +} + type CategoriesResponse struct { Retval int `json:"retval"` Retdesc any `json:"retdesc"`