-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (77 loc) · 2.45 KB
/
index.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
let express = require('express'),
app = express(),
axios = require('axios');
app.set('view engine', 'pug');
if (process.env.NODE_ENV !== 'production') {
app.use(require('morgan')('dev'));
}
async function getToday() {
let response = await axios.get('https://rezervacije.fri.uni-lj.si/sets/rezervacije_fri/types/classroom/time_view');
return response.data.res_list
}
async function getOccupiedStatus(res_list) {
let response = [];
for (let classroom of res_list) {
let reservations = classroom.reservations;
for (let period of reservations) {
if (nowBetween(period.start, period.end)) {
let current = await getCurrent(period);
response.push({
name: classroom.reservable.slug,
free: current.free,
reason: current.reason
})
}
}
}
return response
}
async function getReservation(resId) {
let response = await axios.get('https://rezervacije.fri.uni-lj.si/reservations/'+resId+'/');
return response.data;
}
async function getCurrent(period) {
if (period.reservations.length == 0) return {
free: true,
reason: null
};
for (let resId of period.reservations) {
let reservation = await getReservation(resId);
if (nowBetween(reservation.start, reservation.end)) return {
free: false,
reason: reservation.reason
};
}
return {
free: true,
reason: null
};
}
function nowBetween(startS, endS) {
let start = new Date(startS);
let end = new Date(endS);
let now = new Date();
if (now > start && now < end) return true;
return false;
}
app.get('/classrooms', async (req, res) => {
let res_list = await getToday();
let response = await getOccupiedStatus(res_list);
res.send(response);
})
app.get('/classrooms/:resource', async (req, res) => {
let res_list = (await getToday())
.filter((element) => {
if (element.reservable.resources.indexOf(parseInt(req.params.resource))>=0) return true;
return false;
})
let response = await getOccupiedStatus(res_list);
res.send(response);
})
app.get('/', (req, res) => {
res.render('status');
})
app.use(express.static('./static'));
app.use(express.static('./node_modules/bootstrap/dist'));
let port = process.env.PORT || 3000;
app.listen(port, () => console.log('listening on', port));