init parser
This commit is contained in:
72
cmd/cli/digi/digi.go
Normal file
72
cmd/cli/digi/digi.go
Normal file
@ -0,0 +1,72 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
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