-
Notifications
You must be signed in to change notification settings - Fork 1
/
getMajors.js
56 lines (47 loc) · 1.47 KB
/
getMajors.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
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const majorsUrl = "http://www.registrar.ucla.edu/Faculty-Staff/Courses-and-Programs/Major-and-Minor-Codes/Undergraduate-Majors-and-Premajors"
const fileDest = __dirname + '/src/majors.json';
function writeToFile(majors) {
fs.writeFile(fileDest, JSON.stringify(majors), function(err) {
if (err) {
console.error("Error writing to majors.json file");
process.exit(1);
} else {
console.log("Successfully wrote majors to file!");
}
});
}
function extractMajors(arr) {
majors = [];
arr.forEach(function(string) {
const singleMajor = string.split('\n');
if (singleMajor[3].trim() == '' || singleMajor[3].trim() == 'I A STD') {
majors.push(singleMajor[4].trim());
} else {
majors.push(singleMajor[3].trim());
}
});
writeToFile(majors);
}
function parseHtml(body) {
strings = [];
const $ = cheerio.load(body);
$("#myTable").find('tbody').find('tr').each(function(idx, thing) {
strings.push($(this).text());
});
extractMajors(strings);
}
// get the html from the registrar website
function getHtml() {
request(majorsUrl, function(error, res, body) {
if (error) {
console.error("error getting HTML: ", error);
process.exit(1);
} else {
parseHtml(body);
}
})
}
getHtml();