Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate grade book #72

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
50 changes: 50 additions & 0 deletions examples/gradebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as pronote from "../src";
import { credentials } from "./_credentials";

void async function main() {
const session = pronote.createSessionHandle();
await pronote.loginCredentials(session, {
url: credentials.pronoteURL,
kind: pronote.AccountKind.STUDENT,
username: credentials.username,
password: credentials.password,
deviceUUID: credentials.deviceUUID
});
const tab = session.userResource.tabs.get(pronote.TabLocation.Gradebook);
if (!tab) throw new Error("Cannot retrieve periods for the grades tab, you maybe don't have access to it.");
const selectedPeriod = tab.defaultPeriod!;

console.log("Available periods for this tab ->", tab.periods.map((period) => period.name).join(", "));
console.log("We selected the default period,", selectedPeriod.name, "!\n");

const gradebook = await pronote.gradebook(session, selectedPeriod);

if (!gradebook.available) throw new Error(`The gradebook for "${selectedPeriod.name}" is not available` + (gradebook.message ? ` : ${gradebook.message}`:""));

console.group("--- Subjects ---");

gradebook.subjects.forEach(
(subject) => {
console.group(subject.subject.name, ":", subject.teachers.join(", "));
console.log("Coef:", subject.coef);
console.group("Averages:");
console.log("Student:", subject.averages.student);
console.log("Class overall:", subject.averages.classOverall);
console.log("Max:", subject.averages.max);
console.log("Min:", subject.averages.min);
console.groupEnd(); // Averages
console.group("Assessment" + (subject.assessments.length > 1 ? "s: " : ": "));
subject.assessments.forEach((a) => console.log(a));
console.groupEnd(); // Assessments
console.groupEnd(); // Subject
}
);
console.groupEnd(); // Subjects
console.log(); // Make space between categories

console.group("--- Overall ---");
console.group("Assessment" + (gradebook.overallAssessments.length > 1 ? "s :" : " :"));
gradebook.overallAssessments.forEach((a) => console.log(a.name, ":", a.value));
console.groupEnd(); // Assessments
console.groupEnd(); // Overall
}();
26 changes: 26 additions & 0 deletions src/api/gradebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RequestFN } from "~/core/request-function";
import { encodePeriod } from "~/encoders/period";
import { type Period, type SessionHandle, TabLocation } from "~/models";
import { apiProperties } from "./private/api-properties";

import { decodeGradeBook } from "~/decoders/gradebook";
import { GradeBook } from "~/models/gradebook";

export const gradebook = async (session: SessionHandle, period: Period): Promise<GradeBook> => {
const properties = apiProperties(session);

const periodModified = JSON.parse(JSON.stringify(period));
periodModified.kind = 2; // Why ? Idk, but needed

const request = new RequestFN(session, "PageBulletins", {
[properties.data]: {
classe: {},
eleve: {},
periode: encodePeriod(periodModified)
},
[properties.signature]: { onglet: TabLocation.Gradebook }
});

const response = await request.send();
return await decodeGradeBook(session, period, response.data[properties.data]);
};
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from "./discussions";
export * from "./evaluations";
export * from "./geolocation";
export * from "./gradebook-pdf";
export * from "./gradebook";
export * from "./grades-overview";
export * from "./homepage";
export * from "./instance";
Expand Down
48 changes: 48 additions & 0 deletions src/decoders/gradebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { GradeBook, GradeBookSubject } from "~/models/gradebook";
import { decodeSubject } from "./subject";
import { gradebookPDF } from "~/api";
import { Period, SessionHandle } from "~/models";

/**
* @param gradeBookData response from PageBulletins
*/
export const decodeGradeBook = async (session: SessionHandle, period: Period, gradeBookData: any): Promise<GradeBook> => {
// When bad period is used, the return is `{ data: {}, nom: 'PageBulletins' }` but the session don't expire.
if (Object.keys(gradeBookData).length == 0 || gradeBookData.message)
return { available: false, message: gradeBookData.message ?? undefined };

let overallAssessments: { name: string; value: string; }[] = gradeBookData.ObjetListeAppreciations.V.ListeAppreciations.V.map(
(assessment: any) => {
return { name: assessment.Intitule, value: assessment.L };
}
);
const subjects = (gradeBookData.ListeServices.V as Array<any>).map(
(subjectData) => {
return {
subject: decodeSubject(subjectData.Matiere?.V),
subjectGroup: subjectData.SurMatiere.V,
coef: parseInt(subjectData.Coefficient.V),

averages: {
student: parseFloat(subjectData.MoyenneEleve.V.replace(",", ".")),
classOverall: parseFloat(subjectData.MoyenneClasse.V.replace(",", ".")),
max: parseFloat(subjectData.MoyenneSup.V.replace(",", ".")),
min: parseFloat(subjectData.MoyenneInf.V.replace(",", "."))
},

assessments: (subjectData.ListeAppreciations.V as Array<any>).map((a) => a.L),
teachers: subjectData.ListeElements?.V.map((elem: any) => elem.ListeProfesseurs?.V.map((v: any) => v.L))
?? subjectData.ListeProfesseurs?.V.map((a: any) => a.L)
?? []
} as GradeBookSubject;
}
);

return {
available: true,
overallAssessments,
graph: gradeBookData?.graphe?.replace("\\n", ""),
subjects,
url: await gradebookPDF(session, period)
};
};
41 changes: 41 additions & 0 deletions src/models/gradebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Subject } from "./subject";

export type GradeBook =
| GradeBookNot
| GradeBookOk;

type GradeBookNot = Readonly<{
available: false;
message?: string;
}>;

type GradeBookOk = Readonly<{
available: true;
/**
* The differents assessments like the mentions or the overall rating
*/
overallAssessments: {
name: string;
value: string;
}[];
/**
* graph image as base64 png (with white backgound)
*/
graph?: string;
subjects: GradeBookSubject[];
url: string;
}>;

export type GradeBookSubject = {
subject: Subject;
subjectGroup: string;
coef: number;
averages: {
student: number;
classOverall: number;
max: number;
min: number;
};
assessments: string[];
teachers: string[];
};