This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (56 loc) · 1.57 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
const debug = require('debug')('server');
const express = require('express');
const app = express();
const {
parseURL,
checkURL,
fetchCSV,
parseCSV,
getCourseCodes,
getRules,
getEvents,
filterEvents,
finalizeEvents
} = require('./methods.js');
app.get('/', (req, res) => {
debug('serving instructions');
res.set('content-type', 'text/plain; charset=UTF-8');
res.send(`
1. Välj kurser m.m. på TimeEdit som vanligt.
2. Klicka på 'Prenumerera'
3. Kopiera länken
4. Lägg till ett snedstreck efter den nuvarande URL:en och klistra in länken`);
});
app.get('*', (req, res) => {
if (req.url.indexOf('.ico') !== -1)
return res.status(404).send('Not found');
// Make sure that the url is parsed in full
if (req.url.indexOf('/') === 0)
req.url = req.url.substr(1);
const school = req.url.split('/')[4];
parseURL({url: req.url, school})
.then(checkURL)
.then(fetchCSV)
.then(parseCSV)
.then(getCourseCodes)
.then(getRules)
.then(getEvents)
.then(filterEvents)
.then(finalizeEvents)
.then(ical => {
debug('handling result');
res.set('content-type', 'text/calendar; charset=UTF-8');
res.set('content-disposition', 'attachment; filename="Schema"');
res.send(ical);
// Log school name
debug(`served schedule for: ${req.url.split('/')[3]}`);
})
.catch(error => res.status(500).send(error));
});
app.listen(3000, () => {
debug('started server');
});
// Handle unhandled rejections
process.on('unhandledRejection', reason => {
debug('unhandled rejection due to: ' + reason);
});