-
Notifications
You must be signed in to change notification settings - Fork 0
/
course.js
78 lines (69 loc) · 2.31 KB
/
course.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
const _ = require('lodash');
const fp = require('lodash/fp');
const Promise = require('bluebird');
const rp = require('request-promise');
const cheerio = require('cheerio');
const { composeUri } = require('./util');
const argv = require('yargs').argv;
let parsers = {
status: (v) => cheerio.load(v)('span').first().text(),
type: (v) => v,
section: (v) => String(cheerio.load(v).text()).substring(0,3),
time: (v) => {
var reg = /\d{2}:\d{2}[A-Z]{2}\s-\s\d{2}:\d{2}[A-Z]{2}/gmi;
return _.uniq(String(cheerio.load(v)('div').text()).match(reg));
},
day: (v) => {
var reg = /[A-Z]+/gmi;
return _.uniq(String(cheerio.load(v).text()).match(reg));
},
location: (v) => {
var locations = [];
$ = cheerio.load(v);
$('div').each(function (i, elem) {
locations.push($(this).text());
});
return locations;
},
instructor: (v) => {
var reg = /[A-Z][a-z]*,\s[A-Z]/gm;
return _.uniq(String(cheerio.load(v).text()).match(reg));
}
};
module.exports = fetchAndParseCourse = ({ path }) => {
var options = {
uri: composeUri([ 'schedule', ...path ]),
transform: function (body) {
return { $: cheerio.load(body), body };
}
};
return rp(options).then(({ $, body }) => {
let course = {};
let couseString = String($('.app-inline').text());
course.subject = couseString.substring(0, couseString.indexOf(' '));
course.course = couseString.substring(couseString.indexOf(' ') + 1);
course.title = $('.app-label.app-text-engage').text();
course.gened = [];
$('.list-unstyled.sort-list li').each(function () {
course.gened.push($(this).text());
});
course.sections = [];
try {
$('script').each((idx, el) => {
let c = $(el).html();
if (c.indexOf('sectionDataObj') != -1) {
course.sections = eval('(function(){' + c + ';return sectionDataObj;})()');
}
});
} catch (e) {};
course.sections = _.map(course.sections, section => _.pick(_.mapValues(section, (v, k, o) => {
if (_.has(parsers, k)) return parsers[k](v); else return v;
}), [ 'status', 'crn', 'section', 'time', 'day', 'location', 'instructor' ]));
return course;
});
};
if (require.main === module) {
return fetchAndParseCourse({
path: _.split(argv.path, /,\//g)
}).then(data => { console.log(data); });
}