-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (116 loc) · 2.96 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
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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/antchfx/htmlquery"
"github.com/spf13/viper"
)
var (
store Storer
webhookCall func(message, url string) error
)
type Storer interface {
GetValue(key string) (result string, err error)
PutValue(key, value string) (err error)
}
func main() {
url := viper.GetString(urlFlagName)
xpath := viper.GetString(xpathFlagName)
store = NewFileStore(viper.GetString(storeDirFlagName))
slackWebhookUrl := viper.GetString(slackWebhookFlagName)
webhookCall = postToSlack
hash := md5.Sum([]byte(url))
key := hex.EncodeToString(hash[:])
currentValue, err := queryPath(url, xpath)
if err != nil {
log.Printf("Couldn't query url=%s: %s", url, err)
err = webhookCall(fmt.Sprintf("couldn't get current value for url=%s", url), slackWebhookUrl)
if err != nil {
log.Fatalf("couldn't post message to slack. exit: %s", err.Error())
}
os.Exit(1)
}
log.Printf("queried value=%s", currentValue)
storedValue, err := store.GetValue(key)
if err != nil {
log.Printf("there is no a stored value: %s", err)
err = webhookCall(fmt.Sprintf("there is no a stored value for url=%s\n%s",
url,
viper.GetString(extramessageFlagName),
), slackWebhookUrl)
if err != nil {
log.Fatalf("couldn't post message to slack. exit: %s", err.Error())
}
err = store.PutValue(key, currentValue)
if err != nil {
log.Fatalf("couldn't put a new value: %s", err.Error())
}
os.Exit(0)
}
if storedValue != currentValue {
err = webhookCall(fmt.Sprintf("url=%s is updated:\n stored= %s\n current= %s\n%s",
viper.GetString(urlFlagName),
storedValue,
currentValue,
viper.GetString(extramessageFlagName),
), slackWebhookUrl)
if err != nil {
log.Fatalf("couldn't post message to slack. exit: %s", err.Error())
}
err = store.PutValue(key, currentValue)
if err != nil {
log.Fatalf("couldn't put a new value: %s", err.Error())
}
}
}
func queryPath(url string, xpath string) (result string, err error) {
doc, err := htmlquery.LoadURL(url)
if err != nil {
return
}
node, err := htmlquery.Query(doc, xpath)
if err != nil {
return
}
if node == nil {
err = fmt.Errorf("element wasn't found")
return
}
result = htmlquery.InnerText(node)
return
}
func postToSlack(message string, url string) (err error) {
log.Printf("POSTING TO SLACK: %s", message)
payload := struct {
Text string `json:"text"`
}{
Text: message,
}
data, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
err = fmt.Errorf("error: response status = %s", resp.Status)
return
}
log.Println("posted to slack successfully with a response status:", resp.Status)
return
}