-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
74 lines (58 loc) · 2.57 KB
/
scraper.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
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
/** disable-eslint */
function parseSchedule(id) {
return new Promise(async (resolve, reject) => {
const url = `https://classes.cornell.edu/shared/schedule/${id}`;
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })
const page = await browser.newPage();
await page.goto(url, { timeout: 70000 });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = cheerio.load(html);
// Initialize a map of classes on schedule indicating that it is a
// selected class
const coursesMap = {};
const scheduleClasses = $('.fc-content');
// Loop through all classes on schedule
for (let i = 0; i < scheduleClasses.length; i++) {
const scheduleClass = scheduleClasses[i];
const courseCode = $(scheduleClass).children('span').first().text().trim();
if (!coursesMap[courseCode]) coursesMap[courseCode] = true;
}
// Initialize list for class objects
let classList = []
const expander = $(".expander.ng-binding");
// Loops through all expanders
for (let i = 0; i < expander.length; i++) {
const courseHeader = expander[i];
// Grab course codes
courseCode = $(courseHeader.children[1]).text().trim();
// If the course is in the map on the schedule
if (coursesMap[courseCode]) {
// Initialize the class object
let classInfo = {}
classInfo['course'] = courseCode;
// Grab course name
courseNameRaw = $(courseHeader).text().trim();
courseName = courseNameRaw.split(' ').slice(2).join(" ");
classInfo['name'] = courseName;
// Grab section ids
const coursePinnedSection = $(courseHeader.parent.parent).find('.ng-binding.classnbr-pinned').toArray();
classInfo['section'] = coursePinnedSection.map(section => section.children[0].data.trim());
// Grab days for specific sections
const coursePinnedDays = $(courseHeader.parent.parent).find('.mtg-pat.classnbr-pinned').toArray();
classInfo['days'] = coursePinnedDays.map(day => $(day.children[2]).text().trim());
// Grab times for specific sections
const coursePinnedTimes = $(courseHeader.parent.parent).find('.mtg-time.classnbr-pinned').toArray();
classInfo['times'] = coursePinnedTimes.map(time => $(time.children[2]).text().trim());
// Add to class list
classList.push(classInfo);
}
}
resolve(classList);
await browser.close();
})
}
module.exports = {
parseSchedule
}