73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
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)
|
||
}
|
||
}
|
||
}
|