-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
369 lines (342 loc) · 10.1 KB
/
fetch.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
database string
collection string
)
var client http.Client = http.Client{
Timeout: time.Second * 15,
}
var wg sync.WaitGroup
const (
connectionStringEnv = "SRC_WEBHOOK_MONGODB_CONNECTION_STRING"
databaseNameEnv = "SRC_WEBHOOK_DATABASE"
collectionNameEnv = "SRC_WEBHOOK_COLLECTION"
postRunUri = "SRC_WEBHOOK_RUN_URI"
)
type Data struct {
ID string `json:"id,omitempty"`
Order int `json:"order,omitempty"`
New bool `json:"new,omitempty"`
Weblink string `json:"weblink,omitempty"`
Game struct {
Data struct {
ID string `json:"id,omitempty"`
Names struct {
International string `json:"international,omitempty"`
Japanese string `json:"japanese,omitempty"`
Twitch string `json:"twitch,omitempty"`
} `json:"names,omitempty"`
Name string `json:"name,omitempty"`
Abbreviation string `json:"abbreviation,omitempty"`
Platforms []string `json:"platforms,omitempty"`
Regions []string `json:"regions,omitempty"`
// im not sure this works
Moderators []string `json:"moderators,omitempty"`
Assets struct {
Trophy1st struct {
URI string `json:"uri,omitempty"`
} `json:"trophy-1st,omitempty"`
Trophy2nd struct {
URI string `json:"uri,omitempty"`
} `json:"trophy-2nd,omitempty"`
Trophy3rd struct {
URI string `json:"uri,omitempty"`
} `json:"trophy-3rd,omitempty"`
Trophy4th struct {
URI string `json:"uri,omitempty"`
} `json:"trophy-4th,omitempty"`
} `json:"assets,omitempty"`
} `json:"data,omitempty"`
} `json:"game,omitempty"`
Level struct {
Data struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"data,omitempty"`
} `json:"level,omitempty"`
Category struct {
Data struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Miscellaneous bool `json:"miscellaneous,omitempty"`
Variables struct {
Data []struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Category interface{} `json:"category,omitempty"`
Scope struct {
Type string `json:"type,omitempty"`
} `json:"scope,omitempty"`
Mandatory bool `json:"mandatory,omitempty"`
UserDefined bool `json:"user-defined,omitempty"`
Obsoletes bool `json:"obsoletes,omitempty"`
Values map[string]interface{} `json:"values,omitempty"`
IsSubcategory bool `json:"is-subcategory,omitempty"`
} `json:"data,omitempty"`
} `json:"variables,omitempty"`
} `json:"data,omitempty"`
} `json:"category,omitempty"`
Times struct {
Primary float64 `json:"primary_t,omitempty"`
RealTime float64 `json:"realtime_t,omitempty"`
RealTimeLoadless float64 `json:"realtime_noloads_t,omitempty"`
InGameTime float64 `json:"ingame_t,omitempty"`
}
Videos struct {
Links []struct {
URI string `json:"uri,omitempty"`
} `json:"links,omitempty"`
} `json:"videos,omitempty"`
Comment string `json:"comment,omitempty"`
Status struct {
Status string `json:"status,omitempty"`
Examiner string `json:"examiner,omitempty"`
Reason string `json:"reason,omitempty"`
VerifyDate time.Time `json:"verify-date,omitempty"`
} `json:"status,omitempty"`
Players struct {
Data []struct {
ID string `json:"id,omitempty"`
Weblink string `json:"weblink,omitempty"`
Names struct {
International string `json:"international,omitempty"`
Japanese string `json:"japanese,omitempty"`
} `json:"names,omitempty"`
Assets struct {
Icon struct {
URI string `json:"uri,omitempty"`
} `json:"icon,omitempty"`
Image struct {
URI string `json:"uri,omitempty"`
} `json:"image,omitempty"`
} `json:"assets,omitempty"`
} `json:"data,omitempty"`
} `json:"players,omitempty"`
Date string `json:"date,omitempty"`
Submitted time.Time `json:"submitted,omitempty"`
System struct {
Platform string `json:"platform,omitempty"`
Emulated bool `json:"emulated,omitempty"`
Region string `json:"region,omitempty"`
} `json:"system,omitempty"`
Values map[string]string `json:"values,omitempty"`
Region struct {
Data struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"data,omitempty"`
} `json:"region,omitempty"`
Platform struct {
Data struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"data,omitempty"`
} `json:"platform,omitempty"`
}
type Response struct {
Data []Data `json:"data,omitempty"`
Pagination struct {
Offset int `json:"offset,omitempty"`
Max int `json:"max,omitempty"`
Size int `json:"size,omitempty"`
} `json:"pagination,omitempty"`
Scope string `json:"scope,omitempty"`
}
func MakeRuns(res Response, scope string) Response {
for i := range res.Data {
res.Data[i].Order = i
}
res.Scope = scope
return res
}
func main() {
chNew, chVerified, chRejected := make(chan Response), make(chan Response), make(chan Response)
go New(chNew)
go Verified(chVerified)
go Rejected(chRejected)
for {
if chNew == nil && chVerified == nil && chRejected == nil {
break
}
select {
case res := <-chNew:
go handleRuns(&res, "new")
chNew = nil
case res := <-chVerified:
go handleRuns(&res, "verified")
chVerified = nil
case res := <-chRejected:
go handleRuns(&res, "rejected")
chRejected = nil
}
}
wg.Wait()
}
func handleRuns(r *Response, scope string) {
runs := MakeRuns(*r, scope)
runs = *update(&runs, scope)
if len(runs.Data) != 0 {
Delete(scope)
Create(runs)
}
wg.Done()
}
func update(runs *Response, scope string) *Response {
exists := false
var newRuns []Data
existingRuns := List(scope).Data
for _, run := range runs.Data {
for _, xRun := range existingRuns {
if run.ID == xRun.ID {
exists = true
break
}
}
if exists {
break
}
run.New = true
newRuns = append(newRuns, run)
}
fmt.Printf("New runs found for scope %v: %v\n", scope, len(newRuns))
i := len(newRuns)
if i == 0 {
return &Response{Data: newRuns, Scope: scope}
}
body, _ := json.Marshal(newRuns)
http.Post(os.Getenv(postRunUri), "application/json", bytes.NewBuffer(body))
for i < 20 {
(existingRuns)[i].Order = i
(existingRuns)[i].New = false
newRuns = append(newRuns, (existingRuns)[i])
i++
}
return &Response{Data: newRuns, Scope: scope}
}
func Connect() *mongo.Client {
connectionString := os.Getenv(connectionStringEnv)
if connectionString == "" {
log.Fatal("The database connection string variable is missing!")
}
database = os.Getenv(databaseNameEnv)
if database == "" {
log.Fatal("The database name variable is missing!")
}
collection = os.Getenv(collectionNameEnv)
if collection == "" {
log.Fatal("The collection name variable is missing!")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
options := options.Client().ApplyURI(connectionString).SetDirect(true)
c, _ := mongo.NewClient(options)
err := c.Connect(ctx)
if err != nil {
log.Fatalf("Connection couldn't be initialized!\n%v", err)
}
err = c.Ping(ctx, nil)
if err != nil {
log.Fatalf("Connection didn't respond back!\n%v", err)
}
return c
}
func Create(runs Response) {
c := Connect()
ctx := context.Background()
defer c.Disconnect(ctx)
runCollection := c.Database(database).Collection(collection)
r, err := runCollection.InsertOne(ctx, runs)
if err != nil {
log.Fatalf("The runs couldn't be added!\n%v", err)
}
fmt.Println("Added runs", r.InsertedID)
}
func List(scope string) *Response {
c := Connect()
ctx := context.Background()
defer c.Disconnect(ctx)
var runs Response
runCollection := c.Database(database).Collection(collection)
err := runCollection.FindOne(ctx, bson.D{{Key: "scope", Value: scope}}).Decode(&runs)
if err != nil {
log.Printf("The runs couldn't be listed!\n%v", err)
}
return &runs
}
func Delete(scope string) {
c := Connect()
ctx := context.Background()
defer c.Disconnect(ctx)
runCollection := c.Database(database).Collection(collection)
res, err := runCollection.DeleteMany(ctx, bson.D{{Key: "scope", Value: scope}})
if err != nil {
log.Fatalf("The runs couldn't be deleted!\n%v", err)
}
fmt.Printf("Deleted %v entry for scope %v\n", res.DeletedCount, scope)
}
// todo: implement pagination to all of these
func New(c chan Response) {
wg.Add(1)
r, err := client.Do(createReq("?status=new&orderby=submitted&direction=desc&embed=category.variables,game,level,region,platform,players"))
if err != nil {
log.Panic(err)
}
c <- parseRes(r)
}
func Verified(c chan Response) {
wg.Add(1)
r, err := client.Do(createReq("?status=verified&orderby=verify-date&direction=desc&embed=category.variables,game,level,region,platform,players"))
if err != nil {
log.Panic(err)
}
c <- parseRes(r)
}
func Rejected(c chan Response) {
wg.Add(1)
r, err := client.Do(createReq("?status=rejected&orderby=verify-date&direction=desc&embed=category.variables,game,level,region,platform,players"))
if err != nil {
log.Panic(err)
}
c <- parseRes(r)
}
func createReq(queries string) *http.Request {
url := "https://speedrun.com/api/v1/runs"
// this nanosecond formatting has to be done because of src's aggressive caching behavior
req, err := http.NewRequest("GET", url+queries+"&vary="+strconv.FormatInt(int64(time.Now().Nanosecond()), 10), nil)
if err != nil {
log.Panic(err)
}
req.Header.Add("User-Agent", "SRCStats Webhook")
return req
}
func parseRes(r *http.Response) Response {
if r.StatusCode == 400 || r.StatusCode == 404 {
// this doesnt *have* to be a panic, could just return an empty Response
log.Panic("Server returned a failure!")
}
result, err := io.ReadAll(r.Body)
if err != nil {
log.Panic(err)
}
r.Body.Close()
var res Response
json.Unmarshal(result, &res)
return res
}