-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (114 loc) · 3.78 KB
/
server.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
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
const express = require("express");
const app = express();
const untis = require("./untis");
app.use((req, res, next) => {
console.log(req.originalUrl);
next();
});
app.use("/", express.static("public"));
app.get("/timetable", async (req, res) => {
let weeks = req.query.weeks;
if (weeks == null) weeks = 1;
weeks = parseInt(weeks);
if (weeks < 1) weeks = 4;
if (weeks > 4) weeks = 4;
let username = req.query.username;
let password = req.query.password;
let school = req.query.school;
let baseurl = req.query.baseurl;
if (
username == null ||
password == null ||
school == null ||
baseurl == null
) {
res.status(400).send("Missing required query params");
return;
}
try {
await untis.login(school, username, password, baseurl);
} catch {
res.status(400).send();
}
let basedate = untis.util.getNextMonday(new Date());
let tempcal;
let cal_collection = [];
try {
for (let i = 0; i < weeks; i++) {
tempcal = (await untis.getWeeklyTimetableICAL(basedate))
.replace(/\r?\nEND:VCALENDAR/g, "")
.split("BEGIN:VEVENT");
tempcal.shift();
cal_collection.push(tempcal);
basedate = untis.util.getNextMonday(
new Date(basedate.getTime() + 7 * 24 * 60 * 60 * 1000),
);
}
} catch (e) {
console.error(e);
res.status(400).send("An unknown error occured");
return;
}
let cal =
"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//WebUntis//WebUntis//EN\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nX-WR-CALNAME:WebUntis\nX-WR-TIMEZONE:Europe/Berlin\nX-WR-CALDESC:WebUntis\nREFRESH-INTERVAL;VALUE=DURATION:P1H\n";
for (let i = 0; i < cal_collection.length; i++) {
for (let j = 0; j < cal_collection[i].length; j++) {
cal += "BEGIN:VEVENT" + cal_collection[i][j];
}
}
cal += "END:VCALENDAR";
res.set("Content-Type", "text/calendar");
res.send(cal);
await untis.logout();
});
app.get("/kendltimetable", async (req, res) => {
// weeks
let weeks = req.query.weeks;
if (weeks == null) weeks = 1;
weeks = parseInt(weeks);
if (weeks < 1) weeks = 1;
if (weeks > 4) weeks = 4;
try {
await untis.login();
} catch (e) {
console.error(e);
res.status(400).send();
return;
}
console.log("Logged in");
let basedate = untis.util.getNextMonday(new Date());
let tempcal;
let cal_collection = [];
try {
for (let i = 0; i < weeks; i++) {
tempcal = (await untis.getWeeklyTimetableICAL(basedate))
.replace(/\r?\nEND:VCALENDAR/g, "")
.split("BEGIN:VEVENT");
tempcal.shift();
cal_collection.push(tempcal);
basedate = untis.util.getNextMonday(
new Date(basedate.getTime() + 7 * 24 * 60 * 60 * 1000),
);
}
} catch (e) {
console.error(e);
res.status(400).send("An unknown error occured");
}
let cal =
"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//WebUntis//WebUntis//EN\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nX-WR-CALNAME:WebUntis\nX-WR-TIMEZONE:Europe/Berlin\nX-WR-CALDESC:WebUntis\nREFRESH-INTERVAL;VALUE=DURATION:P1H\n";
for (let i = 0; i < cal_collection.length; i++) {
for (let j = 0; j < cal_collection[i].length; j++) {
cal += "BEGIN:VEVENT" + cal_collection[i][j];
}
}
cal += "END:VCALENDAR";
res.set("Content-Type", "text/calendar");
res.send(cal);
await untis.logout();
});
app.get("/", async (req, res) => {
res.redirect("/swagger/index.html");
});
app.listen(process.env.PORT || 3000, () => {
console.log("Server started");
});