-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (184 loc) · 5.83 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import ical2json from "ical2json";
import fs from "node:fs";
import got from "got";
import express from "express";
import compression from "compression";
import ShortUniqueId from "short-unique-id";
const app = express();
const port = 3000;
const uid = new ShortUniqueId({ length: 8 });
import { fileURLToPath } from "node:url";
import { dirname, join, resolve } from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const config = JSON.parse(
fs.readFileSync(resolve(__dirname, "./config/config.json"), "utf-8")
);
app.use(express.static("public"));
app.use(
express.urlencoded({
extended: true,
})
);
app.use(compression());
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.sendFile(join(__dirname, "/public/index.html"));
});
app.get("/register", (req, res) => {
const url = req.query.timeEditUrl;
if (url.startsWith("https://cloud.timeedit.net/") && url.endsWith(".ics")) {
const userid = uid.rnd();
config[userid] = {};
config[userid].url = url;
fs.writeFileSync("./config/config.json", JSON.stringify(config, null, 2));
res.redirect(`/config/${userid}`);
} else {
res.send("Invalid URL, make sure it is an .ics feed from TimeEdit");
}
});
app.post("/config/:user/save", (req, res) => {
const user = req.params.user;
console.log(req.body);
if (req.body.option1 && req.body.option1 === "option1") {
config[user].option1 = true;
} else {
config[user].option1 = false;
}
if (req.body.option2 && req.body.option2 === "option2") {
config[user].option2 = true;
} else {
config[user].option2 = false;
}
if (req.body.removeCourses && typeof req.body.removeCourses === "string") {
config[user].removeCourses = req.body.removeCourses
.replaceAll(" ", "")
.split(",");
}
res.redirect(`/config/${user}`);
fs.writeFileSync("./config/config.json", JSON.stringify(config, null, 2));
});
app.get("/config/:user", (req, res) => {
const user = req.params.user;
let url = "error";
if (config[user].url) {
url = config[user].url;
}
let removeCourses = "";
if (config[user].removeCourses) {
removeCourses = config[user].removeCourses.join(", ");
}
let option1 = true;
if (config[user].option1) {
option1 = config[user].option1;
}
let option2 = false; // Default to false
if (config[user].option2) {
option2 = config[user].option2;
}
res.render("config", {
url: url,
user: user,
removeCourses: removeCourses,
option1: option1,
option2: option2,
});
});
app.get("/cal/:user.ics", async (req, res) => {
const user = req.params.user;
console.log(user);
console.log(config[user]);
console.log(config[user].url);
if (config[user]?.url) {
const toSend = await fetchyFilter(config[user]);
res.set({
"Content-Disposition": `attachment; filename=${user}.ics`,
"Content-type": "text/calendar",
});
res.send(toSend);
} else {
res.send(`${user} not found`);
}
});
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
const filterEvents = (events, user) => {
const removeCourses = user?.removeCourses || [];
// Plaintext Replacement
let plaintext = JSON.stringify(events);
plaintext = plaintext.replaceAll("Kurskod\\\\, Kursnamn: ", "");
plaintext = plaintext.replaceAll("Kursnamn: ", "");
plaintext = plaintext.replaceAll("Kurskod: ", "");
plaintext = plaintext.replaceAll("Course name: ", "");
plaintext = plaintext.replaceAll("Rubrik: ", "");
plaintext = plaintext.replaceAll("Aktivitet: ", "");
let filteredEvents = JSON.parse(plaintext);
filteredEvents = filteredEvents.map((event) => {
event.DESCRIPTION = event.DESCRIPTION.replaceAll(
'";CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:invalid:nomail',
""
);
event.DESCRIPTION = event.DESCRIPTION.replaceAll(
"VIDUAL;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:invalid:nomail",
""
);
return event;
});
// JSON Filtering
if (user?.option1) {
filteredEvents = filteredEvents.filter(
(event) =>
!event.SUMMARY.startsWith("CHARM") &&
!event.SUMMARY.startsWith("Tentaanmälan") &&
!event.SUMMARY.startsWith("Tentamen") &&
!event.SUMMARY.startsWith("Självstudier") &&
!event.SUMMARY.startsWith("Nyårsdagen") &&
!event.SUMMARY.startsWith("Nyårsafton") &&
!event.SUMMARY.includes("Holiday") &&
!event.SUMMARY.includes("Omtentamen") &&
!event.SUMMARY.includes("Anmälan omtenta")
);
}
for (let i = 0, l = removeCourses.length; i < l; i++) {
filteredEvents = filteredEvents.filter(
(event) => !event.SUMMARY.includes(removeCourses[i])
);
}
if (user?.option2) {
// copy location to title and title to location
filteredEvents = filteredEvents.map((event) => {
const temp = event.SUMMARY;
event.SUMMARY = event.LOCATION;
event.LOCATION = temp;
return event;
});
}
return filteredEvents;
};
// Fetch and filter and to serve it back to the client
const fetchyFilter = async (user) => {
const url = user.url;
try {
const response = await got(url);
if (response.statusCode !== 200) {
console.error("Failed to fetch calendar");
return "Failed to fetch calendar";
}
const output = ical2json.convert(response.body);
output.VCALENDAR[0].PRDOID = "-//TimeEditEd\\\\\\, //TimeEditEd//EN";
output.VCALENDAR[0]["X-WR-CALNAME"] = output.VCALENDAR[0][
"X-WR-CALNAME"
].replace("TimeEdit-", "");
const events = output.VCALENDAR[0].VEVENT;
const filteredEvents = await filterEvents(events, user);
output.VCALENDAR[0].VEVENT = filteredEvents;
const result = ical2json.revert(output);
console.log(result);
return result;
} catch (error) {
console.error(error);
return "Failed to fetch calendar";
}
};
// fetchyFilter(config.emrik);