init parser
This commit is contained in:
74
cmd/cli/plati/plati.go
Normal file
74
cmd/cli/plati/plati.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.home.4it.me/dilap54/platiparser/plati"
|
||||
)
|
||||
|
||||
func main() {
|
||||
categories := openCategories("./categories.json").Content
|
||||
categories = filterBySubstring("Gift", categories)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
platiCli := plati.New()
|
||||
|
||||
// printNames(categories)
|
||||
|
||||
for i, c := range categories {
|
||||
log.Printf("fetching goods [%d/%d] for %s\n", i, len(categories), c.FlatName)
|
||||
goods, err := platiCli.GetBlockGoodsCategory(ctx, c.ID, c.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(goods)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user