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.
This commit is contained in:
Steve Dudenhoeffer 2025-01-20 13:00:59 -05:00
parent f37e60dddc
commit 6de455b1bd
2 changed files with 16 additions and 2 deletions

View File

@ -22,8 +22,9 @@ var (
) )
type Item struct { type Item struct {
ID int ID int
Name string Name string
Price float64
} }
func deferClose(cl io.Closer) { 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() 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 return res, nil
} }

View File

@ -53,6 +53,10 @@ func (c Config) GetItemPrice(ctx context.Context, b extractor.Browser, u *url.UR
return Item{}, ErrInvalidURL return Item{}, ErrInvalidURL
} }
if a[1] != "product" {
return Item{}, ErrInvalidURL
}
id, _ := strconv.Atoi(a[2]) id, _ := strconv.Atoi(a[2])
if id == 0 { if id == 0 {