Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (62 loc) · 1.53 KB
/
Copy pathindex.js
File metadata and controls
75 lines (62 loc) · 1.53 KB
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
// Set up modules
var request = require('request');
var cheerio = require('cheerio');
// Process invokation
exports.handler = function(event, context, callback) {
// Response placeholder
var resp = {};
new Promise(function(resolve, reject) {
// Check for url
if (!event.url)
reject('Classroom "url" parameter is required.');
// Make request
request(event.url, function(error, response, html) {
// Check for error
if (error)
reject('Error with HTTP request.');
// Go to process HTML
resolve(html);
});
})
// Process HTML
.then(function(html) {
var classInfo = classObj = null;
// Load page into Cheerio.
var $ = cheerio.load(html);
$('.no-bottom-margin').filter(function() {
// Get date
var prevElem = $(this).prev();
var date = (prevElem.html().split('<br>')[1]).trim();
// Add to response
resp[date] = [];
// Get classes
$(this).find('.row').each(function(i, classElem) {
// Set up object
classObj = {};
// Get class info
$(this).find('.data').each(function(j, classInfoElem) {
// Set up data
classInfo = $(this).text().trim();
// Start
if (j == 0)
classObj.start = classInfo;
// End
else if (j == 1)
classObj.end = classInfo;
// Name
else if (j == 2)
classObj.name = classInfo;
});
// Add to date
resp[date].push(classObj);
});
});
// Respond with response
callback(null, resp);
})
// Catch exceptions
.catch(function(e) {
// Respond with error.
callback(e);
});
}