Skip to content

Commit

Permalink
fix: 🐛 return parsed departments & terms
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-wang0 committed May 16, 2024
1 parent a131930 commit 28fd0a7
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions services/departments-terms-scraper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { CheerioAPI, load } from "cheerio";
import fetch from "cross-fetch";

async function fetchWebSoc() {
const response = await fetch("https://www.reg.uci.edu/perl/WebSoc");
const body = await response.text();
return load(body);
function formatDepartment(department: string) {
const match = department.match(/^(?<code>.+?) \.(?:\.|\s)+(?<name>.+)$/);
if (match?.groups) {
return {
code: match.groups.code,
name: match.groups.name,
};
}

throw new Error(`Failed to parse department: ${department}`);
}

async function getDepartments(webSocContent: CheerioAPI): Promise<string[]> {
async function getDepartments(webSocContent: CheerioAPI) {
const $ = webSocContent;

const departments: string[] = [];
Expand All @@ -19,10 +25,23 @@ async function getDepartments(webSocContent: CheerioAPI): Promise<string[]> {
}
});

return departments;
return departments.map(formatDepartment);
}

async function getTerms(webSocContent: CheerioAPI): Promise<string[]> {
function formatTerm(term: string) {
const match = term.match(/^(?<year>\d+) {2}(?<term>.+)$/);

if (match?.groups) {
return {
year: match.groups.year,
term: match.groups.term,
};
}

throw new Error(`Failed to parse term: ${term}`);
}

async function getTerms(webSocContent: CheerioAPI) {
const $ = webSocContent;

const terms: string[] = [];
Expand All @@ -31,7 +50,13 @@ async function getTerms(webSocContent: CheerioAPI): Promise<string[]> {
terms.push(termText);
});

return terms;
return terms.map(formatTerm);
}

async function fetchWebSoc() {
const response = await fetch("https://www.reg.uci.edu/perl/WebSoc");
const body = await response.text();
return load(body);
}

async function getDepartmentsTerms() {
Expand Down

0 comments on commit 28fd0a7

Please sign in to comment.