-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (63 loc) · 1.51 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
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/bshafiee/bonga/craiglist"
"github.com/bshafiee/bonga/notification"
"github.com/bshafiee/bonga/scraping"
)
const sendmail = "/usr/sbin/sendmail"
func main() {
key := os.Getenv("SENDGRID_API_KEY")
if len(key) == 0 {
log.Fatal("could not load the SENDGRID key")
}
port := os.Getenv("PORT")
if port == "" {
log.Println("$PORT must be set")
port = "8080"
}
// implement /services/ping for service health
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello Beroo")
})
go http.ListenAndServe(":"+port, nil)
notifEngine := notification.NewNotificationEngine("list.txt", []notification.Channel{notification.NewEmailNotification()})
if err := notifEngine.Initialize(); err != nil {
log.Fatal(err)
}
c := craiglist.NewCraiglistScraper()
params := []scraping.Parameter{
craiglist.HasPictureParam{true},
craiglist.MinBathParam{1},
craiglist.MinBedsParam{2},
craiglist.MinPriceParam{2500},
craiglist.MaxPriceParam{3300},
craiglist.MaxBedsParam{2},
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
ticker := time.NewTicker(60 * 15 * time.Second)
for {
select {
case <-ticker.C:
log.Println("here tick")
res, err := c.Query(params)
if err != nil {
log.Println("craiglist query error:", err)
continue
}
if len(res) > 0 {
if err := notifEngine.Notify(res); err != nil {
log.Fatal(err)
}
}
case <-sigCh:
return
}
}
}