This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
forked from benb116/PennCourseSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
347 lines (323 loc) · 14.1 KB
/
parse.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
var parse = {};
var r = require('./reqFunctions.js'); // Functions that help determine which req rules apply to a class
var allRevs = require('./loadRevs.js');
function GetRevData (dept, num, inst) {
// Given a department, course number, and instructor (optional), get back the three rating values
var deptData = allRevs[dept]; // Get dept level ratings
var thisRevData = {"cQ": 0, "cD": 0, "cI": 0};
if (deptData) {
var revData = deptData[num]; // Get course level ratings
if (revData) {
// Try to get instructor specific reviews, but fallback to general course reviews
thisRevData = (revData[(inst || '').trim().toUpperCase()] || (revData.Recent));
}
}
return thisRevData;
}
function getTimeInfo(JSONObj) { // A function to retrieve and format meeting times
var OCStatus = JSONObj.course_status; // Is the section open or closed
var isOpen;
if (OCStatus === "O") {
isOpen = true;
} else {
isOpen = false;
}
var TimeInfo = []; // TimeInfo is textual e.g. '10:00 to 11:00 on MWF'
try { // Not all sections have time info
for(var meeting in JSONObj.meetings) { if (JSONObj.meetings.hasOwnProperty(meeting)) {
// Some sections have multiple meeting forms (I'm looking at you PHYS151)
var thisMeet = JSONObj.meetings[meeting];
var StartTime= thisMeet.start_time.split(" ")[0]; // Get start time
var EndTime = thisMeet.end_time.split(" ")[0]; // Get end time
if (StartTime[0] === '0') {
StartTime = StartTime.slice(1);
} // If it's 08:00, make it 8:00
if (EndTime[0] === '0') {
EndTime = EndTime.slice(1);
}
var MeetDays = thisMeet.meeting_days; // Output like MWF or TR
var meetListInfo = StartTime+" to "+EndTime+" on "+MeetDays;
TimeInfo.push(meetListInfo);
}}
}
catch (err) {
console.log(("Error getting times", JSONObj.section_id));
TimeInfo = '';
}
return [isOpen, TimeInfo];
}
function SectionMeeting(sec) {
var isOpen = (sec.course_status !== 'X') && (Number(sec.course_number) < 600);
var thisInfo = {
'idDashed': sec.course_department + '-' + sec.course_number + '-' + sec.section_number,
'course': sec.course_department + '-' + sec.course_number,
'actType': sec.activity,
'assclec': sec.lectures,
'assclab': sec.labs,
'asscrec': sec.recitations,
'meetblk': [],
'open': isOpen
};
sec.meetings.forEach(function(meeting) {
for (i = 0; i < meeting.meeting_days.length; i++) {
thisInfo.meetblk.push({
'meetday': meeting.meeting_days[i],
'starthr': meeting.start_hour_24 + (meeting.start_minutes)/60,
'endhr': meeting.end_hour_24 + (meeting.end_minutes)/60
});
}
});
return thisInfo;
}
parse.SchedInfo = function(entry) { // Get the properties required to schedule the section
if (entry.result_data) {
entry = entry.result_data;
} else if (!entry[0]) {
entry = [entry];
}
var resJSON = [];
for (var key in entry) { if (entry.hasOwnProperty(key)) {
var idDashed = entry[key].section_id_normalized.replace(/ /g, ""); // Format ID
var idSpaced = idDashed.replace(/-/g, ' ');
try { // Not all sections have time info
for(var meeti in entry[key].meetings) { if (entry[key].meetings.hasOwnProperty(meeti)) { // Some sections have multiple meetings
var thisMeet = entry[key].meetings[meeti];
var StartTime = (thisMeet.start_hour_24) + (thisMeet.start_minutes)/60;
var EndTime = (thisMeet.end_hour_24) + (thisMeet.end_minutes)/60;
var hourLength = EndTime - StartTime;
var MeetDays = thisMeet.meeting_days;
var Building = (thisMeet.building_code || '');
var Room = (thisMeet.room_number || '');
var SchedAsscSecs = [];
if (entry[key].lectures.length) {
SchedAsscSecs = entry[key].lectures.map(function(a) {
return [a.subject, a.course_id, a.section_id].join('-');
});
} else if (entry[key].recitations.length) {
SchedAsscSecs = entry[key].recitations.map(function(a) {
return [a.subject, a.course_id, a.section_id].join('-');
});
} else if (entry[key].labs.length) {
SchedAsscSecs = entry[key].labs.map(function(a) {
return [a.subject, a.course_id, a.section_id].join('-');
});
}
// Full ID will have sectionID+MeetDays+StartTime
// This is necessary for classes like PHYS151, which has times: M@13, TR@9, AND R@18
var FullID = idDashed+'-'+MeetDays+StartTime.toString().replace(".", "");
resJSON.push({
'fullID': FullID,
'idDashed': idDashed,
'idSpaced': idSpaced,
'hourLength': hourLength,
'meetDay': MeetDays,
'meetHour': StartTime,
'meetLoc': Building+' '+Room,
'SchedAsscSecs': SchedAsscSecs
});
}}
}
catch (err) {
console.log("Error getting times: "+err);
}
}}
return resJSON;
};
parse.DeptList = function(res) {
for (var course in res) { if (res.hasOwnProperty(course)) {
var courData = res[course].idSpaced.split(' ');
var courDept = courData[0];
var courNum = courData[1];
res[course].revs = GetRevData(courDept, courNum); // Append PCR data to courses
}}
return res;
};
// This function spits out the array of courses that goes in #CourseList
// Takes in data from the API
parse.CourseList = function(Res) {
var coursesList = {};
for(var key in Res) { if (Res.hasOwnProperty(key)) {
var thisKey = Res[key];
if (!thisKey.is_cancelled) { // Iterate through each course that isn't cancelled
var thisDept = thisKey.course_department.toUpperCase();
var thisNum = thisKey.course_number.toString();
var courseListName = thisDept+' '+thisNum; // Get course dept and number
var numCred = Number(thisKey.credits.split(" ")[0]); // How many credits does this section count for
if (!coursesList[courseListName]) { // If there's no entry, make a new one
var courseTitle = thisKey.course_title;
var reqCodesList = r.GetRequirements(thisKey); // Check which requirements are fulfilled by this course
var revData = GetRevData(thisDept, thisNum); // Get review information
coursesList[courseListName] = {
'idSpaced': courseListName,
'idDashed': courseListName.replace(/ /g,'-'),
'courseTitle': courseTitle,
'courseReqs': reqCodesList[0],
'courseCred': numCred,
'revs': revData
};
} else if (coursesList[courseListName].courseCred < numCred) { // If there is an entry, choose the higher of the two numcred values
coursesList[courseListName].courseCred = numCred;
}
}
}}
var arrResp = [];
for (var course in coursesList) { if (coursesList.hasOwnProperty(course)) {
arrResp.push(coursesList[course]); // Convert from object to array
}}
return arrResp;
};
// This function spits out section-specific info
parse.SectionInfo = function(Res) {
var entry = Res[0];
var sectionInfo = {};
// try {
if (entry && !entry.is_cancelled) { // Don't return cancelled sections
var Title = entry.course_title;
var FullID = entry.section_id_normalized.replace(/-/g, " "); // Format name
var CourseID = entry.section_id_normalized.split('-')[0] + ' ' + entry.section_id_normalized.split('-')[1];
var Instructor = ((entry.instructors[0] && entry.instructors[0].name) || '');
var Desc = entry.course_description;
var TimeInfoArray = getTimeInfo(entry);
var StatusClass = TimeInfoArray[0];
var meetArray = TimeInfoArray[1];
var prereq = (entry.prerequisite_notes[0] || 'none');
var termsOffered = entry.course_terms_offered;
var OpenClose = 'Closed';
if (StatusClass) {
OpenClose = 'Open';
}
var secCred = Number(entry.credits.split(" ")[0]);
var asscType = '';
var asscList = [];
if (entry.lectures.length) {
asscType = 'lecture';
asscList = entry.lectures.map(function(a) {
return [a.subject, a.course_id, a.section_id].join(' ');
});
} else if (entry.recitations.length) {
asscType = 'recitation';
asscList = entry.recitations.map(function(a) {
return [a.subject, a.course_id, a.section_id].join(' ');
});
} else if (entry.labs.length) {
asscType = 'lab';
asscList = entry.labs.map(function(a) {
return [a.subject, a.course_id, a.section_id].join(' ');
});
}
var reqsArray = r.GetRequirements(entry)[1];
sectionInfo = {
'fullID': FullID,
'CourseID': CourseID,
'title': Title,
'instructor': Instructor,
'description': Desc,
'openClose': OpenClose,
'termsOffered': termsOffered,
'prereqs': prereq,
'timeInfo': meetArray,
'associatedType': asscType,
'associatedSections': asscList,
'sectionCred': secCred,
'reqsFilled': reqsArray
};
return sectionInfo;
} else {
return {};
}
};
// This function spits out the list of sections that goes in #SectionList
parse.SectionList = function(Res) {
// Convert to JSON object
var sectionsList = [];
// var courseInfo = {};
for(var key in Res) {
if (Res.hasOwnProperty(key)) {
var thisEntry = Res[key];
if (!thisEntry.is_cancelled) {
var idDashed = thisEntry.section_id_normalized.replace(/ /g, "");
var idSpaced = idDashed.replace(/-/g, ' ');
var timeInfoArray = getTimeInfo(thisEntry); // Get meeting times for a section
var isOpen = timeInfoArray[0];
var timeInfo = (timeInfoArray[1][0] || ''); // Get the first meeting slot
if (timeInfoArray[1][1]) { // Cut off extra text
timeInfo += ' ...';
}
var actType = thisEntry.activity;
var SectionInst = ((thisEntry.instructors[0] && thisEntry.instructors[0].name) || ''); // Get the instructor for this section
var revData = GetRevData(thisEntry.course_department, thisEntry.course_number, SectionInst); // Get inst-specific reviews
var schedInfo = parse.SchedInfo(thisEntry);
sectionsList.push({
'idDashed': idDashed,
'idSpaced': idSpaced,
'isOpen': isOpen,
'timeInfo': timeInfo,
'courseTitle': Res[0].course_title,
'SectionInst': SectionInst,
'actType': actType,
'revs': revData,
'fullSchedInfo': schedInfo
});
}
}
}
var sectionInfo = parse.SectionInfo(Res);
return [sectionsList, sectionInfo];
};
parse.RecordRegistrar = function(inJSON) {
var fs = require('fs');
var resp = {};
var meetresp = {};
var thisKey;
for(var key in inJSON) { if (inJSON.hasOwnProperty(key)) {
// For each section that comes up
// Get course name (e.g. CIS 120)
thisKey = inJSON[key];
var idSpaced = thisKey.course_department + ' ' + thisKey.course_number;
var secID = thisKey.course_department + '-' + thisKey.course_number + '-' + thisKey.section_number;
var numCred = Number(thisKey.credits.split(" ")[0]);
if (!thisKey.is_cancelled) { // Don't include cancelled sections
var idDashed = idSpaced.replace(' ', '-');
if (!resp[idSpaced]) { // If there is no existing record for the course, make a new record
var reqCodesList = r.GetRequirements(thisKey)[0];
resp[idSpaced] = {
'idDashed': idDashed,
'idSpaced': idSpaced,
'courseTitle': thisKey.course_title,
'courseReqs': reqCodesList,
'courseCred': numCred
};
} else if (resp[idSpaced].courseCred < numCred) { // If there is, make the numCred value the max
resp[idSpaced].courseCred = numCred;
}
var meetingInfo = SectionMeeting(thisKey);
meetresp[secID] = meetingInfo;
}
}}
var arrResp = [];
for (key in resp) { if (resp.hasOwnProperty(key)) {
arrResp.push(resp[key]);
}}
if (thisKey) {
var thedept = thisKey.course_department;
var currentTerm = thisKey.term;
// At the end of the list
fs.writeFile('../Data/'+currentTerm+'/'+thedept+'.json', JSON.stringify(arrResp), function (err) {
// Write JSON to file
if (err) {
console.log(thedept+' '+err);
} else {
console.log(('Reg Spit: '+thedept));
}
});
fs.writeFile('../Data/'+currentTerm+'Meet/'+thedept+'.json', JSON.stringify(meetresp), function (err) {
// Write JSON to file
if (err) {
console.log(thedept+' '+err);
} else {
console.log(('Meet Spit: '+thedept));
}
});
}
};
module.exports = parse;