From 7c510ee2a06598686a4dcfa2dc835af830a18e8b Mon Sep 17 00:00:00 2001 From: Eddy Chen <89349085+ecxyzzy@users.noreply.github.com> Date: Wed, 10 Jan 2024 10:10:45 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20misc=20improvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aponia --- apps/api/src/lib/utils.ts | 8 +- tools/registrar-scraper/src/index.ts | 84 +++++++++---------- .../src/instructor-scraper/index.ts | 12 +-- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/apps/api/src/lib/utils.ts b/apps/api/src/lib/utils.ts index 03415960..af00584c 100644 --- a/apps/api/src/lib/utils.ts +++ b/apps/api/src/lib/utils.ts @@ -72,12 +72,12 @@ export function normalizeCourse(course: PrismaCourse): Course { ...course, courseLevel, instructorHistory: course.instructorHistory, - instructors: course.instructors as unknown as InstructorPreview[], - prerequisiteTree: course.prerequisiteTree as unknown as PrerequisiteTree, + instructors: course.instructors as InstructorPreview[], + prerequisiteTree: course.prerequisiteTree as PrerequisiteTree, prerequisiteList: course.prerequisiteList, prerequisiteFor: course.prerequisiteFor, - prerequisites: course.prerequisites as unknown as CoursePreview[], - dependencies: course.dependencies as unknown as CoursePreview[], + prerequisites: course.prerequisites as CoursePreview[], + dependencies: course.dependencies as CoursePreview[], geList, terms: course.terms, }; diff --git a/tools/registrar-scraper/src/index.ts b/tools/registrar-scraper/src/index.ts index aaaf255f..c13be550 100644 --- a/tools/registrar-scraper/src/index.ts +++ b/tools/registrar-scraper/src/index.ts @@ -56,51 +56,45 @@ async function main() { const instructors = Object.fromEntries( (await prisma.instructor.findMany()).map((x) => [x.ucinetid, x]), ); - const newCourses = []; - for (const course of Object.values(courses)) { - newCourses.push({ - ...course, - prerequisiteTree: course.prerequisiteTree as object, - instructors: course.instructorHistory - .map((x) => instructors[x]) - .filter((x) => x) - .map(({ ucinetid, name, shortenedName }) => ({ ucinetid, name, shortenedName })), - prerequisites: course.prerequisiteList - .map((x) => courses[x.replace(/ /g, "")]) - .filter((x) => x) - .map(({ id, title, department, courseNumber }) => ({ - id, - title, - department, - courseNumber, - })), - dependencies: course.prerequisiteFor - .map((x) => courses[x.replace(/ /g, "")]) - .filter((x) => x) - .map(({ id, title, department, courseNumber }) => ({ - id, - title, - department, - courseNumber, - })), - }); - } - const newInstructors = []; - for (const instructor of Object.values(instructors)) { - newInstructors.push({ - ...instructor, - courseHistory: instructor.courseHistory as object, - courses: Object.keys(instructor.courseHistory!) - .map((x) => courses[x.replace(/ /g, "")]) - .filter((x) => x) - .map(({ id, title, department, courseNumber }) => ({ - id, - title, - department, - courseNumber, - })), - }); - } + const newCourses = Object.values(courses).map((course) => ({ + ...course, + prerequisiteTree: course.prerequisiteTree as object, + instructors: course.instructorHistory + .map((x) => instructors[x]) + .filter((x) => x) + .map(({ ucinetid, name, shortenedName }) => ({ ucinetid, name, shortenedName })), + prerequisites: course.prerequisiteList + .map((x) => courses[x.replace(/ /g, "")]) + .filter((x) => x) + .map(({ id, title, department, courseNumber }) => ({ + id, + title, + department, + courseNumber, + })), + dependencies: course.prerequisiteFor + .map((x) => courses[x.replace(/ /g, "")]) + .filter((x) => x) + .map(({ id, title, department, courseNumber }) => ({ + id, + title, + department, + courseNumber, + })), + })); + const newInstructors = Object.values(instructors).map((instructor) => ({ + ...instructor, + courseHistory: instructor.courseHistory as object, + courses: Object.keys(instructor.courseHistory!) + .map((x) => courses[x.replace(/ /g, "")]) + .filter((x) => x) + .map(({ id, title, department, courseNumber }) => ({ + id, + title, + department, + courseNumber, + })), + })); await prisma.$transaction([ prisma.course.deleteMany({ where: { id: { in: Object.keys(courses) } } }), prisma.instructor.deleteMany({ where: { ucinetid: { in: Object.keys(instructors) } } }), diff --git a/tools/registrar-scraper/src/instructor-scraper/index.ts b/tools/registrar-scraper/src/instructor-scraper/index.ts index 42c407a6..3fd3a90a 100644 --- a/tools/registrar-scraper/src/instructor-scraper/index.ts +++ b/tools/registrar-scraper/src/instructor-scraper/index.ts @@ -25,9 +25,9 @@ type InstructorsData = { log: InstructorsLog; }; -type InstructorsInfo = { - [ucinetid: string]: Omit; -}; +type ScrapedInstructor = Omit; + +type InstructorsInfo = Record; type InstructorsLog = { [key: string]: @@ -139,7 +139,7 @@ export async function getInstructors( } }); }); - const instructorPromises: Promise<[string, Omit]>[] = []; + const instructorPromises: Promise<[string, ScrapedInstructor]>[] = []; Object.keys(instructorsDict).forEach((name) => { const schools = instructorsDict[name].schools; const related_departments = Array.from(instructorsDict[name].courses); @@ -214,9 +214,9 @@ async function getInstructor( relatedDepartments: string[], attempts: number, year_threshold: number, -): Promise<[string, Omit]> { +): Promise<[string, ScrapedInstructor]> { logger.info(`Scraping data for ${instructorName}`); - const instructorObject: Omit = { + const instructorObject: ScrapedInstructor = { name: instructorName, ucinetid: "", title: "",