From b9ed6af77198a3be50d0897ee30e723b39764b24 Mon Sep 17 00:00:00 2001 From: Mikkel RINGAUD Date: Fri, 1 Mar 2024 19:23:46 +0100 Subject: [PATCH] fix(pkg)!: make a more globally working parser - removed `shortName` on `SignaturesSemesterDump` - "UE" is included on id of skill --- packages/library/README.md | 1 - packages/library/src/parser/index.ts | 87 +++++++++++++++------------- packages/library/src/parser/types.ts | 9 +-- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/packages/library/README.md b/packages/library/README.md index 3c50511..377eca2 100644 --- a/packages/library/README.md +++ b/packages/library/README.md @@ -77,7 +77,6 @@ interface SignaturesDump { } interface SignaturesSemesterDump { - shortName: string name: string skills: Array } diff --git a/packages/library/src/parser/index.ts b/packages/library/src/parser/index.ts index b49560a..fdf145e 100644 --- a/packages/library/src/parser/index.ts +++ b/packages/library/src/parser/index.ts @@ -1,4 +1,5 @@ import * as cheerio from "cheerio"; +import utf8 from "../utils/utf8"; import type { SignaturesDump, @@ -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 }; diff --git a/packages/library/src/parser/types.ts b/packages/library/src/parser/types.ts index 18dba59..df0a061 100644 --- a/packages/library/src/parser/types.ts +++ b/packages/library/src/parser/types.ts @@ -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. */ @@ -29,7 +24,7 @@ export interface SignaturesSemesterDump { export interface SignaturesSkillDump { /** * Identifier of the skill. - * @example "1.1" + * @example "UE1.1" */ id: string /**