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
/
extractInfo.js
164 lines (159 loc) · 6.33 KB
/
extractInfo.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
/**
* This script parses course information from data/src/YYMMDDHHmmss/*.json
* This script should trigger once after every fetch since information may change
*/
const fs = require("fs");
const jsonfile = require("jsonfile");
const logger = require("./logger")("parser");
/**
* Extract basic course information
* @param {String} semester - string for semester, e.g. '1810'
* @param {String} folderName - the string of YYMMDDHHmmss, e.g. '1809011315'
* @param {String} subjectName - should not include '.json', e.g. 'ACCT'
*/
const extractBasicInfo = (semester, folderName, subjectName) => {
jsonfile.readFile(
`./data/${semester}/src/${folderName}/${subjectName}.json`,
(err, data) => {
if (err) console.error(err);
Object.entries(data).map(([_, course]) => {
return Object.entries(course["sections"]).map(([_, section]) => {
delete section["quota"];
delete section["avail"];
delete section["enrol"];
delete section["wait"];
return section;
});
});
fs.existsSync(`./data/${semester}/info/`) ||
fs.mkdirSync(`./data/${semester}/info/`);
fs.writeFileSync(
`./data/${semester}/info/${subjectName}.json`,
JSON.stringify(data),
{
mode: 0755
}
);
logger.info(`${subjectName}.json course info parsed`);
}
);
};
/**
* Extract course quota information
* @param {String} semester - string for semester, e.g. '1810'
* @param {String} folderName - the string of YYMMDDHHmmss, e.g. '1809011315'
* @param {String} subjectName - should not include '.json', e.g. 'ACCT'
*/
const extractQuotaInfo = (semester, folderName, subjectName) => {
jsonfile.readFile(
`./data/${semester}/src/${folderName}/${subjectName}.json`,
(err, data) => {
if (err) console.error(err);
Object.entries(data).map(([_, course]) => {
delete course["title"];
delete course["credits"];
delete course["details"];
return Object.entries(course["sections"]).map(([_, section]) => {
delete section["code"];
delete section["dateTime"];
delete section["room"];
delete section["instructors"];
delete section["remarks"];
return section;
});
});
fs.existsSync(`./data/${semester}/quota/`) ||
fs.mkdirSync(`./data/${semester}/quota/`);
if (!fs.existsSync(`./data/${semester}/quota/${subjectName}.json`)) {
fs.writeFileSync(
`./data/${semester}/quota/${subjectName}.json`,
JSON.stringify(data),
{
mode: 0755
}
);
logger.info(`${subjectName}.json course quota parsed`);
} else {
const originalData = jsonfile.readFileSync(
`./data/${semester}/quota/${subjectName}.json`
);
const newData = [originalData]
.concat(data) // Put originalData and data into an array and trigger reduce
.reduce((result, currentData) => {
return (
Object.keys(currentData).forEach(currentCourseIndex => {
// currentData can be {originalData, data}, loops through courses in data
if (!result[currentCourseIndex])
// if course does not exists, just concat
result[currentCourseIndex] = currentData[currentCourseIndex];
// otherwise
else {
Object.keys(
currentData[currentCourseIndex]["sections"]
).forEach(
// loop through sections of a course
currentSectionIndex => {
const resultSection = // pointer to current resultSection
result[currentCourseIndex]["sections"][
currentSectionIndex
];
const currentSection = // pointer to current currentSection
currentData[currentCourseIndex]["sections"][
currentSectionIndex
];
if (!resultSection)
// if section does not exist, just concat
result[currentCourseIndex]["sections"][
currentSectionIndex
] =
currentData[currentCourseIndex]["sections"][
currentSectionIndex
];
else {
// otherwise
const entries = ["quota", "enrol", "avail", "wait"];
entries.forEach(entry => {
// consider the 4 entires, loop through them and concat them
resultSection[entry] = resultSection[entry].concat(
currentSection[entry]
);
});
}
}
);
// Now find the entries is originalData that no longer exist
Object.keys(result[currentCourseIndex]["sections"])
.diff(
Object.keys(currentData[currentCourseIndex]["sections"])
)
// loop through them
.forEach(item => {
const entries = ["quota", "enrol", "avail", "wait"];
entries.forEach(entry => {
// consider the 4 entires, loop through them and append null
result[currentCourseIndex]["sections"][item][
entry
].push(null);
});
});
}
}),
result
); // default return value
}, {});
fs.writeFile(
`./data/${semester}/quota/${subjectName}.json`,
JSON.stringify(newData),
{
mode: 0755
},
err => {
if (err) logger.error(err);
}
);
logger.info(`${subjectName}.json course quota parsed and appended`);
}
}
);
};
module.exports = { extractBasicInfo, extractQuotaInfo };