forked from VIAplanner/via-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.js
More file actions
58 lines (53 loc) · 1.81 KB
/
manager.js
File metadata and controls
58 lines (53 loc) · 1.81 KB
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
const express = require("express")
const cors = require("cors")
const rateLimit = require("express-rate-limit");
const router = new express.Router()
const {
PdfData,
VerbosityLevel
} = require('pdfdataextract');
const multer = require('multer');
const upload = multer();
let corsOptions = {
origin: 'https://timetable.viaplanner.ca', // allow only viaplanner to use the api
optionsSuccessStatus: 200
}
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per 15 minutes, so 9 requests per seconds
});
const re = /(Class Participation|Lab|Assignment|Term Test|Final Exam|Other|Quiz|Presentations|Final Exam Changed To)(.*|(?:.*\n+)*)(On-going|TBA|TBD|\d{4}-\d{2}-\d{2})+ (\d{1,3}%)\n/gmi;
router.post('/manager/parser', [upload.single('syllabus'), limiter, cors(corsOptions)], (req, res) => {
PdfData.extract(req.file.buffer, {
verbosity: VerbosityLevel.ERRORS, // set the verbosity level for parsing
get: { // enable or disable data extraction (all are optional and enabled by default)
text: true, // get text of each page
},
}).then((data) => {
let result = [];
let content = data.text.join();
let matches = [...content.matchAll(re)];
for (const match of matches) {
const item = {
type: match[1],
description: match[2].trim(),
deadline: /\d{4}-\d{2}-\d{2}/.test(match[3]) ? match[3] : null,
on_going: match[3] === 'On-going',
weight: match[4]
}
result.push(item);
}
if (result.length === 0) {
res.status(400).send({
message: 'Invalid syllabus format. Cannot parse the uploaded syllabus.'
})
} else {
res.status(200).send(result);
}
}).catch(e => {
res.status(500).send({
message: e.message
})
})
})
module.exports = router