-
Notifications
You must be signed in to change notification settings - Fork 0
/
TBA.js
58 lines (56 loc) · 1.43 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
const https = require("https");
const config = require("./config");
const utils = require("./utils");
const keys = require("./keys");
module.exports.getEvents = function(callback) {
const url = config.TBA_URL + "/events/2018/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.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/2018?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);
});
});
}