-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
198 lines (176 loc) · 4.6 KB
/
scrape.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var http = require('http');
var https = require('https');
var url = require('url');
var fs = require('fs');
// remove duplicates from an array
function uniq(arr) {
return arr.filter(function (el, i) {
return arr.indexOf(el, i + 1) == -1;
});
}
function isListingEmpty(listing) {
return !listing.cn && !listing.dept;
}
// scraping
function get(u, postData, cb) {
if (arguments.length == 2) {
cb = arguments[1];
postData = null;
}
var urlParts = url.parse(u);
var secure = urlParts.protocol == "https:";
var options = {
method: postData ? "POST" : "GET",
host: urlParts.hostname,
port: urlParts.port || (secure ? 443 : 80),
path: urlParts.pathname + (urlParts.search || ''),
headers: postData && {
"Content-Length": postData.length,
"Content-Type": "application/x-www-form-urlencoded",
}
};
var req = (secure ? https : http).request(options, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
cb(data);
});
});
if (postData) {
req.write(postData);
}
req.end();
}
var clusterRe = /(.*?) \((.+?)\)<\/legend><p.*?>(.*?)<\/p>.*?Academic Division:<\/b>(.*?)<.*?Academic Department:<\/b>(.*?)</,
courseRe = /<td>.*?(Query\.aspx.*?)<td>(.*?)<\/td>/g,
listingsRe = /Query\.aspx\?id=DARS&dept=(.*?)&cn=(.*?)'/g;
function sanitizeString(str) {
return str.replace(/\s{3,}/g, ' ')
.replace(/'/g, "'")
.replace(/&/g, "&")
.trim();
}
function getAllClusters(cb) {
console.log('Getting clusters.');
get('https://secure1.rochester.edu/registrar/CSE/searchResults.php',
'Division=ALL&Department=ALL&SearchString=&submit=Search',
function(html) {
if (!html) throw new Error("Failed to get curriculum data.");
parseCurriculum(html, cb);
}
);
}
function parseCluster(section) {
var m = clusterRe.exec(section);
if (!m) throw new Error("Cluster section did not match RE. " +
sections[i]);
var cluster = {
title: sanitizeString(m[1]),
id: m[2],
description: sanitizeString(m[3]),
division: m[4].trim(),
dept: m[5].trim(),
courses: []
};
// Get all the courses in this cluster section
while (m = courseRe.exec(section)) {
var course = {
title: sanitizeString(m[2]),
listings: []
};
if (!course.title ||
course.title == "N/A" ||
course.title.indexOf("<del>") == 0) {
continue;
}
// get and add crosslistings
var n;
while (n = listingsRe.exec(m[1])) {
course.listings.push({
dept: n[1].trim(),
cn: n[2].trim()
});
}
if (course.listings.every(isListingEmpty)) {
continue;
}
cluster.courses.push(course);
}
return cluster;
}
function parseCurriculum(html, cb) {
var clusters = [];
// Split page into sections, one for each cluster.
var sections = html.split("<legend id='clusterInformation'>");
console.log("Got " + sections.length + " clusters.");
for (var i = 1; i < sections.length; i++) {
clusters[i-1] = parseCluster(sections[i]);
}
clusters.sort(function (a, b) {
return b.id > a.id;
});
cb(clusters);
}
function extractCoursesFromClusters(cb) {
var courses = [];
var courseIndexById = {};
var numCourses = 0;
// uniqify courses, including crosslists
function courseToIndex(course) {
var ids = course.listings.map(function (listing) {
return listing.dept + ' ' + listing.cn;
});
// get first index that has already been seen
var i = ids.map(function (id) {
return courseIndexById[id];
}).filter(Boolean)[0];
if (i == null) {
i = courseIndexById[ids[0]] = numCourses++;
courses[i] = course;
course.clusters = [];
}
return i;
}
getAllClusters(function (clusters) {
var clustersNice = clusters.map(function (c, clusterI) {
c.courses.forEach(function (course) {
courses[courseToIndex(course)].clusters.push(clusterI);
});
return {
id: c.id,
title: c.title,
dept: c.dept,
division: c.division,
description: c.description,
courses: c.courses.map(courseToIndex)
};
});
cb({
clusters: clustersNice,
courses: courses
});
});
}
function stringifyByLine(arr) {
return arr.map(JSON.stringify).join(',\n');
}
var args = process.argv;
if (!args[2]) {
process.stdout.write("Usage: node scrape.js [output.json]\n");
return;
}
var max = args[3] || 0;
var output = args[2];
//var output = 'courses-clusters.json';
extractCoursesFromClusters(function (data) {
var niceJSON = [].concat(
'{"clusters":[', stringifyByLine(data.clusters), '],',
'"courses":[', stringifyByLine(data.courses), ']}').join('\n');
fs.writeFile(output, niceJSON, function (err) {
if (err) throw err;
console.log('Courses and clusters have been saved to ' + output + '.');
});
});