-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (91 loc) · 3.3 KB
/
app.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
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
var http = require('http');
var url = require('url');
var fs = require('fs');
var handlebars = require('handlebars');
var axios = require('axios');
process.config = require("./config.json")
var teamNum = process.config.teamNumber;
var token = process.config.tba_key;
handlebars.registerHelper("printTime", function(timestamp) {
var date = new Date(timestamp * 1000);
return date.toLocaleTimeString('en-US', { hour: '2-digit', minute:'2-digit' }) + " PST";
});
handlebars.registerHelper("slice", function(team){
var sliced = team.slice(3);
if (sliced === teamNum) {
return "<b>" + teamNum + "</b>";
} else {
return sliced;
}
});
handlebars.registerHelper("winner", function(winner){
if(winner === "red"){
return "red";
}else if(winner === "blue"){
return "blue";
}else{
return "green";
}
})
handlebars.registerHelper("removeFRC", function(team){
return team.slice(3);
})
http.createServer(async (req, res) => {
var path = url.parse(req.url).pathname;
var api_path = "";
var template_name = "";
var request = await axios.get('https://frc-api.firstinspires.org/v2.0')
console.log('frc-api.firstinspires.org:' + request.status)
var season = request.data.currentSeason.toString()
if (path === '/events' || path === "/events/") {
api_path = `/api/v3/team/frc${teamNum}/events/${season}/simple`;
template_name = 'events.html';
} else if (path.startsWith('/matches/')) {
var eventCode = path.split('/')[2];
api_path = '/api/v3/team/frc' + teamNum + '/event/' + season + eventCode + '/matches/simple';
template_name = 'matches.html';
} else if (path.startsWith('/stream/')){
var event = path.split('/')[2];
api_path = '/api/v3/event/' +season + event
template_name = 'stream.html';
} else if (path.startsWith('/events/')) {
var eventnum = path.split('/')[2];
api_path = '/api/v3/event/' + season + eventnum;
template_name = 'options.html';
}else if (path === "/") {
template_name = "index.html";
} else {
template_name = "404.html";
}
var template = handlebars.compile(fs.readFileSync('templates/header.html', 'utf8') + fs.readFileSync('templates/' + template_name, 'utf8'));
if (api_path) {
axios({
method: 'GET',
url: 'https://thebluealliance.com' + api_path,
headers: {
'Content-Type': 'application/json',
'User-Agent': 'frc2022',
'X-TBA-Auth-Key': token
}
}).then(function(response){
console.log('thebluealliance.com:'+response.status)
if(response && response.status === 200) {
if (template_name === 'matches.html'){
response.data.sort(function(a, b){
return a.match_number - b.match_number
})
res.end(template({'data': response.data}))
} else{
res.end(template({'data': response.data}))
}
}
else{
res.end(template({}))
}
}).catch(function(error){
console.log(error)
})
} else {
res.end(template({}));
}
}).listen(process.config.port);