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
/
fetch.js
131 lines (123 loc) · 3.96 KB
/
fetch.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
const request = require("request-promise");
const cheerio = require("cheerio");
const fs = require("fs");
const moment = require("moment");
const logger = require("./logger")("fetcher");
const { extractBasicInfo, extractQuotaInfo } = require("./extractInfo");
const { Course } = require("./course");
const domain = "https://w5.ab.ust.hk/";
const uri = "https://w5.ab.ust.hk/wcq/cgi-bin/";
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); // Sleep function in ms
/**
* Fetch information from ARRO website
* @param {Integer} timeout - Timeout per request in ms
*/
const fetchCourses = timeout =>
request({
uri: uri,
transform: (body, response) => {
return [response, cheerio.load(body)];
}
})
.then(([response, $]) => {
const semester = response.request.uri.href.match(/\d{4}/g)[0];
const depts = $(".depts")
.find("a")
.map((index, el) => {
var obj = {};
obj["subject"] = $(el).text();
obj["href"] = domain + $(el).attr("href");
return obj;
})
.get();
const currentTime = moment();
const currentTimeFormated = moment(currentTime).format("YYMMDDHHmmss");
fs.existsSync("./data") || fs.mkdirSync("./data");
fs.existsSync(`./data/${semester}`) || fs.mkdirSync(`./data/${semester}`);
fs.existsSync(`./data/${semester}/src`) ||
fs.mkdirSync(`./data/${semester}/src`);
fs.mkdirSync(`./data/${semester}/src/${currentTimeFormated}/`);
for (var i = 0; i < depts.length; i++) {
setTimeout(
async i => {
await parseSubject(semester, currentTimeFormated, depts[i]);
extractBasicInfo(
semester,
currentTimeFormated,
depts[i]["subject"]
);
extractQuotaInfo(
semester,
currentTimeFormated,
depts[i]["subject"]
);
},
timeout * i,
i
);
}
var times = [moment(currentTime).format()];
if (fs.existsSync(`./data/${semester}/times.json`)) {
times = JSON.parse(
fs.readFileSync(`./data/${semester}/times.json`).toString()
).concat(times);
}
fs.writeFile(
`./data/${semester}/times.json`,
JSON.stringify(times),
{
mode: 0755
},
err => {
if (err) logger.error(err);
}
);
logger.info(`times.json updated.`);
fs.writeFile(
`./data/${semester}/subjects.json`,
JSON.stringify(depts.map(dept => dept["subject"])),
{
mode: 0755
},
err => {
if (err) logger.error(err);
}
);
logger.info(`subjects.json updated.`);
})
.catch(logger.error);
/**
* The entry point of parsing one subject
* @param {String} semester - Semester id
* @param {String} time - Current time
* @param {Object} subject - Subject code and link
*/
function parseSubject(semester, time, subject) {
return request({
uri: subject["href"],
transform: function(body) {
return cheerio.load(body);
}
})
.then($ => {
const raw_courses = $("div.course");
var courses = {};
for (var i = 0; i < raw_courses.length; i++) {
const raw_course = $(raw_courses[i]);
var course = new Course($);
course
.parseTitle(raw_course.find("h2").text())
.parseCourseInfo(raw_course.find(".courseattr .popupdetail")[0])
.parseSections(raw_course.find(".sections")[0]);
courses[course["code"]] = course;
}
fs.writeFileSync(
`./data/${semester}/src/${time}/${subject.subject}.json`,
JSON.stringify(courses),
{ mode: 0755 }
);
logger.info(`${subject.subject}.json written.`);
})
.catch(logger.error);
}
module.exports = fetchCourses;