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:
parent
f37e60dddc
commit
6de455b1bd
@ -24,6 +24,7 @@ var (
|
||||
type Item struct {
|
||||
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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user