support parsing item page info

This commit is contained in:
2024-02-03 20:28:33 +03:00
parent 6044e116f8
commit be6aec73df
10 changed files with 262 additions and 61 deletions

View File

@ -197,6 +197,7 @@ func newParseEwayCmd() *cli.Command {
Usage: "parse all available eway goods",
Commands: []*cli.Command{
newParseEwayGetCmd(),
newParseEwayListCmd(),
newParseEwayDumpCmd(),
},
}
@ -205,6 +206,21 @@ func newParseEwayCmd() *cli.Command {
func newParseEwayGetCmd() *cli.Command {
return &cli.Command{
Name: "get",
Usage: "loads information about the product by parsing product's html page",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "cart",
Aliases: []string{"c"},
Usage: "cart of the product",
},
},
Action: decorateAction(parseEwayGetAction),
}
}
func newParseEwayListCmd() *cli.Command {
return &cli.Command{
Name: "list",
Usage: "parse all available eway goods",
Flags: []cli.Flag{
&cli.IntFlag{
@ -222,7 +238,7 @@ func newParseEwayGetCmd() *cli.Command {
Usage: "filters by minimum available items in stock",
},
},
Action: decorateAction(parseEwayGetAction),
Action: decorateAction(parseEwayListAction),
}
}
@ -653,7 +669,7 @@ func decorateAction(a action) cli.ActionFunc {
start := time.Now()
defer func() {
log.Info().Dur("elapsed", time.Since(start)).Msg("command completed")
log.Info().Float64("elapsed", time.Since(start).Seconds()).Msg("command completed")
}()
log.Info().Msg("command execution started")
@ -749,6 +765,29 @@ func exportYMLCatalogAction(ctx context.Context, c *cli.Command) error {
}
func parseEwayGetAction(ctx context.Context, cmd *cli.Command) error {
cartID := cmd.Int("cart")
client, err := components.GetEwayClient()
if err != nil {
return fmt.Errorf("getting eway client: %w", err)
}
pi, err := client.GetProductInfo(ctx, cartID)
if err != nil {
return fmt.Errorf("getting product info: %w", err)
}
enc := json.NewEncoder(cmd.Writer)
enc.SetIndent("", " ")
err = enc.Encode(pi)
if err != nil {
return fmt.Errorf("encoding data: %w", err)
}
return nil
}
func parseEwayListAction(ctx context.Context, cmd *cli.Command) error {
page := cmd.Int("page")
limit := cmd.Int("limit")
atLeast := cmd.Int("min-stock")
@ -759,6 +798,11 @@ func parseEwayGetAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("getting eway client: %w", err)
}
log, err := components.GetLogger()
if err != nil {
return fmt.Errorf("getting logger: %w", err)
}
start := page * limit
items, total, err := client.GetGoodsNew(ctx, eway.GetGoodsNewParams{
@ -782,19 +826,26 @@ func parseEwayGetAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("getting remnants: %w", err)
}
tbl := table.New("sku", "category", "cart", "stock", "price")
tbl := table.New("sku", "category", "cart", "stock", "price", "parameters")
for _, item := range items {
outGood, err := entity.MakeGoodsItem(item, remnants)
pi, err := client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
log.Warn().Err(err).Msg("unable to get product info")
}
outGood, err := entity.MakeGoodsItem(item, remnants, pi)
if err != nil {
return fmt.Errorf("making goods item: %w", err)
}
parameters, _ := json.MarshalIndent(outGood.Parameters, "", " ")
tbl.AddRow(
outGood.Articul,
outGood.Type,
outGood.Cart,
outGood.Stock,
outGood.Price,
string(parameters),
)
}
@ -867,7 +918,12 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
goodsItems = goodsItems[:0]
for _, item := range items {
goodsItem, err := entity.MakeGoodsItem(item, remnants)
pi, err := client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
return fmt.Errorf("getting product info: %w", err)
}
goodsItem, err := entity.MakeGoodsItem(item, remnants, pi)
if err != nil {
logger.Warn().Err(err).Any("item", item).Msg("unable to make goods item")
continue
@ -942,26 +998,34 @@ func goodsItemAsOffer(in entity.GoodsItem, categoryIDByName map[string]int64) (o
categoryID := categoryIDByName[in.Type]
pictureURLs := make([]string, 0, len(in.PhotoURLs))
for _, url := range in.PhotoURLs {
pictureURLs = append(pictureURLs, imgurl(url))
}
params := make([]export.Param, len(in.Parameters))
for k, v := range in.Parameters {
params = append(params, export.Param{
Name: k,
Value: v,
})
}
params = append(params, export.Param{
Name: quantityParamName,
Value: strconv.Itoa(in.Stock),
})
out = export.Offer{
ID: in.Cart,
VendorCode: in.Articul,
Price: int(in.TariffPrice),
CategoryID: categoryID,
PictureURLs: []string{
imgurl(in.Photo),
},
ID: in.Cart,
VendorCode: in.Articul,
Price: int(in.TariffPrice),
CategoryID: categoryID,
PictureURLs: pictureURLs,
Model: in.Name,
Vendor: in.Producer,
TypePrefix: in.Name,
Description: in.Description,
ManufacturerWarrany: true,
Params: []export.Param{
{
Name: quantityParamName,
Value: strconv.Itoa(in.Stock),
},
},
Params: params,
Type: defaultType,
CurrencyID: defaultCurrency,
@ -987,7 +1051,7 @@ func testFBSAction(ctx context.Context, c *cli.Command) error {
return fmt.Errorf("parsing: %w", err)
}
if got != gooditem {
if got.Articul != gooditem.Articul {
gotStr := fmt.Sprintf("%v", got)
hasStr := fmt.Sprintf("%v", gooditem)
println(gotStr, "\n", hasStr)
@ -1093,6 +1157,9 @@ func cryptoDeEncryptAction(encrypt bool) cli.ActionFunc {
}
_, err = c.Writer.Write([]byte(out))
if err != nil {
return err
}
_, err = c.Writer.Write([]byte{'\n'})
return err
}