-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplates.go
63 lines (53 loc) · 1.13 KB
/
plates.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"github.com/fatih/color"
"github.com/ianchildress/platebot/throttle"
)
func checkPlates(plates []Product, throttler throttle.Throttler) []Product {
var products []Product
exists := func(s string, l []string) bool {
for i := range l {
if l[i] == s {
return true
}
}
return false
}
// get a list of all product urls so we only need to request a page once
var urls []string
for i := range plates {
if !exists(plates[i].URL, urls) {
urls = append(urls, plates[i].URL)
}
}
for i := range urls {
//fmt.Println("checking", urls[i])
r, err := throttler.Get(urls[i])
if err != nil {
fmt.Println(err)
continue
}
b, err := ioutil.ReadAll(r)
if err != nil {
fmt.Println(err)
continue
}
r.Close()
for _, p := range plates {
// skip untracked products
if !p.Track {
//fmt.Println("skipping", p.Name)
continue
}
tag := fmt.Sprintf("grouped-product-item-%s", p.Number)
if bytes.Contains(b, []byte(tag)) {
color.Green("%v %s?=%s", p.Name, p.URL, random())
products = append(products, p)
}
}
}
return products
}