Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
fix(pkg)!: make a more globally working parser
Browse files Browse the repository at this point in the history
- removed `shortName` on `SignaturesSemesterDump`
- "UE" is included on id of skill
  • Loading branch information
Vexcited committed Mar 1, 2024
1 parent 2f14738 commit b9ed6af
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
1 change: 0 additions & 1 deletion packages/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ interface SignaturesDump {
}

interface SignaturesSemesterDump {
shortName: string
name: string
skills: Array<SignaturesSkillDump>
}
Expand Down
87 changes: 48 additions & 39 deletions packages/library/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cheerio from "cheerio";
import utf8 from "../utils/utf8";

import type {
SignaturesDump,
Expand All @@ -21,69 +22,77 @@ export const dumpSignatureResponse = (html: string): SignaturesDump => {

// Parse the table header containing the semester name.
const header = table.find($(`tr[data-tt-id="${tableID}"]`));
const [shortTableName, ...fullTableNameParts] = header.children().first().text().split(" ");
const fullTableName = fullTableNameParts.join(" ");
const semesterName = header.children().first().text().trim();

const skills: SignaturesSkillDump[] = [];
const columns: string[] = [];

table.find($("thead th")).each(function () {
columns.push($(this).text().trim());
});

table.find($("tr.tr_ue")).each(function () {
const ue = $(this);
const children = ue.children();
let element = ue.children();

const fullName = element.first().text();
const fullNameParts = fullName.split(" ");

element = element.last();
const coefficient = parseFloat(element.text());
element = element.prev();
const absences = parseInt(element.text()) || 0;
element = element.prev();
const globalAverage = parseFloat(element.text());

const skill: SignaturesSkillDump = {
id: fullNameParts.shift()!.trim(),
name: fullNameParts.join(" ").trim(),

const skillGlobalData: string[] = [];
for (let iteration = children.first(), i = 0; i < 4; i++) {
skillGlobalData.push(iteration.text());
iteration = iteration.next();
}
globalAverage: isNaN(globalAverage) ? null : globalAverage,
coefficient,
absences,

const [, shortSkillName, ...fullSkillNameParts] = skillGlobalData[0].split(" ");
const modules: SignaturesModuleDump[] = [];
modules: []
};

const tableID = ue.attr("data-tt-id");
table.find($(`tr.tr_module[data-tt-parent-id="${tableID}"]`)).each(function () {
const module = $(this);
const children = module.children();
let element = $(this).children();

const moduleData: string[] = [];
for (let iteration = children.first(), i = 0; i < 4; i++) {
moduleData.push(iteration.text());
iteration = iteration.next();
}
const fullName = element.first().text();
const fullNameParts = fullName.split(" ");

const [shortModuleName, ...fullModuleNameParts] = moduleData[0].split(" ");
const average = parseFloat(moduleData[1]);
element = element.last();
const coefficient = parseFloat(element.text());
element = element.prev();
const absences = parseInt(element.text()) || 0;
element = element.prev();
const average = parseFloat(element.text());

modules.push({
id: shortModuleName,
name: fullModuleNameParts.join(" "),
const module: SignaturesModuleDump = {
id: fullNameParts.shift()!.trim(),
name: utf8(fullNameParts.join(" ").trim()),
average: isNaN(average) ? null : average,
absences: parseInt(moduleData[2]) || 0,
coefficient: parseFloat(moduleData[3])
});
});

const globalAverage = parseFloat(skillGlobalData[1]);
coefficient,
absences
};

skills.push({
id: shortSkillName,
name: fullSkillNameParts.join(" "),
globalAverage: isNaN(globalAverage) ? null : globalAverage,
absences: parseInt(skillGlobalData[2]) || 0,
coefficient: parseFloat(skillGlobalData[3]),
modules
skill.modules.push(module);
});

skills.push(skill);
});

semesters.push({
shortName: shortTableName,
name: fullTableName,
name: semesterName,
skills
});
});

return {
firstName,
familyName,
firstName: utf8(firstName),
familyName: utf8(familyName),
className,
semesters
};
Expand Down
9 changes: 2 additions & 7 deletions packages/library/src/parser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ export interface SignaturesDump {
}

export interface SignaturesSemesterDump {
/** @example "S1" */
shortName: string
/**
* Long name for the semester.
* @example "Semestre 1"
*/
/** @example "S1 Semestre 1" */
name: string

/** Contains the skills dump. */
Expand All @@ -29,7 +24,7 @@ export interface SignaturesSemesterDump {
export interface SignaturesSkillDump {
/**
* Identifier of the skill.
* @example "1.1"
* @example "UE1.1"
*/
id: string
/**
Expand Down

0 comments on commit b9ed6af

Please sign in to comment.