Skip to content

Commit

Permalink
fix: 🐛 address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ecxyzzy committed May 19, 2024
1 parent 01f5751 commit d81b2cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions tools/degreeworks-scraper/src/components/DegreeworksClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 10 additions & 7 deletions tools/degreeworks-scraper/src/components/PPAPIOfflineClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ 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<string, Course>();

private constructor() {}

static async new(): Promise<PPAPIOfflineClient> {
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<Course[]> = await res.json();
if (isErrorResponse(json))
throw new Error("Could not fetch courses cache from PeterPortal API");
Expand All @@ -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);
}
}
7 changes: 4 additions & 3 deletions tools/degreeworks-scraper/src/components/Scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,9 +176,8 @@ export class Scraper {
};
}
static async new(authCookie: string): Promise<Scraper> {
const studentId = jwtDecode<JwtPayload>(authCookie.slice("Bearer+".length))?.sub;
if (!studentId || studentId.length !== 8)
throw new Error("Could not parse student ID from auth cookie.");
const studentId = jwtDecode<JwtPayload>(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}`,
Expand Down

0 comments on commit d81b2cc

Please sign in to comment.