-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduleScraper.js
414 lines (393 loc) · 17.1 KB
/
ScheduleScraper.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
(function() {
var getLoadingPage = function() {
var deferred = jQuery.Deferred();
jQuery('body').data('f57b7ad2ab284e388323484708a031f7', deferred)
jQuery.ajax('https://raw.githack.com/Shadowen/ScheduleScraper/master/LoadingPage.js', {
success: function() {
console.log("Loading page retrieved.")
},
error: function() {
console.log("Loading page failed thrown");
deferred.reject("Ajax error while trying to find loading page.");
}
});
return deferred.promise();
}
// Retrieves the session information from the current page
var getSession = function() {
console.log('Getting session...');
var session = jQuery('div.session-info>span:contains("Session")')
.next()
.contents()
.filter(function() {
return this.nodeType == 3;
})
.text()
// A very aggressive .trim() function
.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
console.log('Detected session "' + session + '"');
return session;
}
var getTimestamp = function() {
console.log('Getting timestamp...');
var timestamp = jQuery('td.section>p.note.skipprint').text().trim()
console.log('Got timestamp! "' + timestamp + '"');
return timestamp;
}
var parseTimetable = function(session) {
console.log('Parsing timetable...');
var parseCourse = function(meetingInfo, dayNum, isPastNoon) {
var formatTime = function(times, isPastNoon) {
timeSplit = times.split('-');
var startTimeSplit = timeSplit[0].split(':');
var endTimeSplit = timeSplit[1].split(':');
var startHour = parseInt(startTimeSplit[0]);
var endHour = parseInt(endTimeSplit[0])
if (isPastNoon) {
startHour += 12;
endHour += 12;
} else if (startHour >= endHour) {
endHour += 12;
}
var startString = ('0' + startHour.toString()).slice(-2) + startTimeSplit[1];
var endString = ('0' + endHour.toString()).slice(-2) + endTimeSplit[1];
return [startString, endString];
}
var course = {};
course.name = meetingInfo.find('.courseCodeInfo').attr('title');
course.code = meetingInfo.find('.courseCodeInfo').text().trim();
if (course.code.slice(-1) == '*') {
course.isBiweekly = true;
course.code = course.code.slice(0, -2);
} else {
course.isBiweekly = false;
}
course.session = course.code.slice(-1);
var year = session.split(" ")[0];
if (session.indexOf("Fall") != -1 && (course.session == 'F' || course.session == 'Y')) {
course.startDate = new Date(year, 8, 1);
} else if (session.indexOf("Winter") != -1 && (course.session == 'S' || course.session == 'Y')) {
course.startDate = new Date(year, 0, 1);
} else {
console.error('Session code \'' + course.session + '\' not recognized for \'' + course.code + '\'');
}
course.day = dayNum;
course.meeting = meetingInfo.contents(".courseSection").text();
var times = formatTime(meetingInfo.children('.meetInfo').text(), isPastNoon);
course.startTime = times[0];
course.endTime = times[1];
course.room = meetingInfo.children(".roomInfo").text();
return course;
}
// A list of courses detected
var schedule = [];
// Total number of columns in the table
var numColumns = 0;
// Keeps track of how many multi-column days are eating up columns invisibly
// Each element represents the column on which each day starts
var dayColumnStart = [0];
// Keeps track of how many multi-row courses are eating up rows invisibly
var colOffsets = [];
var isPastNoon = false;
jQuery('table.timetableSchedule>tbody>tr').each(function(i, r) {
// The first row (header with day-of-week info)
if (i == 0) {
jQuery(this).children('th').each(function(i, r) {
var prevColumnStart = dayColumnStart[i];
var thisColumnSpan = +(jQuery(this).attr('colspan') || 1);
numColumns += thisColumnSpan;
dayColumnStart.push(prevColumnStart + thisColumnSpan);
colOffsets.push(0);
});
return true;
}
var tdTags = jQuery(this).contents().filter(function() {
// http://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery
return this.nodeType == 1 || this.nodeType == 8;
});
var dayNum = -1;
for (var columnNum = 0;; columnNum++) {
// If the column is a new day
if (dayColumnStart.indexOf(columnNum) != -1) {
dayNum++;
// End of a week
if (dayNum > 5) {
break;
}
}
var slotTag = tdTags.eq(columnNum);
// If the slot is taken up by a multi-row course from before, skip it
if (colOffsets[columnNum] > 0) {
colOffsets[columnNum]--;
continue;
} else if (slotTag.is(function() {
return this.nodeType == 8;
})) {
colOffsets[columnNum]--;
continue;
}
// Skip empty classes
if (slotTag.hasClass("timeOfDay")) {
// Detect when we cross noon
if (slotTag.text() == "1:00") {
isPastNoon = true;
}
continue;
} else if (slotTag.hasClass("emptySlot")) {
continue;
}
// Parse valid course slots
var meetingInfo = slotTag.children('.meetingInfo');
schedule.push(parseCourse(meetingInfo, dayNum, isPastNoon));
// Multi-row courses
if (typeof(slotTag.attr('rowspan')) != undefined) {
// Wide courses, fill up the rectangle
var rowSpan = +(parseInt(slotTag.attr('rowspan')) || 1);
for (var c = 0; c < +(slotTag.attr('colspan') || 1); c++) {
colOffsets[columnNum + c] = rowSpan;
}
colOffsets[columnNum] -= 1;
}
}
});
console.log('Timetable parsed! ' + schedule.length + ' timeslots detected.');
return schedule;
}
var getMasterTimetable = function(session) {
console.log('Requesting master timetable for ' + session + '...');
var deferred = jQuery.Deferred();
var successCallback = function(response, reason, obj) {
var numCourses = 0;
for (var key in response) {
numCourses++;
}
console.log('Master timetable found! ' + numCourses + ' courses retrieved.');
deferred.resolve(response);
};
var errorCallback = function(jqXHR, textStatus, errorThrown) {
deferred.reject(textStatus, errorThrown);
}
var url;
if (session.indexOf('Fall') != -1) {
url = 'https://raw.githack.com/Shadowen/ScheduleScraper/master/timetable-fall.js';
} else if (session.indexOf('Winter') != -1) {
url = 'https://raw.githack.com/Shadowen/ScheduleScraper/master/timetable-winter.js';
} else {
console.log("Invalid session code thrown!");
deferred.reject("Invalid session code!");
}
jQuery.ajax({
dataType: "jsonp",
url: url,
jsonpCallback: 'c311745ae7ee4925b17eb440fd06a31d',
success: successCallback,
error: errorCallback
});
return deferred.promise();
}
var decorateWithExtra = function(schedule, master) {
var deferred = jQuery.Deferred();
console.log("Starting decorations...");
for (var i = 0; i < schedule.length; i++) {
// Extract some information and format it the way it is done in the master timetable
var course = schedule[i];
var code = course.code.replace(/[ ]/g, '');
var section = course.meeting.replace(/[ ]/g, '');
var day = course.day;
var startTime = course.startTime;
var endTime = course.endTime;
var rooms = course.room.replace(/[ ]/g, '').split('/');
var courseFound = false;
// Try all the rooms it could be stored under
for (var r = 0; r < rooms.length; r++) {
var room = rooms[r];
if (master[code] && master[code][section] && master[code][section][day + startTime + endTime + room]) {
courseFound = true;
course.startDate = new Date(master[code][section][day + startTime + endTime + room][0].startDate);
course.professors = master[code][section][day + startTime + endTime + room][0].professors;
course.notes = master[code][section][day + startTime + endTime + room][0].notes;
}
}
// The course was not found in the master timetable
if (!courseFound) {
console.log("Course " + code + ":" + section + "@" + (day + startTime + endTime + room) + " not found in master timetable.")
course.professors = [];
course.notes = "";
}
}
deferred.resolve(schedule);
console.log("Decorations successful!");
return deferred.promise();
}
var generateICS = function(schedule) {
console.log('Generating .ics file...');
var calculateNextDay = function(startDate, dayOfWeek) {
var date = new Date(startDate);
while (date.getDay() != dayOfWeek) {
date.setDate(date.getDate() + 1);
}
return date;
}
var dayToString = function(day) {
switch (day) {
case 1:
return 'MO';
case 2:
return 'TU';
case 3:
return 'WE';
case 4:
return 'TH';
case 5:
return 'FR';
default:
console.error('Error parsing day: ' + day);
return "ERR";
}
}
var formatMeeting = function(meetingCode) {
switch (meetingCode.split(' ')[0]) {
case "LEC":
return "Lecture";
case "TUT":
return "Tutorial";
case "PRA":
return "Lab"
default:
console.error("Error parsing meeting code " + meetingCode);
return "Error";
}
}
// Generate .ics
var icsString;
var today = new Date();
icsString = 'BEGIN:VCALENDAR\n';
icsString += 'PRODID:Wesley Inc.\n';
icsString += 'CALSCALE:GREGORIAN\n';
icsString += 'METHOD:PUBLISH\n';
for (var c = 0; c < schedule.length; c++) {
var course = schedule[c];
var dateString = course.startDate.getFullYear().toString() +
('0' + (course.startDate.getMonth() + 1)).slice(-2).toString() +
('0' + calculateNextDay(course.startDate, course.day).getDate()).slice(-2);
icsString += 'BEGIN:VEVENT\n';
icsString += 'DTSTART:' + dateString + 'T' + course.startTime + '00\n';
icsString += 'DTEND:' + dateString + 'T' + course.endTime + '00\n';
icsString += 'UID:' + (today.getTime() + c) + '@heungs.com\n';
icsString += 'LOCATION:' + course.room + '\n';
icsString += 'SUMMARY:' + (course.name || course.code) + ' ' + formatMeeting(course.meeting) + '\n';
icsString += 'DESCRIPTION:Course code: ' + course.code + '\\nSection: ' + course.meeting + '\\n';
if (course.professors) {
icsString += 'Professors: ' + course.professors.reduce(function(prev, cur, index) {
return prev + (index == 0 ? '' : ', ') + cur.first + ' ' + cur.last;
}, '') + '\\n';
}
if (course.notes) {
icsString += 'Additional Notes: ' + course.notes;
}
icsString += '\n';
icsString += 'RRULE:FREQ=WEEKLY;' + (course.isBiweekly ? 'INTERVAL=2;' : '') + 'BYDAY=' + dayToString(course.day) + ';COUNT=' + (course.isBiweekly ? '7' : '14') + '\n';
icsString += 'END:VEVENT\n';
}
icsString += 'END:VCALENDAR\n';
console.log('.ics file generated!');
return icsString;
}
var createDownloadButton = function(icsString) {
console.log('Creating download button...');
// Create download link
var textFileAsBlob = new Blob([icsString], {
type: 'text/plain'
});
var fileNameToSaveAs = "scheduleScraper_export.ics";
var downloadLink = jQuery('#download-link');
if (downloadLink.length == 0) {
downloadLink = jQuery(document.createElement('a'));
}
downloadLink.html("Download as .ics")
.attr('id', 'download-link')
.attr('download', fileNameToSaveAs)
.attr('href', window.URL.createObjectURL(textFileAsBlob))
// Copy the styling from the rest of the site.
// Can't use classes because the app specifically serves css for each page
.css('color', '#fff')
.css('font-weight', 400)
.css('background', '#002a5c')
.css('border-radius', '5px')
.css('padding', '6px 12px')
.css('margin', '')
.css('width', 'auto')
.css('font-size', '13px')
.css('text-decoration', 'none')
.hover(function() {
jQuery(this).css('background-color', '#003E8D');
// jQuery(this).css('text-decoration', 'none');
}, function() {
jQuery(this).css('background-color', '#002a5c');
})
// Insert in proper location
.insertBefore('table.timetableSchedule');
console.log('Download button created!');
return downloadLink;
}
////////// Main program
console.log("Script started...");
// Actual things
var run = function() {
jQuery.noConflict();
console.log('jQuery ' + jQuery.fn.jquery + ' loaded.');
// Make a promise
jQuery.when(
// Loading screen
getLoadingPage()
.then(function() {
console.log('Loading page complete!');
}),
// Other
jQuery.when(getSession())
.then(function(session) {
return jQuery.when(parseTimetable(session), getMasterTimetable(session));
})
.then(decorateWithExtra)
// Generate the .ics
.then(generateICS)
// Create a download button
.then(createDownloadButton)
)
// Programatically click the button to start the download
.then(function(loadingPage, button) {
console.log('done!');
button[0].click()
});
};
// Detect the page
var acornURL = 'https://acorn.utoronto.ca/sws/timetable/main.do?main.dispatch#/calendar';
if (location.href.indexOf(acornURL) != -1) {
console.log('ACORN detected');
// Load jQuery
var importScript = (function(oHead) {
function loadError(oError) {
throw new URIError("The script " + oError.target.src + " is not accessible.");
}
return function(sSrc, fOnload) {
var oScript = document.createElement("script");
oScript.type = "text\/javascript";
oScript.onerror = loadError;
if (fOnload) {
oScript.onload = fOnload;
}
oHead.appendChild(oScript);
oScript.src = sSrc;
}
})(document.head || document.getElementsByTagName("head")[0]);
importScript('https://code.jquery.com/jquery-2.1.4.min.js', run)
} else {
if (window.confirm("Please run this script on the page where you can see your timetable!\n" +
"I can 't hack into your ACORN account to grab your schedule for you...\n" +
"Click OK to go there now.\n Click Cancel to stay here.")) {
window.location.href = acornURL;
} else {
console.log("Script not run.")
}
}
})();