Skip to content

Commit be12aab

Browse files
committed
Update scraper CleanPrice function to return multiple prices
1 parent 406c9b5 commit be12aab

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

scraper/src/repository/scraper_repository_impl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (s *ScraperRepositoryImpl) Create(product models.Product) (models.Product,
3636
"DiscountedPrice": {
3737
N: aws.String(fmt.Sprintf("%d", product.DiscountedPrice)),
3838
},
39+
40+
"LastUpdated": {
41+
S: aws.String(product.LastUpdated),
42+
},
3943
},
4044
}
4145

scraper/src/scraper/scraper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import "github.com/dieg0code/shared/models"
44

55
type Scraper interface {
66
ScrapeData(baseURL string, maxPage int, category string) ([]models.Product, error)
7-
CleanPrice(price string) (int, error)
7+
CleanPrice(price string) ([]int, error)
88
}

scraper/src/scraper/scraper_impl.go

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scraper
22

33
import (
4+
"errors"
45
"fmt"
56
"strconv"
67
"strings"
@@ -14,14 +15,37 @@ type ScraperImpl struct {
1415
Collector *colly.Collector
1516
}
1617

17-
// cleanPrice implements Scraper.
18-
func (s *ScraperImpl) CleanPrice(price string) (int, error) {
19-
cleaned := strings.ReplaceAll(price, "$", "")
18+
// CleanPrice implements Scraper.
19+
func (s *ScraperImpl) CleanPrice(price string) ([]int, error) {
20+
cleaned := strings.ReplaceAll(price, ".", "")
2021
cleaned = strings.ReplaceAll(cleaned, ".", "")
21-
return strconv.Atoi(cleaned)
22+
23+
if strings.Contains(cleaned, "-") {
24+
priceParts := strings.Split(cleaned, "-")
25+
var prices []int
26+
for _, part := range priceParts {
27+
price, err := strconv.Atoi(strings.TrimSpace(part))
28+
if err != nil {
29+
logrus.WithError(err).Error("error converting price to int")
30+
return nil, errors.New("error converting price to int")
31+
}
32+
33+
prices = append(prices, price)
34+
}
35+
36+
return prices, nil
37+
}
38+
39+
priceInt, err := strconv.Atoi(cleaned)
40+
if err != nil {
41+
logrus.WithError(err).Error("error converting price to int")
42+
return nil, errors.New("error converting price to int")
43+
}
44+
45+
return []int{priceInt}, nil
2246
}
2347

24-
// scrapeData implements Scraper.
48+
// ScrapeData implements Scraper.
2549
func (s *ScraperImpl) ScrapeData(baseURL string, maxPage int, category string) ([]models.Product, error) {
2650
var products []models.Product
2751

@@ -35,22 +59,32 @@ func (s *ScraperImpl) ScrapeData(baseURL string, maxPage int, category string) (
3559
originalPriceStr = e.ChildText(".price .woocommerce-Price-amount.amount")
3660
}
3761

38-
originalPrice, err := s.CleanPrice(originalPriceStr)
39-
if err != nil {
40-
originalPrice = 0
62+
// Limpiar precios originales
63+
originalPrices, err := s.CleanPrice(originalPriceStr)
64+
if err != nil || len(originalPrices) == 0 {
65+
logrus.WithError(err).Error("error cleaning original price")
66+
originalPrices = []int{0}
4167
}
4268

43-
discountPrice, err := s.CleanPrice(discountPriceStr)
44-
if err != nil {
45-
discountPrice = 0
69+
// Limpiar precios con descuento
70+
discountPrices, err := s.CleanPrice(discountPriceStr)
71+
if err != nil || len(discountPrices) == 0 {
72+
logrus.WithError(err).Error("error cleaning discount price")
73+
discountPrices = []int{0}
4674
}
4775

48-
products = append(products, models.Product{
49-
Name: name,
50-
Category: category,
51-
OriginalPrice: originalPrice,
52-
DiscountedPrice: discountPrice,
53-
})
76+
// Crear una entrada por cada precio original
77+
for _, originalPrice := range originalPrices {
78+
for _, discountPrice := range discountPrices {
79+
product := models.Product{
80+
Name: name,
81+
Category: category,
82+
OriginalPrice: originalPrice,
83+
DiscountedPrice: discountPrice,
84+
}
85+
products = append(products, product)
86+
}
87+
}
5488
})
5589

5690
for i := 1; i <= maxPage; i++ {
@@ -69,7 +103,7 @@ func (s *ScraperImpl) ScrapeData(baseURL string, maxPage int, category string) (
69103
return products, nil
70104
}
71105

72-
func NewScraperImpl(collector *colly.Collector) *ScraperImpl {
106+
func NewScraperImpl(collector *colly.Collector) Scraper {
73107
return &ScraperImpl{
74108
Collector: collector,
75109
}

scraper/src/service/scraper_service_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package service
22

33
import (
4+
"time"
5+
46
"github.com/dieg0code/scraper/src/repository"
57
"github.com/dieg0code/scraper/src/scraper"
68
"github.com/dieg0code/shared/models"
@@ -38,6 +40,7 @@ func (s *ScraperServiceImpl) GetProducts() (bool, error) {
3840
Category: product.Category,
3941
OriginalPrice: product.OriginalPrice,
4042
DiscountedPrice: product.DiscountedPrice,
43+
LastUpdated: time.Now().Format("02-01-2006"),
4144
}
4245
_, err := s.ScraperRepository.Create(productModel)
4346
if err != nil {

shared/models/product.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ type Product struct {
66
Category string `json:"category" dynamodbav:"Category"`
77
OriginalPrice int `json:"original_price" dynamodbav:"OriginalPrice"`
88
DiscountedPrice int `json:"discounted_price" dynamodbav:"DiscountedPrice"`
9+
LastUpdated string `json:"last_updated" dynamodbav:"LastUpdated"`
910
}

0 commit comments

Comments
 (0)