package main import ( "context" "encoding/json" "fmt" "log" "os" "strings" "time" "gitea.home.4it.me/dilap54/platiparser/gorm" "gitea.home.4it.me/dilap54/platiparser/plati" "github.com/joho/godotenv" uuid "github.com/satori/go.uuid" ) func init() { godotenv.Load(".env") } func main() { categories := openCategories("./categories.json").Content categories = filterBySubstring("Gift", categories) ctx := context.Background() db := gorm.GetDB() 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) } if len(goods) == 0 { continue } gormGoods := convertGoodsToGorm(c, goods) if err := db.Create(gormGoods).Error; err != nil { log.Fatal(err) } json.NewEncoder(os.Stdout).Encode(goods) } } func convertGoodsToGorm(cat *plati.Category, goods []*plati.Good) []*gorm.Good { out := make([]*gorm.Good, 0, len(goods)) for _, g := range goods { gormGood := gorm.Good{ ID: uuid.NewV4().String(), Name: g.Name, IDC: cat.ID, IDR: cat.ParentID, Goodlink: g.GoodLink, Seller: g.Seller, Sellerlink: g.SellerLink, Sellerrating: g.SellerRating, Sold: g.Sold, Price: g.Price, CreatedAt: time.Now(), } out = append(out, &gormGood) } return out } 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 }