diff --git a/tools/degreeworks-scraper/src/components/DegreeworksClient.ts b/tools/degreeworks-scraper/src/components/DegreeworksClient.ts index d828fd77..6f475885 100644 --- a/tools/degreeworks-scraper/src/components/DegreeworksClient.ts +++ b/tools/degreeworks-scraper/src/components/DegreeworksClient.ts @@ -28,10 +28,10 @@ export class DegreeworksClient { * as the catalog year. Otherwise, we use the former. */ const currentYear = new Date().getUTCFullYear(); - dw.catalogYear = `${currentYear}${currentYear + 1}`; - if (!(await dw.getMajorAudit("BS", "U", "201"))) { - dw.catalogYear = `${currentYear - 1}${currentYear}`; - } + const dataThisYear = await dw.getMajorAudit("BS", "U", "201"); + dw.catalogYear = dataThisYear + ? `${currentYear}${currentYear + 1}` + : `${currentYear - 1}${currentYear}`; console.log(`[DegreeworksClient.new] Set catalogYear to ${dw.catalogYear}`); return dw; } diff --git a/tools/degreeworks-scraper/src/components/PPAPIOfflineClient.ts b/tools/degreeworks-scraper/src/components/PPAPIOfflineClient.ts index 22fc065b..a1c9729a 100644 --- a/tools/degreeworks-scraper/src/components/PPAPIOfflineClient.ts +++ b/tools/degreeworks-scraper/src/components/PPAPIOfflineClient.ts @@ -2,6 +2,8 @@ import { isErrorResponse } from "@peterportal-api/types"; import type { Course, RawResponse } from "@peterportal-api/types"; import fetch from "cross-fetch"; +const ENDPOINT = "https://api-next.peterportal.org/v1/rest/courses/all"; + export class PPAPIOfflineClient { private cache = new Map(); @@ -9,9 +11,7 @@ export class PPAPIOfflineClient { static async new(): Promise { const ppapi = new PPAPIOfflineClient(); - const res = await fetch("https://api-next.peterportal.org/v1/rest/courses/all", { - headers: { "accept-encoding": "gzip" }, - }); + const res = await fetch(ENDPOINT, { headers: { "accept-encoding": "gzip" } }); const json: RawResponse = await res.json(); if (isErrorResponse(json)) throw new Error("Could not fetch courses cache from PeterPortal API"); @@ -22,13 +22,16 @@ export class PPAPIOfflineClient { return ppapi; } - getCourse = (courseNumber: string): Course | undefined => this.cache.get(courseNumber); + getCourse(courseNumber: string): Course | undefined { + return this.cache.get(courseNumber); + } - getCoursesByDepartment = ( + getCoursesByDepartment( department: string, predicate: (x: Course) => boolean = () => true, - ): Course[] => - Array.from(this.cache.values()) + ): Course[] { + return Array.from(this.cache.values()) .filter((x) => x.id.startsWith(department)) .filter(predicate); + } } diff --git a/tools/degreeworks-scraper/src/components/Scraper.ts b/tools/degreeworks-scraper/src/components/Scraper.ts index 6493e996..bef2a812 100644 --- a/tools/degreeworks-scraper/src/components/Scraper.ts +++ b/tools/degreeworks-scraper/src/components/Scraper.ts @@ -5,6 +5,8 @@ import type { Program } from "../types"; import { AuditParser, DegreeworksClient } from "."; +const JWT_HEADER_PREFIX_LENGTH = 7; + export class Scraper { private ap!: AuditParser; private dw!: DegreeworksClient; @@ -174,9 +176,8 @@ export class Scraper { }; } static async new(authCookie: string): Promise { - const studentId = jwtDecode(authCookie.slice("Bearer+".length))?.sub; - if (!studentId || studentId.length !== 8) - throw new Error("Could not parse student ID from auth cookie."); + const studentId = jwtDecode(authCookie.slice(JWT_HEADER_PREFIX_LENGTH))?.sub; + if (studentId?.length !== 8) throw new Error("Could not parse student ID from auth cookie."); const headers = { "Content-Type": "application/json", Cookie: `X-AUTH-TOKEN=${authCookie}`,