brownieinmotion-georgiatechregistration.web.val.run
November 20, 2023
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
// ported from https://github.com/brownie-in-motion/gt-waitlist-bot
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" });
}
// regex <3 html
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]),
},
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
November 20, 2023