Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"url": "https://github.com/DSCChula/chula-util/issues"
},
"homepage": "https://github.com/DSCChula/chula-util#readme",
"dependencies": {},
"dependencies": {
"axios": "^0.20.0",
"iconv-lite": "^0.6.2"
},
"scripts": {
"dev": "ts-node-dev --respawn src/index.ts",
"build": "tsc",
Expand Down
44 changes: 43 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFaculty, getFacultyList } from ".";
import { getFaculty, getFacultyList, getSubject, getSubjectList } from ".";

describe("getFacultyList", () => {
it("should return list of faculties containing: code, name_en, name_th", () => {
Expand Down Expand Up @@ -31,3 +31,45 @@ describe("getFaculty", () => {
expect(getFaculty("123")).toBeUndefined();
});
});

describe("getSubjectList", () => {
it(`should return list of subjects containing correct properties`, () => {
const subjectList = getSubjectList();
subjectList.forEach((s) => {
expect(s).toHaveProperty("code");
expect(s).toHaveProperty("abbr");
expect(s).toHaveProperty("facultyCode");
expect(s).toHaveProperty("name");
expect(s).toHaveProperty("isClosed");
expect(s).toHaveProperty("openSemester");
expect(s).toHaveProperty("closeSemester");
});
});
});

describe("getSubject", () => {
it(`should return subject from the subject code specified`, () => {
expect(getSubject("2000501")).toEqual({
code: "2000501",
facultyCode: "20",
abbr: "CON PDG PEACE CONF",
name: {
en: "CONCEPTS AND PARADIGMS IN PEACE AND CONFLICT STUDIES",
th: "มโนทัศน์และกระบวนทัศน์ในการศึกษาสันติภาพและความขัดแย้ง",
},
isClosed: false,
openSemester: "1/2552",
});
expect(getSubject("2110413")).toEqual({
code: "2110413",
facultyCode: "21",
abbr: "COMP SECURITY",
name: {
en: "COMPUTER SECURITY",
th: "ความมั่นคงของคอมพิวเตอร์",
},
isClosed: false,
openSemester: "2/2546",
});
});
});
81 changes: 80 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Faculty, FacultyMap } from "./types";
import { Faculty, FacultyMap, Subject, SubjectMap } from "./types";
import f from "./faculties.json";
import s from "./subjects.json";
import axios from "axios";
import iconv from "iconv-lite";
import fs from "fs";

const faculties = f as FacultyMap;
const subjects = s as SubjectMap;

export const getFacultyList = (): Faculty[] => {
return Object.entries(faculties).map(([code, { name_en, name_th }]) => ({
Expand All @@ -16,3 +21,77 @@ export const getFaculty = (code: string): Faculty | undefined => {
const res = faculties[code];
return { code, ...res };
};

export const getSubjectList = (): Subject[] => {
return Object.entries(subjects).map(
([
code,
{ abbr, facultyCode, name, isClosed, openSemester, closeSemester },
]) => ({
code,
abbr,
facultyCode,
name,
isClosed,
openSemester,
closeSemester,
}),
);
};

export const getSubject = (code: string): Subject | undefined => {
if (!(code in subjects)) return;
const res = subjects[code];
return { code, ...res };
};

export const runGetSubject = async (): Promise<Subject[] | undefined> => {
const facultyList = getFacultyList();
const noSubjectFacultyCodeList = ["56", "58", "99", "01"];
const filteredFacultyList = facultyList.filter((f) => {
return !noSubjectFacultyCodeList.includes(f.code);
});
let results;
const subjectData: SubjectMap = {};
for (const f of filteredFacultyList) {
try {
const response = await axios.get(
`https://www.reg.chula.ac.th/document/courseName${f.code}.txt`,
{
responseType: "arraybuffer",
transformResponse: [
(data) => {
return iconv.decode(Buffer.concat([data]), "TIS-620");
},
],
},
);
const regex = /(\d+)\s+(\d+)\s+((?:\S+\s)+)\s+(\d\/\d+)(?:\s+)?(\d\/\d+)?\n(?:\ +)?([^\n]+)?\n(?:\ +)?([^\n]+)?\n/g;
const regexResults: RegExpMatchArray | null = response.data.match(regex);
if (regexResults === null) throw new Error("regexResult is null");
regexResults.forEach((s) => {
const subject = s.split(regex);
subjectData[subject[2]] = {
facultyCode: f.code,
abbr: subject[3].trim(),
name: {
en: subject[7],
th: subject[6],
},
isClosed: subject[5] !== undefined,
openSemester: subject[4],
closeSemester: subject[5],
};
});
} catch (e) {
throw e;
}
}
const jsonData = JSON.stringify(subjectData);
await fs.writeFile(`subjects.json`, jsonData, function (err: any) {
if (err) {
console.log(err);
}
});
return results;
};
Loading