-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
627 lines (549 loc) · 20.6 KB
/
index.ts
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Copyright (c) 2024 ILEFA Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import qs from 'qs';
import axios from 'axios';
import moment from 'moment';
import cheerio from 'cheerio';
import tableparse from 'cheerio-tableparser';
import Classrooms from '../classrooms.json';
import CourseMappings from '../courses.json';
import { decode as decodeEntity } from 'html-entities';
import {
CampusType,
Classroom,
CourseMapping,
CoursePayload,
EnrollmentPayload,
ProfessorData,
RateMyProfessorResponse,
RmpReport,
SearchParts,
SectionData,
SectionLocationData,
SectionPayload,
UConnService,
UConnServiceReport,
UConnServiceStatus,
detectCampusBySection,
getCatalogUrl
} from './util';
export * from './util';
export const COURSE_IDENTIFIER = /^[a-zA-Z]{2,4}\d{3,4}(Q|E|W)*$/;
export const SECTION_IDENTIFIER = /^(H|Z|W|N)*\d{2,3}(L|D|X)*$/;
const DEFAULT_PREREQS = 'There are no prerequisites for this course.';
const DEFAULT_DESC = 'There is no description provided for this course.';
const DEFAULT_SEARCH_PARTS = [SearchParts.SECTIONS, SearchParts.PROFESSORS];
/**
* Attempts to retrieve data regarding
* a specific UConn course, and returns
* all sections, metadata, and other related
* data about it.
*
* Using ``useMappings`` as true will only return
* the base course information, and will always
* omit professors and sections from the result.
*
* Do note that if the mapping does not exist,
* it will fallback to querying the catalog.
*
* Also do note that {@link SearchParts.PROFESSORS} is contingent
* upon {@link SearchParts.SECTIONS} being included, so if it is
* not, you will not get professors data.
*
* @param identifier a valid course identifier
* @param campus a valid campus type
* @param useMappings whether or not to use offline mappings first, and if not found then query catalog
* @param include overrides what parts are included in the CoursePayload, omit parameter to include all parts
*/
export const searchCourse = async (identifier: string, campus: CampusType = 'any', useMappings: boolean = false, include: SearchParts[] = DEFAULT_SEARCH_PARTS): Promise<CoursePayload> => {
if (!COURSE_IDENTIFIER.test(identifier))
return null;
let prefix = identifier.split(/[0-9]/)[0].toUpperCase();
let number = identifier.split(/[a-zA-Z]{2,4}/)[1];
if (useMappings) {
let mapping = (CourseMappings as any).find(ent => ent.name === identifier);
if (!mapping) return await searchCourse(identifier, campus, false, []);
let marker = moment().isBefore(new Date().setHours(6))
? moment(new Date().setHours(-6))
: moment(new Date().setHours(0));
return {
name: mapping.name,
grading: mapping.grading,
credits: mapping.credits.toString(),
prereqs: mapping.prerequisites,
lastDataMarker: marker.toDate(),
description: mapping.description,
sections: [],
professors: []
}
}
let target = getCatalogUrl(prefix, number);
let res = await axios
.get(target)
.then(res => res.data)
.catch(_ => null);
if (!res)
return null;
let $ = cheerio.load(res);
tableparse($);
let name = $('.single-course > h3:nth-child(2)')
.text()
.split(/\d{4}(?:Q|E|W)*\.\s/)[1];
let grading = $('.grading-basis')
.text()
.trim()
.split('Grading Basis: ')[1] || 'Graded';
let credits = $('.credits')
.text()
.trim()
.split(' ')[0] || 'Unknown Credits';
let prereqs = $('.prerequisites').text() || DEFAULT_PREREQS;
if (prereqs && prereqs !== DEFAULT_PREREQS) {
let parts = prereqs
.trim()
.split(/Prerequisite(?:s){0,1}\:\s/);
prereqs = parts.length === 1 ? parts[0] : parts[1];
if (prereqs.includes('None.'))
prereqs = DEFAULT_PREREQS;
if (prereqs.includes('Recommended Preparation'))
prereqs = prereqs.split('Recommended Preparation')[0].trim()
}
let lastDataRaw = $('.last-refresh').text() || moment().format('DD-MMM-YY h.mm.ss.[123456] a').toUpperCase();
if (lastDataRaw.includes('.')) lastDataRaw = replaceAll(lastDataRaw, '.', ':');
let lastDataMarker = new Date(lastDataRaw.split(/:\d{6}/).join(''));
let description = $('.description').text() || DEFAULT_DESC;
if (!include.includes(SearchParts.SECTIONS)) return {
name, grading, credits,
prereqs, lastDataMarker,
description,
sections: [],
professors: []
};
let sections: SectionData[] = [];
let data: string[][] = ($('.tablesorter') as any).parsetable();
if (!data[0]) return {
name, grading, credits,
prereqs, lastDataMarker,
description,
sections: [],
professors: []
};
let grad = target.includes('gradcatalog');
let sectionCount = data[0].length - 1;
for (let i = 0; i < sectionCount + 1; i++) {
let internalData = cheerio.load(data[0][i].trim());
let term = data[1][i];
let campus = decodeEntity(data[grad ? 3 : 2][i]);
let mode = decodeEntity(data[grad ? 4 : 3][i]);
let instructor = data[grad ? 5 : 4][i]
.replace(/\ /g, ' ')
.replace(/<br\s*\/*>/g, ' | ')
.split(' | ')
.map(ent => ent
.split(', ')
.reverse()
.join(' '))
.join(' & ');
let section = data[grad ? 6 : 5][i];
let session = data[grad ? 7 : 6][i].split('</a>')[0].split('>')[1];
let schedule = data[grad ? 8 : 7][i];
schedule = schedule.substring(0, schedule.length - 4);
let location: string | any = data[grad ? 10 : 8][i];
let locations: SectionLocationData[] = [];
if (location?.includes('classrooms.uconn.edu')) {
location = cheerio.load(location);
if (!location.html().includes('<br>')) {
let locationPayload: SectionLocationData = { name: location('a').text(), url: location('a').attr('href') };
locations.push(locationPayload);
} else {
location('a').each((_, el) => {
let locationPayload: SectionLocationData = { name: $(el).text(), url: $(el).attr('href') };
locations.push(locationPayload);
});
}
} else {
let locationPayload: SectionLocationData = { name: location };
locations.push(locationPayload);
}
let enrollment = data[9][i];
let enrollmentPayload = {} as any;
let spaces = enrollment.split('<')[0];
let current = spaces.split('/')[0];
let seats = spaces.split('/')[1];
enrollmentPayload.max = seats;
enrollmentPayload.current = current;
enrollmentPayload.full = current >= seats;
enrollmentPayload.waitlist = enrollment.includes('Waitlist Spaces:')
? enrollment.split('Waitlist Spaces: ')[1]
: null;
let notes = data[grad ? 13 : 10][i];
let virtual: SectionData = {
internal: {
termCode: internalData('span.term-code').text(),
classNumber: internalData('span.class-number').text(),
classSection: internalData('span.class-section').text(),
sessionCode: internalData('span.session-code').text(),
},
term,
mode,
campus,
instructor,
section,
session,
schedule,
location: locations
.filter((ent, i) => locations.findIndex(ent2 => ent2.name === ent.name) === i)
.filter(ent => ent.name),
enrollment: enrollmentPayload,
notes
}
if (virtual.campus.toLowerCase() === 'off-campus')
continue;
sections.push(virtual);
}
if (campus !== 'any') {
sections = sections.filter(section =>
section
.campus
.replace(' ', '_')
.toLowerCase() === campus.toLowerCase());
sectionCount = sections.length;
}
let professors: ProfessorData[] = [];
sections = sections.slice(1, sections.length);
if (!include.includes(SearchParts.PROFESSORS)) return {
name, grading, credits,
prereqs, lastDataMarker,
description, sections,
professors: []
}
for (let section of sections) {
let profs = section.instructor.split(' & ');
for (let prof of profs) {
if (professors.some(p => p.name === prof))
continue;
let rmp = await searchRMP(prof);
let teaching = sections
.filter(section => section.instructor.split(' & ').includes(prof))
.sort((a, b) => a.section.localeCompare(b.section));
prof = decodeEntity(replaceAll(prof, '<br>', ' '));
if (!rmp) {
professors.push({
name: prof,
sections: teaching,
rmpIds: []
});
continue;
}
professors.push({
name: prof,
sections: teaching,
rmpIds: rmp.rmpIds
});
}
}
professors = professors.filter(prof => !!prof.name.trim())
return {
name, grading, credits,
prereqs, lastDataMarker,
description, sections,
professors
}
}
/**
* Attempts to retrieve information about
* the given section of a course in the form
* of a SectionData object.
*
* @param identifier the course identifier
* @param section the course section to query
*/
export const searchBySection = async (identifier: string, section: string): Promise<SectionPayload> => {
let res = await searchCourse(identifier, detectCampusBySection(section) || 'any');
if (!res)
return null;
let data = res
.sections
.find(({ section: sec }) => sec.toLowerCase() === section.toLowerCase());
if (!data)
return null;
return {
name: res.name,
grading: res.grading,
credits: res.credits,
prereqs: res.prereqs,
lastDataMarker: res.lastDataMarker,
description: res.description,
section: data
}
}
/**
* Returns all Course Mappings from the `courses.json`
* file in the root of this project.
*/
export const getMappings = (): CourseMapping[] => CourseMappings as any;
/**
* Returns a single CourseMapping object by some attribute.
*
* @param key the key to search on
* @param val the value to search for
*/
export const getMappingByAttribute = <K extends keyof CourseMapping>(key: K, val: CourseMapping[K]): CourseMapping => getMappings().find(ent => ent[key] === val);
/**
* Returns all CourseMappings that match a some attribute.
*
* @param key the key to search on
* @param filter the filtering function to match results
*/
export const getMappingMatches = <K extends keyof CourseMapping>(key: K, filter: (val: CourseMapping[K]) => boolean): CourseMapping[] => getMappings().filter(ent => filter(ent[key]));
/**
* Returns all Classroom Mappings from the `classrooms.json`
* file in the root of this project.
*/
export const getClassrooms = (): Classroom[] => Classrooms as any;
/**
* Returns all classrooms that match a specific building.
*
* @param key the building attribute key to search on
* @param val the value to search for
*/
export const getClassroomsForBuilding = <K extends keyof Classroom['building']>(key: K, val: Classroom['building'][K]): Classroom[] => getClassrooms().filter(ent => ent.building[key] === val);
/**
* Returns a single Classroom object by some attribute.
*
* @param key the key to search on
* @param val the value to search for
*/
export const getClassroomByAttribute = <K extends keyof Classroom>(key: K, val: Classroom[K]): Classroom => getClassrooms().find(ent => ent[key] === val);
/**
* Returns all Classroom objects matching the filtering criteria.
*
* @param key the key to search on
* @param filter the filtering function to match results
*/
export const getClassroomMatches = <K extends keyof Classroom>(key: K, filter: (val: Classroom[K]) => boolean): Classroom[] => getClassrooms().filter(ent => filter(ent[key]));
/**
* Attempts to locate entries on RMP
* for a specified professor.
*
* @param instructor the instructor to search for
* @author Noah Struck <https://github.com/Struck713/eagle>
*/
export const searchRMP = async (instructor: string): Promise<RateMyProfessorResponse> => {
let res = await axios
.post(`https://www.ratemyprofessors.com/graphql`,
{
query: `
query AutocompleteSearchQuery {
autocomplete(query: "${instructor}") {
teachers {
edges {
node {
id
school {
name
id
}
}
}
}
}
}
`
},
{
headers: {
"Authorization": "Basic dGVzdDp0ZXN0"
}
}
)
.then(res => res.data.data)
.catch(_ => null);
if (!res || !res.autocomplete?.teachers?.edges) return {
name: instructor,
rmpIds: []
}
let rmp: string[] = res
.autocomplete
.teachers
.edges
.filter(e => e.node.school.name.toLowerCase().includes('university of connecticut'))
.map(e => e.node.id);
return {
name: instructor,
rmpIds: rmp
}
}
/**
* Attempts to create a report based
* off of RMP data available for a
* specified professor's RMP ID.
*
* @param id the instructor's RMP id
* @author Noah Struck <https://github.com/Struck713/eagle>
*/
export const getRmpReport = async (id: string): Promise<RmpReport> => {
let res = await axios
.post(`https://www.ratemyprofessors.com/graphql`,
{
query: `
query Node {
node(id: "${id}") {
... on Teacher {
avgRating
avgDifficultyRounded
wouldTakeAgainPercent
numRatings
teacherRatingTags {
tagName
}
courseCodes {
courseName
courseCount
}
firstName
lastName
ratings(first: 1000) {
edges {
node {
id
legacyId
class
comment
date
difficultyRating
helpfulRating
clarityRating
thumbsUpTotal
thumbsDownTotal
wouldTakeAgain
attendanceMandatory
grade
isForCredit
isForOnlineClass
ratingTags
}
}
}
}
}
}
`
},
{
headers: {
"Authorization": "Basic dGVzdDp0ZXN0"
}
}
)
.then(res => res.data)
.catch(_ => null);
if (!res) return null;
return { ...res.data.node };
}
/**
* Attempts to query enrollment data from the
* course catalog enrollment API.
*
* Returns an unformatted string of #/# which
* represents the current available seats and
* capacity of the requested class.
*
* @param term the term id of the current term
* @param classNumber the class number for the requested class
* @param section the requested section
*/
export const getRawEnrollment = async (term: string, classNumber: string, section: string): Promise<EnrollmentPayload> => await axios
.post('https://catalog.uconn.edu/wp-content/plugins/uc-courses/soap.php', qs.stringify({
action: 'get_latest_enrollment',
term: term,
classNbr: classNumber,
sessionCode: 1,
classSection: section
}))
.then(res => res.data)
.then(async res => {
if (!res.success)
throw new Error('Request failed');
let seats: string[] = res.data.split('/');
let available = parseInt(seats[0]);
let total = parseInt(seats[1]);
let overfill = available >= total;
return {
course: {
term,
section,
classNumber
},
available,
total,
overfill,
percent: Number((available / total).toFixed(2))
}
})
.catch(_ => null);
/**
* Attempts to lookup service statuses from
* the [UConn IT Status page](https://itstatus.uconn.edu)
* and return them as UConnServiceReport objects.
*
* @param services [optional] the services to lookup
*/
export const getServiceStatus = async (...include: UConnService[]): Promise<UConnServiceReport[]> => {
let data = await axios
.get('https://itstatus.uconn.edu')
.then(res => res.data)
.catch(_ => null);
if (!data)
return null;
if (include.includes(UConnService.UNKNOWN))
include = include.filter(srv => srv !== UConnService.UNKNOWN);
let $ = cheerio.load(data);
let services: UConnServiceReport[] = [];
$('.list-group > li').each(i => {
let selector = `li.list-group-item:nth-child(${i + 1})`;
let name = $(`${selector} > p.box-1200`).text();
let status = determineStatusFromHTML($(selector).html());
services.push({
service: UConnService[replaceAll(name.toUpperCase(), ' ', '_')] || UConnService.UNKNOWN,
status: status,
time: Date.now()
});
});
if (include && include.length)
services = services.filter(srv => include.includes(srv.service));
if (services.some(srv => srv.service === UConnService.UNKNOWN))
services = services.filter(srv => srv.service !== UConnService.UNKNOWN);
return services;
}
const determineStatusFromHTML = (listItemSelector: string) => {
if (listItemSelector.includes('text-success')) return UConnServiceStatus.OPERATIONAL;
if (listItemSelector.includes('text-info')) return UConnServiceStatus.REPORTING;
if (listItemSelector.includes('text-warning')) return UConnServiceStatus.DEGRADED;
if (listItemSelector.includes('text-danger')) return UConnServiceStatus.OUTAGE;
return UConnServiceStatus.UNKNOWN;
}
const replaceAll = (input: string, search: string, replace: string) => {
let copy = String(input);
if (!copy.includes(search)) {
return copy;
}
while (copy.includes(search)) {
copy = copy.replace(search, replace);
}
return copy;
}