-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
75 lines (58 loc) · 1.45 KB
/
util.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"fmt"
"net/url"
"os"
scrape "github.com/muly/product-scrape"
)
const (
requestTypeAvailability = "AVAILABILITY"
requestTypePrice = "PRICE"
)
var websiteNotSupported error = fmt.Errorf("unsupported website")
// function for conditions to satisfy for sending email //notify condition
func shouldNotify(i trackInput, p scrape.Product) bool {
if (p == scrape.Product{}) {
return false
}
if i.TypeOfRequest == requestTypePrice && p.Price < i.MinThreshold {
return true
}
if i.TypeOfRequest == requestTypeAvailability && p.Availability {
return true
}
return false
}
func readSupportedWebsites() (map[string]bool, error) {
f, err := os.Open("supported_websites")
if err != nil {
return nil, err
}
s := bufio.NewScanner(f)
output := make(map[string]bool)
for s.Scan() {
output[s.Text()] = true
}
return output, nil
}
func validateAndCleanup(t *trackInput) error {
if t.URL == "" {
return fmt.Errorf("product url is mandatory field")
}
if t.TypeOfRequest == requestTypeAvailability ||
t.TypeOfRequest == requestTypePrice {
if t.EmailID == "" {
return fmt.Errorf("email id is mandatory field")
}
}
u, err := url.Parse(t.URL)
if err != nil {
return fmt.Errorf("error parsing url %s: %v", t.URL, err)
}
if _, ok := supportedWebsites[u.Hostname()]; !ok {
return fmt.Errorf("url %s not supported: %w", t.URL, websiteNotSupported)
}
t.URL = scrape.CleanupURL(u).String()
return nil
}