forked from Schemetrical/UBCScheduler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
203 lines (187 loc) · 6.43 KB
/
parser.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
/**
* A particular section of a course
* @typedef {Object} Section
* @property {string} status
* @property {string} sectionName
* @property {string} activity
* @property {Time} times
* @property {boolean} subactivities
*/
/**
* Time blocks defined by start and end times in particular days
* @typedef {Object} Time
* @property {number} days
* @property {LocalTime} beginTime
* @property {LocalTime} endTime
*/
/**
* Subactivities of a Lecture such as Laboratory or Tutorial. When there is no Lecture,
* the next available item (Laboratory or Tutorial) is chosen as the main activity and
* the item after it is the subactivity.
* @typedef {Object.<string, Section[]>} Subactivities
*/
/**
* @callback sessionsCallback
* @param {string[]} sessions
*/
/**
* @param {sessionsCallback} completion
*/
function parseSessions(completion) {
function parse(data) {
var sessions = []
var pattern = /sessyr=(\d{4})&sesscd=(\w)/g;
var match = pattern.exec(data)
while (match !== null) {
sessions.push(match[1] + match[2])
match = pattern.exec(data);
}
completion(sessions.sort().reverse())
}
$.ajax({ url: 'https://cors-anywhere.herokuapp.com/https://courses.students.ubc.ca/cs/main', success: parse });
}
/**
* @param {string} campus
* @param {string} year
* @param {string} session
* @param {string} subject
* @param {string} course
* @returns {string}
*/
function urlForCourse(campus, year, session, subject, course) {
return `https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=3&campuscd=${campus}&sessyr=${year}&sesscd=${session}&dept=${subject}&course=${course}`
}
/**
* @callback sectionsCallback
* @param {Section[]} sections
*/
/**
* Parses all sections of a certain course, filters for specific term
* @param {string} campus
* @param {string} year
* @param {string} session
* @param {string} subject
* @param {string} course
* @param {string} term
* @param {sectionsCallback} completion
*/
function parseSections(campus, year, session, subject, course, term, completion) {
function parse(data) {
var sections = []
var parser = new DOMParser();
try {
var doc = $($.parseHTML(data))
var rows = doc.find('.section-summary')[0].children[1].children
for (let row of rows) {
parseRow(row, sections)
}
} catch (err) {
console.log(err)
}
completion(postprocessSections(sections))
}
/**
* @param {*} row
* @param {Section[]} sections
*/
function parseRow(row, sections) {
let items = row.children
let status = $(items[0]).text()
let sectionName = $(items[1]).text().trim()
let activity = $(items[2]).text()
let courseTerm = $(items[3]).text()
// let interval = $(items[4]).text()
let days = parseWeekdays($(items[5]).text())
let beginTime = preprocessTime($(items[6]).text())
let endTime = preprocessTime($(items[7]).text())
// let comments = $(items[8]).text()
if (courseTerm !== term && courseTerm !== "1-2") {
sections.push({ status: status, sectionName: sectionName, activity: activity, times: [] })
return // Ignore terms that do not apply but take note
}
if (sectionName === "") {
sections[sections.length - 1].times.push({
days: days,
beginTime: beginTime,
endTime: endTime
})
return
}
sections.push({
status: status, sectionName: sectionName, activity: activity, times: [{
days: days,
beginTime: beginTime,
endTime: endTime
}]
})
}
/**
* Converts weekday string to mask
* @param {string} weekdayString
* @returns {number}
*/
function parseWeekdays(weekdayString) {
let weekdayMask = Weekday.None
let days = weekdayString.split(" ")
for (day of days) {
if (day === "Mon") weekdayMask += Weekday.Monday
if (day === "Tue") weekdayMask += Weekday.Tuesday
if (day === "Wed") weekdayMask += Weekday.Wednesday
if (day === "Thu") weekdayMask += Weekday.Thursday
if (day === "Fri") weekdayMask += Weekday.Friday
}
return weekdayMask
}
/**
* @param {Section[]} sections
* @returns {Section[]}
*/
function postprocessSections(sections) {
sections = sections.filter(function (section) {
// filter out all sections with no times or waitlist
return (section.times.length > 0 &&
section.activity !== "Waiting List" &&
section.times[0].days != Weekday.None &&
section.times[0].beginTime !== "" &&
section.times[0].endTime !== "")
})
if (sections.length <= 0) return
// take the first item's activity as the main activity, such as "Lecture" or "Laboratory"
let mainActivity = sections[0].activity
newSections = []
for (section of sections) {
if (section.activity === mainActivity) {
section.subactivities = {}
newSections.push(section)
continue
}
var subactivities = newSections[newSections.length - 1].subactivities
if (!(section.activity in subactivities)) {
subactivities[section.activity] = []
}
subactivities[section.activity].push(section)
}
// if there are sections that have no subactivities but the last section has subactivities, then take that. (BIOL 200)
for (section of newSections) {
if (Object.keys(section.subactivities).length === 0) {
section.subactivities = newSections[newSections.length - 1].subactivities
}
}
return newSections
}
/**
* @param {string} time
* @returns {LocalTime}
*/
function preprocessTime(time) {
if (time.length < 4) {
// invalid time
return ""
} else if (time.length == 4) {
return LocalTime.parse("0" + time)
} else {
return LocalTime.parse(time)
}
}
$.ajax({ url: 'https://cors-anywhere.herokuapp.com/' + urlForCourse(campus, year, session, subject, course), success: parse });
}