-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (46 loc) · 1011 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"os"
"sync"
"product-watcher/models"
"product-watcher/scraper"
log "github.com/sirupsen/logrus"
)
type Config struct {
Products []models.Product `json:"products"`
}
func main() {
if os.Getenv("ENV") == "production" {
log.SetFormatter(&log.JSONFormatter{})
}
productsFile, err := os.Open("./products.json")
if err != nil {
panic(err)
}
config := &Config{}
err = json.NewDecoder(productsFile).Decode(config)
if err != nil {
panic(err)
}
s := scraper.NewScraper()
wg := sync.WaitGroup{}
for _, p := range config.Products {
wg.Add(1)
go func(product models.Product) {
defer wg.Done()
if product.Active {
for _, URL := range product.URLs {
newPrice, err := s.ScrapePrice(URL)
if err != nil {
log.Errorln(URL.Company.Name, product.Name, err)
} else {
log.Infoln(URL.Company.Name, product.Name, "new price:", newPrice)
}
}
}
}(p)
}
wg.Wait()
log.Infoln("Done getting all products")
}