categories to db

This commit is contained in:
2023-12-21 20:52:47 +03:00
parent 96b7e49fa2
commit ed1716232a
4 changed files with 146 additions and 72 deletions

118
cmd/cli/digi.go Normal file
View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

14
gorm/category.go Normal file
View File

@ -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"
}

View File

@ -48,6 +48,20 @@ type Category struct {
CanAdd bool `json:"can_add"` 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 { type CategoriesResponse struct {
Retval int `json:"retval"` Retval int `json:"retval"`
Retdesc any `json:"retdesc"` Retdesc any `json:"retdesc"`