From 6de455b1bdc18e6f38a091e990aca8c5361f380e Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Mon, 20 Jan 2025 13:00:59 -0500 Subject: [PATCH] Add price extraction and validate URL structure in parsers Added price field to Item struct in AisleGopher and implemented logic to extract price data. Updated Wegmans parser to validate URL structure by ensuring the second segment is "product". These changes improve data accuracy and error handling. --- sites/aislegopher/aislegopher.go | 14 ++++++++++++-- sites/wegmans/wegmans.go | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sites/aislegopher/aislegopher.go b/sites/aislegopher/aislegopher.go index 35d3e69..babb76c 100644 --- a/sites/aislegopher/aislegopher.go +++ b/sites/aislegopher/aislegopher.go @@ -22,8 +22,9 @@ var ( ) type Item struct { - ID int - Name string + ID int + Name string + Price float64 } func deferClose(cl io.Closer) { @@ -67,5 +68,14 @@ func (c Config) GetItemFromURL(ctx context.Context, b extractor.Browser, u *url. res.Name, _ = names[0].Text() } + prices := doc.Select("h4.h2") + + if len(prices) > 0 { + priceStr, _ := prices[0].Text() + priceStr = strings.ReplaceAll(priceStr, "$", "") + priceStr = strings.TrimSpace(priceStr) + res.Price, _ = strconv.ParseFloat(priceStr, 64) + } + return res, nil } diff --git a/sites/wegmans/wegmans.go b/sites/wegmans/wegmans.go index 12a48ce..57ef620 100644 --- a/sites/wegmans/wegmans.go +++ b/sites/wegmans/wegmans.go @@ -53,6 +53,10 @@ func (c Config) GetItemPrice(ctx context.Context, b extractor.Browser, u *url.UR return Item{}, ErrInvalidURL } + if a[1] != "product" { + return Item{}, ErrInvalidURL + } + id, _ := strconv.Atoi(a[2]) if id == 0 {