-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (52 loc) · 1.4 KB
/
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
60
61
62
63
64
65
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/ianchildress/platebot/alert"
"github.com/ianchildress/platebot/throttle"
)
const (
sleepDuration = time.Millisecond * 500
)
func main() {
rand.Seed(time.Now().Unix())
var sid, token, sendTo, sendFrom string
flag.StringVar(&sid, "sid", "", "twilio sid")
flag.StringVar(&token, "token", "", "twilio token")
flag.StringVar(&sendTo, "to", "", "phone number to send alert message to")
flag.StringVar(&sendFrom, "from", "", "phone number to send alert message from")
flag.Parse()
alerter, err := alert.NewTwilioAlerter(sid, token, sendTo, sendFrom)
if err != nil {
exit(err)
}
throttler := throttle.NewSimpleThrottler(throttle.DefaultThrottleDuration)
for {
start := time.Now()
var products []Product
products = append(products, checkPlates(plateProducts, throttler)...)
products = append(products, checkBars(barProducts, throttler)...)
if len(products) > 0 {
if err := sendMsg(products, alerter); err != nil {
fmt.Println(err)
} else {
fmt.Println("alert successfully sent")
}
}
fmt.Println("completed search in", time.Since(start))
}
}
func sendMsg(products []Product, alerter alert.Alerter) error {
var msg string
for i := range products {
msg += fmt.Sprintf("%s %s\n", products[i].Name, products[i].URL)
}
return alerter.Alert(msg)
}
func exit(err error) {
fmt.Println(err)
os.Exit(1)
}