-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.go
More file actions
80 lines (67 loc) · 1.77 KB
/
Copy pathchecker.go
File metadata and controls
80 lines (67 loc) · 1.77 KB
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
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
func checkReportRemote(info *configRemoteInfo) {
logrus.WithFields(logrus.Fields{
"name": info.Name,
"last-modified": info.LastModified,
}).Info("⚠ New update!")
for _, channelID := range info.Channels {
line := fmt.Sprintf(
":warning: **New update!** %s is now at *%s*: <%s>",
info.Name,
info.LastModified,
info.URL,
)
_, err := gDiscord.ChannelMessageSend(channelID, line)
if err != nil {
logrus.Warn("Unable to send message to channel with ID ", channelID)
}
}
}
func checkRemote(info *configRemoteInfo) bool {
lastKnownModified := time.Time{}
if info.LastModified != "" {
var err error
lastKnownModified, err = time.Parse(time.RFC3339, info.LastModified)
if err != nil {
logrus.WithError(err).Warn("Invalid date format")
}
}
req, err := http.NewRequest("HEAD", info.URL, nil)
if err != nil {
logrus.WithError(err).Error("Error creating http request to ", info.Name)
return false
}
req.Header.Set("User-Agent", "Openplanet Bot / @miss / miss@openplanet.dev")
client := http.Client{}
res, err := client.Do(req)
if err != nil {
logrus.WithError(err).Warn("Error sending http request to ", info.Name)
return false
}
lastModified, err := time.Parse(time.RFC1123, res.Header.Get("Last-Modified"))
if err != nil {
logrus.WithError(err).Error("Invalid date format from server")
return false
}
if lastModified.After(lastKnownModified) {
info.LastModified = lastModified.Format(time.RFC3339)
checkReportRemote(info)
return true
}
return false
}
func checkerRemote(info *configRemoteInfo) {
for gKeepAlive {
if checkRemote(info) {
saveConfig()
}
time.Sleep(time.Duration(59+rand.Intn(2)) * time.Second)
}
}