1
1
package scraper
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"strconv"
6
7
"strings"
@@ -14,14 +15,37 @@ type ScraperImpl struct {
14
15
Collector * colly.Collector
15
16
}
16
17
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 , ". " , "" )
20
21
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
22
46
}
23
47
24
- // scrapeData implements Scraper.
48
+ // ScrapeData implements Scraper.
25
49
func (s * ScraperImpl ) ScrapeData (baseURL string , maxPage int , category string ) ([]models.Product , error ) {
26
50
var products []models.Product
27
51
@@ -35,22 +59,32 @@ func (s *ScraperImpl) ScrapeData(baseURL string, maxPage int, category string) (
35
59
originalPriceStr = e .ChildText (".price .woocommerce-Price-amount.amount" )
36
60
}
37
61
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 }
41
67
}
42
68
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 }
46
74
}
47
75
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
+ }
54
88
})
55
89
56
90
for i := 1 ; i <= maxPage ; i ++ {
@@ -69,7 +103,7 @@ func (s *ScraperImpl) ScrapeData(baseURL string, maxPage int, category string) (
69
103
return products , nil
70
104
}
71
105
72
- func NewScraperImpl (collector * colly.Collector ) * ScraperImpl {
106
+ func NewScraperImpl (collector * colly.Collector ) Scraper {
73
107
return & ScraperImpl {
74
108
Collector : collector ,
75
109
}
0 commit comments