This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (178 loc) · 5.37 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
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
const express = require("express");
const bodyParser = require("body-parser");
const fetchCourses = require("./fetch");
const schedule = require("node-schedule");
const logger = require("./logger")("api");
const jsonfile = require("jsonfile");
const util = require("util");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
let jobs = [];
/**
* Find the difference between two arrays
* @param {Array} a Another array to compare to
* @example a.diff(b) - finds the difference in entry between a and b
*/
Array.prototype.diff = function (a) {
return this.filter(function (i) {
return a.indexOf(i) < 0;
});
};
/**
* POST Scrape configuration
*
* @param apiKey API key for updating configuration
* @param config Configuration list, each object in list should include
* - start, task start time
* - end, task end time
* - per, interval in minutes
*/
app.route("/api/config").post((req, res) => {
if (req.body.apiKey !== process.env.apiKey) {
res.sendStatus(401);
} else {
let config = req.body.config;
if (config === null || config === undefined) {
// Cancel all current jobs
jobs.forEach(el => el.cancel());
return res.json({ cleared: true });
}
// Check if invalid
for (rule of config) {
console.log(rule);
if (
rule.start === null ||
rule.start === undefined ||
rule.end === null ||
rule.end === null ||
!Number.isInteger(rule.per)
) {
return res.sendStatus(400);
}
}
// Map each config json into json that schedule takes
config = config.map(rule => {
return {
start: new Date(rule.start),
end: new Date(rule.end),
rule: `0 */${rule.per} * * * *`
};
});
logger.info(
`Got scheduling request: ${util.inspect(config, false, null, true)}`
);
// Cancel all current jobs
jobs.forEach(el => el.cancel());
// Add new job
for (rule of config) {
jobs.push(
schedule.scheduleJob(rule, fireDate => {
logger.info(`fetchCourses() fired at ${fireDate}`);
fetchCourses(1000);
})
);
}
return res.json({ done: true });
}
});
/**
* GET List of subjects
* @param semester Semester id, for example '1810'
*/
app.route("/api/:semester/").get((req, res) => {
try {
logger.info(`/api/${req.params.semester} called.`);
const subjects = jsonfile.readFileSync(
`./data/${req.params.semester}/subjects.json`
);
res.json(subjects);
} catch (error) {
res.sendStatus(404);
}
});
/**
* GET List of courses
* @param semester Semester id, for example '1810'
* @param subject Subject name, for example 'ACCT'
*/
app.route("/api/:semester/:subject/").get((req, res) => {
try {
logger.info(`/api/${req.params.semester}/${req.params.subject} called.`);
const subject = jsonfile.readFileSync(
`./data/${req.params.semester}/info/${req.params.subject}.json`
);
const courses = Object.keys(subject);
res.json(courses);
} catch (error) {
res.sendStatus(404);
}
});
/**
* GET List of section codes
* @param semester Semester id, for example '1810'
* @param subject Subject name, for example 'ACCT'
* @param course Course code, for example '1010'
*/
app.route("/api/:semester/:subject/:course/").get((req, res) => {
try {
logger.info(
`/api/${req.params.semester}/${req.params.subject}/${
req.params.course
} called.`
);
const subject = jsonfile.readFileSync(
`./data/${req.params.semester}/info/${req.params.subject}.json`
);
var codes = {};
Object.keys(subject[req.params.course]["sections"]).forEach(key => {
codes[key] = subject[req.params.course]["sections"][key]["code"];
});
res.json(codes);
} catch (error) {
res.sendStatus(404);
}
});
/**
* GET List of section codes
* @param semester Semester id, for example '1810'
* @param subject Subject name, for example 'ACCT'
* @param course Course code, for example '1010'
* @param section Section id, for example '1017' which corresponds to L1
*/
app.route("/api/:semester/:subject/:course/:section").get((req, res) => {
try {
logger.info(
`/api/${req.params.semester}/${req.params.subject}/${req.params.course}/${
req.params.section
} called.`
);
const times = jsonfile.readFileSync(
`./data/${req.params.semester}/times.json`
);
var data = jsonfile.readFileSync(
`./data/${req.params.semester}/quota/${req.params.subject}.json`
);
// remove id
var { id, ...data } = data[req.params.course]["sections"][
req.params.section
];
const entries = ["avail", "enrol", "quota", "wait"];
var graphData = [];
// make into appropriate format
entries.forEach(entry => {
var graphDatum = {};
graphDatum["name"] = entry;
graphDatum["data"] = {};
data[entry].forEach((datum, index) => {
graphDatum["data"][times[index]] = datum;
});
graphData.push(graphDatum);
});
res.json(graphData);
} catch (error) {
res.sendStatus(404);
}
});
app.listen(port, () => logger.info(`Listening on port ${port}`));