Skip to content

Commit

Permalink
🐛 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmnqdds committed Oct 6, 2024
1 parent 15cc0da commit d049823
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 390 deletions.
133 changes: 18 additions & 115 deletions app/api/result/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,23 @@ import { IMALUUM_RESULT_PAGE } from "@/constants";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
import { parse } from "node-html-parser";

/**
* A helper function to get the result from a single session
* @param {string} sessionQuery
* @param {string} sessionName
* @returns {Result} An object containing the result for a single session
*/
const getResultFromSession = async (
sessionQuery: string,
sessionName: string,
): Promise<Result> => {
const url = `https://imaluum.iium.edu.my/MyAcademic/result${sessionQuery}`;
try {
const response = await fetch(url, {
headers: {
Cookie: cookies().toString(),
},
// https: { rejectUnauthorized: false },
// followRedirect: false,
});

if (!response.ok) {
throw new Error("Failed to fetch schedule");
}

const body = await response.text();

const root = parse(body);

const table = root.querySelector(".box-body table.table.table-hover");
const rows = table?.querySelectorAll("tr");

if (!rows) throw new Error("Failed to fetch schedule");

const result = [];

const tds = rows[rows.length - 1].querySelectorAll("td");

if (
tds[0].textContent.trim() ===
"Please contact finance division regarding tuition fees"
) {
for (const row of rows) {
const tds = row.querySelectorAll("td");

// Check if tds array has enough elements
if (tds.length >= 4) {
const courseCode = tds[0].textContent.trim();
if (courseCode.split(/\s{2,}/)[0] === "Total Credit Points") {
break;
}
const courseName = tds[1].textContent.trim();
const courseGrade = tds[2].textContent.trim() || "N/A";
const courseCredit = tds[3].textContent.trim();
result.push({
courseCode,
courseName,
courseGrade,
courseCredit,
});
}
}
return {
sessionQuery,
sessionName,
result,
gpaValue: "N/A",
cgpaValue: "N/A",
status: "N/A",
remarks: "Please contact finance division regarding tuition fees",
};
}

const neutralized1 = tds[1].textContent.trim().split(/\s{2,}/) || [];
const gpaValue = neutralized1[2];
const status = neutralized1[3];
const remarks = neutralized1[4];

const neutralized2 = tds[3].textContent.trim().split(/\s{2,}/) || [];
const cgpaValue = neutralized2[2];

// Remove the last row
rows.pop();

for (const row of rows) {
const tds = row.querySelectorAll("td");

// Check if tds array has enough elements
if (tds.length >= 4) {
const courseCode = tds[0].textContent.trim();
const courseName = tds[1].textContent.trim();
const courseGrade = tds[2].textContent.trim() || "N/A";
const courseCredit = tds[3].textContent.trim();
result.push({ courseCode, courseName, courseGrade, courseCredit });
}
}

return {
sessionQuery,
sessionName,
result,
gpaValue,
cgpaValue,
status,
remarks,
};
} catch (err) {
console.log("err", err);
throw new Error("Failed to fetch schedule");
}
};
import { getResultFromSession } from "@/lib/server/result";

export async function GET() {
const _cookies = cookies().toString();

try {
const response = await fetch(IMALUUM_RESULT_PAGE, {
headers: {
Cookie: cookies().toString(),
Cookie: _cookies,
},
});

if (!response.ok) {
throw new Error("Failed to fetch result");
return NextResponse.json(
{ error: "Failed to fetch result" },
{ status: 500 },
);
}

const body = await response.text();
Expand Down Expand Up @@ -161,7 +56,11 @@ export async function GET() {

const results: Result[] = await Promise.all(
sessionList.map(({ sessionQuery, sessionName }) =>
getResultFromSession(sessionQuery as string, sessionName as string),
getResultFromSession(
sessionQuery as string,
sessionName as string,
_cookies,
),
),
);

Expand All @@ -174,6 +73,10 @@ export async function GET() {
);
} catch (error) {
console.error("Error fetching data:", error);
throw new Error("Failed to fetch data");
// throw new Error("Failed to fetch data");
return NextResponse.json(
{ error: "Failed to fetch data" },
{ status: 500 },
);
}
}
Loading

0 comments on commit d049823

Please sign in to comment.