-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
148 lines (130 loc) · 2.99 KB
/
checker.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/andreyvit/diff"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
yaml "gopkg.in/yaml.v2"
)
type Checker struct {
Interval int64 `json:"interval" yaml:"interval"`
StoreHash bool `json:"store_hash" yaml:"store_hash"`
Debug bool `json:"debug" yaml:"debug"`
Sites map[string]Site `json:"sites" yaml:"sites"`
ConfigPath string `json:"-" yaml:"-"`
Notifiers []Notifier `json:"-" yaml:"-"`
}
func NewChecker(config string, notifiers []Notifier) (Checker, error) {
c := Checker{ConfigPath: config}
data, err := ioutil.ReadFile(config)
if err != nil {
return c, err
}
err = yaml.Unmarshal([]byte(data), &c)
c.Notifiers = notifiers
return c, err
}
func (c Checker) Run(singleRun bool) {
log.Println("Started...")
for {
for i, s := range c.Sites {
if c.Debug {
log.Printf("Scanning '%s'\n", i)
}
err := s.Check(c.Notifiers)
if err != nil {
log.Println(err)
}
if c.Debug {
log.Printf("%s => %s\n", c.Sites[i].Hash, s.Hash)
}
c.Sites[i] = s
}
if c.StoreHash {
c.UpdateConfig()
}
if singleRun {
break
}
time.Sleep(time.Duration(c.Interval) * time.Second)
}
}
func (c Checker) UpdateConfig() error {
out, err := yaml.Marshal(&c)
if err != nil {
return err
}
err = ioutil.WriteFile(c.ConfigPath, out, 0644)
return err
}
type Site struct {
URL string `json:"url" yaml:"url"`
Template string `json:"template" yaml:"template"`
Recipient string `json:"recipient" yaml:"recipient"`
Selector string `json:"selector" yaml:"selector"`
Diff bool `json:"diff" yaml:"diff"`
Hash string `json:"hash" yaml:"hash"`
Data string `json:"data" yaml:"data"`
}
func (s *Site) Check(n []Notifier) error {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from error '%s' while checking %s\n", r, s.URL)
}
}()
oldHash := s.Hash
oldData := s.Data
response, err := http.Get(s.URL)
if err != nil {
return err
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if s.Selector == "" {
data := string(content)
if s.Diff {
s.Data = data
}
s.Hash = GetMD5Hash(data)
} else {
sel, err := cascadia.Compile(s.Selector)
if err != nil {
return err
}
r := bytes.NewReader(content)
node, err := html.Parse(r)
if err != nil {
return err
}
firstMatch := sel.MatchFirst(node)
data := firstMatch.Data
if s.Diff {
s.Data = data
}
s.Hash = GetMD5Hash(data)
}
if oldHash != "" && oldHash != s.Hash {
diff := diff.LineDiff(oldData, s.Data)
for _, notifier := range n {
err = notifier.Notify(s.Recipient, s.Template, diff)
if err != nil {
log.Printf("Error while notifing: %s\n", err.Error())
}
}
}
return nil
}
func GetMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}