-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.go
86 lines (71 loc) · 1.88 KB
/
title.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"time"
"github.com/codecat/go-libs/log"
)
type encryptedPackage struct {
EncryptedPackageID string `json:"encryptedPackageId"`
Name string
PublicEncryptedPackageVersionID string `json:"publicEncryptedPackageVersionId"`
Timestamp string
TitleID string `json:"titleId"`
}
func checkReportTitle(title *configNadeoTitle) {
log.Info("⚠ New update! %s = %s", title.Name, title.Timestamp)
for _, channelID := range title.Channels {
line := fmt.Sprintf(
":warning: **New update!** %s is now at *%s*",
title.Name,
title.Timestamp,
)
_, err := appDiscord.ChannelMessageSend(channelID, line)
if err != nil {
log.Warn("Unable to send message to channel with ID %s", channelID)
}
}
}
func checkTitle(title *configNadeoTitle) bool {
res, err := gServices.Get(
fmt.Sprintf(
"https://prod.trackmania.core.nadeo.online/encryptedPackages/?titleIdList=%s",
title.ID,
),
0,
)
if err != nil {
log.Error("Unable to check title updates: %s", err.Error())
return false
}
var packages []encryptedPackage
json.Unmarshal([]byte(res), &packages)
if len(packages) == 0 {
log.Error("Title does not exist: %s", title.ID)
return false
}
tm, err := time.Parse(time.RFC3339, packages[0].Timestamp)
if err != nil {
log.Error("Unable to parse timestamp: %s", err.Error())
return false
}
lastKnownTimestamp := time.Time{}
if title.Timestamp != "" {
lastKnownTimestamp, _ = time.Parse(time.RFC3339, title.Timestamp)
}
if tm.After(lastKnownTimestamp) {
title.Timestamp = tm.Format(time.RFC3339)
checkReportTitle(title)
return true
}
return false
}
func checkerTitle(title *configNadeoTitle) {
for gKeepAlive {
if checkTitle(title) {
saveConfig()
}
time.Sleep(time.Duration(59+rand.Intn(2)) * time.Second)
}
}