const ENDPOINT = "https://oscar.gatech.edu/pls/bprod/bwckschd.p_disp_detail_sched";
const TERM = "202402";
export async function georgiaTechRegistration(request: Request): Promise<Response> {
const course = new URL(request.url).searchParams.get("courseId")?.toString();
if (course === undefined) return Response.json({ error: "missing courseId" });
const url = new URL(ENDPOINT);
url.searchParams.set("term_in", TERM);
url.searchParams.set("crn_in", course);
const data = await fetch(url);
const text = await data.text();
if (text.includes("No detailed class information found")) {
return Response.json({ error: "course not found" });
}
const tableRegex = (header) =>
new RegExp(
"<th CLASS=\"ddlabel\" scope=\"row\" >"
+ `<SPAN class="fieldlabeltext">${header}</SPAN>`
+ "</th>\n"
+ "(?:<td CLASS=\"dddefault\">(.+?)</td>\n)?".repeat(10),
);
const seats = text.match(tableRegex("Seats"));
const waitlist = text.match(tableRegex("Waitlist Seats"));
const title = text.match(
/<th CLASS="ddlabel" scope="row" >(.+?)<br \/><br \/><\/th>/,
);
return Response.json({
title: title[1],
seats: {
capacity: parseInt(seats[1]),
actual: parseInt(seats[2]),
remaining: parseInt(seats[3]),
},
waitlist: {
capacity: parseInt(waitlist[1]),
actual: parseInt(waitlist[2]),
remaining: parseInt(waitlist[3]),
},
});
}