-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawl.js
299 lines (267 loc) · 11 KB
/
crawl.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const Sequelize = require('sequelize').Sequelize;
const chalk = require('chalk');
const puppeteer = require('puppeteer');
const termID = '202010';
const sequelize = new Sequelize(`sqlite:./${termID}.db`, {
logging: false
});
async function crawl() {
const CRNS = sequelize.define('crns', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
crn: Sequelize.STRING,
subject: Sequelize.STRING,
classTitle: Sequelize.STRING,
classShortName: Sequelize.STRING,
classNumber: Sequelize.STRING,
classSection: Sequelize.INTEGER,
classType: Sequelize.STRING,
isLab: Sequelize.BOOLEAN,
instructor: Sequelize.STRING,
startTime: Sequelize.TIME,
endTime: Sequelize.TIME,
isSunday: Sequelize.BOOLEAN,
isMonday: Sequelize.BOOLEAN,
isTuesday: Sequelize.BOOLEAN,
isWednesday: Sequelize.BOOLEAN,
isThursday: Sequelize.BOOLEAN,
levels: Sequelize.STRING,
attributes: Sequelize.STRING,
credits: Sequelize.INTEGER,
classroom: Sequelize.STRING,
scheduleType: Sequelize.STRING,
seatsAvailable: Sequelize.BOOLEAN
});
const instructors = sequelize.define('instructors', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: Sequelize.STRING,
email: Sequelize.STRING
});
const subjects = sequelize.define('subjects', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
shortName: Sequelize.STRING,
longName: Sequelize.STRING
});
const levels = sequelize.define('levels', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
level: Sequelize.STRING
});
const attributes = sequelize.define('attributes', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
attribute: Sequelize.STRING
});
await sequelize.authenticate();
await sequelize.sync({
force: true
});
//Now that the database has been setup, time to start crawling
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
page.on('error', async (err) => { //For generic errors
console.log(chalk.red(err));
await browser.close();
});
//Open the first page
console.log(chalk.blue('Browser and page launched.'))
await page.goto('https://banner.aus.edu/axp3b21h/owa/bwckschd.p_disp_dyn_sched');
await page.waitForSelector(`option[VALUE="${termID}`, {
timeout: 10000
}).catch(async (err) => {
await browser.close();
});
//Select the semester from the input
await page.select('select', termID);
//Click the submit button
console.log(chalk.blue('Term selected and submitted.'))
await page.waitForSelector('input[type="submit"]');
await page.click('input[type="submit"]').catch(async err => {
await browser.close();
console.log("INPUT TIMEOUT");
});
await page.waitForSelector('select[name="sel_subj"]', {
timeout: 10000
}).catch(async err => {
await browser.close();
});
//Time to fetch the subjects
const subjectFullName = await page.$eval('select[name="sel_subj"]', result => result.innerText.trim().split('\n'));
const subjectShortName = await page.$$eval('select[name="sel_subj"] option', result => result.map((item) => {
return item.value
}));
//Create array for bulk create
let subjectsArr = [];
for (let i = 0; i < subjectFullName.length; i++) {
subjectsArr.push({
'shortName': subjectShortName[i],
'longName': subjectFullName[i]
})
}
//Insert subjects into the database
await subjects.bulkCreate(subjectsArr);
console.log(chalk.blue(`${subjectFullName.length} subjects inserted into the database.`));
//Time to crawl CRNs
await page.select('select[name="sel_subj"]', ...subjectShortName);
// await page.select('select[name="sel_subj"]', 'COE');
await page.waitForSelector('input[type="submit"]').catch(async err => {
await browser.close();
console.log("INPUT TIMEOUT");
});
await page.click('input[type="submit"]');
await page.waitForSelector('td.dddefault').catch(async err => {
await browser.close();
});
await page.waitForSelector('th a').catch(async err => {
await browser.close();
});
await page.waitForSelector('span.releasetext').catch(async err => {
await browser.close();
});
console.log(chalk.blue('CRN Page loaded.'));
const totalResults = await page.$$eval('th a', result => {
let returnedResult = {
crnInfo: [],
instructorInfo: []
};
for (let i = 0; i < result.length; i++) {
let crnTitle = result[i].innerText.split(' - ');
let descriptionElement = result[i].parentElement.parentElement.nextElementSibling;
let descriptionText = descriptionElement.innerText;
let classTable = descriptionElement.querySelector('table');
let info = {
'crn': crnTitle[1],
'subject': crnTitle[2].split(' ')[0],
'classTitle': crnTitle[0],
'classShortName': crnTitle[2],
'classNumber': crnTitle[2].split(' ')[1],
'classSection': crnTitle[3],
'classType': classTable.querySelectorAll('td')[6].innerText,
'isLab': (crnTitle.length === 5 || classTable.querySelectorAll('td')[6].innerText === 'Lab'),
'instructor': classTable.querySelectorAll('td')[7].innerText.split('(P)')[0],
'startTime': new Date(`0, ${classTable.querySelectorAll('td')[1].innerText.split(' - ')[0]}`).toString(),
'endTime': new Date(`0, ${classTable.querySelectorAll('td')[1].innerText.split(' - ')[1]}`).toString(),
'isSunday': false,
'isMonday': false,
'isTuesday': false,
'isWednesday': false,
'isThursday': false,
'levels': descriptionText.match(/(?<=Levels: ).*/g)[0] || null,
'attributes': null,
'scheduleType': descriptionText.match(/.+?(?= Schedule)/g)[0] || null,
'credits': parseInt(descriptionText.match(/.+?(?= Credits)/g)[0]) || null,
'classroom': classTable.querySelectorAll('td')[4].innerText,
'seatsAvailable': (classTable.querySelectorAll('td')[3].innerText === 'Y')
}
//Slight exception for MTH 103
if (crnTitle.length === 5 && crnTitle[1].includes('Lab')) {
info['crn'] = crnTitle[2];
info['subject'] = crnTitle[3].split(' ')[0];
info['classNumber'] = crnTitle[3].split(' ')[1];
info['classTitle'] = `${crnTitle[0]} ${crnTitle[1]}`;
info['classShortName'] = crnTitle[3];
info['classSection'] = crnTitle[4];
} else if (crnTitle[1].includes('Targeted eLipo')) { //Another exception :)
info['crn'] = crnTitle[2];
info['subject'] = crnTitle[3].split(' ')[0];
info['classNumber'] = crnTitle[3].split(' ')[1];
info['classTitle'] = `${crnTitle[0]} ${crnTitle[1]}`;
info['classShortName'] = crnTitle[3];
info['classSection'] = crnTitle[4];
}
if (descriptionText.match(/(?<=Attributes: ).*/g)) {
info['attributes'] = descriptionText.match(/(?<=Attributes: ).*/g)[0]
}
let instructorInfo = {
name: classTable.querySelectorAll('td')[7].innerText.split('(P)')[0].trim(),
email: (info.instructor === 'TBA') ? 'none' : classTable.querySelector('td a').href.split('mailto:')[1].trim()
};
let days = classTable.querySelectorAll('td')[2].innerText;
if (days.includes('U')) {
info['isSunday'] = true;
} else if (days.includes('M')) {
info['isMonday'] = true;
} else if (days.includes('T')) {
info['isTuesday'] = true;
} else if (days.includes('W')) {
info['isWednesday'] = true;
} else if (days.includes('R')) {
info['isThursday'] = true;
}
returnedResult.crnInfo.push(info);
returnedResult.instructorInfo.push(instructorInfo);
}
return returnedResult;
})
for (let i = 0; i < totalResults.crnInfo.length; i++) {
//First, we insert the instructor into the database
let res = await instructors.findOrCreate({
where: {
email: totalResults.instructorInfo[i].email
},
defaults: totalResults.instructorInfo[i]
});
if (res[0]._options.isNewRecord) {
console.log(chalk.magenta(`Inserting instructor ${totalResults.instructorInfo[i].name} [${totalResults.instructorInfo[i].email}]`))
}
//Next, we insert the attributes and levels
if (totalResults.crnInfo[i].attributes) {
let attributesArr = totalResults.crnInfo[i].attributes.split(', ')
for (let j = 0; j < attributesArr.length; j++) {
let res = await attributes.findOrCreate({
where: {
attribute: attributesArr[j]
},
defaults: {
attribute: attributesArr[j]
}
})
if (res[0]._options.isNewRecord) {
console.log(chalk.yellow(`Inserting attribute ${attributesArr[j]}`))
}
}
}
if (totalResults.crnInfo[i].levels) {
let levelsArr = totalResults.crnInfo[i].levels.split(', ')
for (let j = 0; j < levelsArr.length; j++) {
let res = await levels.findOrCreate({
where: {
level: levelsArr[j]
},
defaults: {
level: levelsArr[j]
}
})
if (res[0]._options.isNewRecord) {
console.log(chalk.blue(`Inserting level ${levelsArr[j]}`))
}
}
}
console.log(chalk.green(`Inserting CRN ${totalResults.crnInfo[i].crn} - ${totalResults.crnInfo[i].classTitle} - ${totalResults.crnInfo[i].classShortName}`))
await CRNS.create(totalResults.crnInfo[i]);
}
await browser.close();
}
crawl().catch((err) => {
console.log(err);
console.log(chalk.red('Error! Quitting now...'));
});