-
Notifications
You must be signed in to change notification settings - Fork 1
/
TBA.js
62 lines (59 loc) · 1.51 KB
/
TBA.js
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
const https = require("https");
const config = require("./config/config");
const utils = require("./utils");
const keys = require("./keys");
module.exports.getEvents = function(callback) {
const url = config.TBA_URL + "/events/2022/simple?X-TBA-Auth-Key=" + keys.TBA_API_KEY;
https.get(url, res => {
var body = "";
res.setEncoding("utf8");
res.on("error", error => {
callback(null, error);
})
res.on("data", data => {
body += data;
});
res.on("end", () => {
var data = JSON.parse(body);
var events = data.map(function (event) {
return {
"key": event.key,
"name": event.name,
"current": event.key == utils.getCurrentEvent()
}
});
events.sort(function(a,b){
return (a.name<b.name) ? -1 : 1;
});
events.splice(0, 0, {
"key": "practice",
"name": "Practice Competition",
"current": utils.getCurrentEvent() == "practice"
});
callback(events, null);
});
});
}
module.exports.getImage = function(team, callback) {
const url = config.TBA_URL + "/team/frc" + team + "/media/2022?X-TBA-Auth-Key=" + keys.TBA_API_KEY;
https.get(url, res => {
var body = "";
res.setEncoding("utf8");
res.on("error", error => {
callback(null, error);
})
res.on("data", data => {
body += data;
});
res.on("end", () => {
var data = JSON.parse(body);
for (var media in data) {
if (data[media]["type"] == "imgur") {
callback("https://i.imgur.com/" + data[media]["foreign_key"] + ".jpg", null);
return;
}
}
callback(null, null);
});
});
}