Skip to content

Commit

Permalink
Tons of stuff related to getHomework
Browse files Browse the repository at this point in the history
+ update some responses
  • Loading branch information
a2br committed Dec 26, 2020
1 parent 60ee859 commit e0ca706
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 32 deletions.
40 changes: 32 additions & 8 deletions lib/account_types/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
toISODate,
} from "../functions";
import { APIError } from "../errors";
import { getUpcomingAssignementDates } from "../functions/student/textbook";

export class Student extends Account {
public type: "student" = "student";
Expand All @@ -39,15 +40,38 @@ export class Student extends Account {
this.token = session.token;
}

async getHomework(date: Date | string | string) {
const d = toISODate(date);
const textbook = await getTextbookPage(this.account.id, this.token, d);
if (isFailure(textbook))
throw new APIError(`${textbook.code} | ${textbook.message}`);
async getHomework(
dates: Array<Date | string | number> | (Date | string | number)
) {
// If no date, get range of upcoming dates from EDAPI
if (!dates) {
dates = await getUpcomingAssignementDates(this.account.id, this.token);
}

const homework = textbook.data;
const cleaned = cleanAssignements(homework);
return cleaned;
if (!Array.isArray(dates)) dates = [dates];

const resultsArray = (
await Promise.all(
dates.map(async (date) => {
const d = toISODate(date);
const textbook = await getTextbookPage(
this.account.id,
this.token,
d
);
if (isFailure(textbook))
throw new APIError(`${textbook.code} | ${textbook.message}`);

const homework = textbook.data;
const cleaned = cleanAssignements(homework);
const withWork = cleaned.filter((v) => !!("aFaire" in v));
return withWork;
})
)
)
.flat()
.sort((a, b) => a.date.getTime() - b.date.getTime());
return resultsArray;
}

async getMessages(direction: "received" | "sent" = "received") {}
Expand Down
8 changes: 4 additions & 4 deletions lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { login, getMainAccount } from "./login";
import { toISODate } from "./util";
import { toISODate, expandBase64 } from "./util";

export { login, getMainAccount };
export { toISODate };
export { toISODate, expandBase64 };

//! TEXTBOOK

import { getTextbook, getTextbookPage } from "./student/textbook";
import { getTextbookPage } from "./student/textbook";
import { cleanAssignements } from "./student/textbook";

export { getTextbook, getTextbookPage, cleanAssignements };
export { getTextbookPage, cleanAssignements };

//! MAILBOX

Expand Down
39 changes: 24 additions & 15 deletions lib/functions/student/textbook.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import fetch from "node-fetch";
import { htmlToText } from "html-to-text";

import { _textbookRes, _textbookDateRes } from "../../types";
import { _textbookRes, _textbookDateRes, isFailure } from "../../types";
import { _textbookDateAssignement, assignement } from "../../types";
import { toISODate } from "../util";
import { APIError } from "../../errors";
import { expandBase64 } from "../util";

/**
* @param id Account id
* @param token Auth token
*/
export async function getTextbook(id: number, token: string) {
export async function getUpcomingAssignementDates(id: number, token: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
Expand All @@ -26,7 +27,11 @@ export async function getTextbook(id: number, token: string) {
}
);
let body: _textbookRes = await edRes.json();
return body;
if (isFailure(body)) throw new APIError(`${body.code} | ${body.message}`);

const dates = Object.keys(body.data); // .map((date) => new Date(date));

return dates;
}

/**
Expand Down Expand Up @@ -58,7 +63,7 @@ export function cleanAssignements(data: {
matieres: _textbookDateAssignement[];
}) {
const assignements = data.matieres;
const date = toISODate(data.date); // Should not make any change
const date = new Date(data.date); // Should not make any change
const cleaned: assignement[] = assignements.map((v) => ({
id: v.id,
date: date,
Expand All @@ -68,18 +73,22 @@ export function cleanAssignements(data: {
code: v.codeMatiere,
},
prof: v.nomProf.startsWith(" par ") ? v.nomProf.substr(5) : v.nomProf,
aFaire: v.aFaire
? {
id: v.aFaire.idDevoir,
contenu: expandBase64(v.aFaire.contenu),
donneLe: new Date(v.aFaire.donneLe),
rendreEnLigne: v.aFaire.rendreEnLigne,
effectue: v.aFaire.effectue,
dernierContenuDeSeance: {
contenu: expandBase64(v.aFaire.contenuDeSeance.contenu),
documents: v.aFaire.contenuDeSeance.documents,
},
}
: undefined,
contenuDeSeance: {
idDevoir: v.contenuDeSeance.idDevoir,
contenu: {
original: v.contenuDeSeance.contenu,
html: Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
text: htmlToText(
Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
{
wordwrap: false,
}
),
},
contenu: expandBase64(v.contenuDeSeance.contenu),
documents: v.contenuDeSeance.documents,
},
_raw: v,
Expand Down
12 changes: 12 additions & 0 deletions lib/functions/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { htmlToText } from "html-to-text";

export function toISODate(date: Date | string | number) {
const d = new Date(date);
if (isNaN(d.getTime())) throw new Error("Invalid date");
Expand All @@ -8,3 +10,13 @@ export function toISODate(date: Date | string | number) {
d.getDate().toString().padStart(2, "0"),
].join("-");
}

export function expandBase64(htmlBase64: string) {
return {
original: htmlBase64,
html: Buffer.from(htmlBase64, "base64").toString(),
text: htmlToText(Buffer.from(htmlBase64, "base64").toString(), {
wordwrap: false,
}),
};
}
3 changes: 3 additions & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { expandedBase64 } from "./util";
export { expandedBase64 };

import { _failureRes, isFailure } from "./failureRes";
export { isFailure };

Expand Down
40 changes: 35 additions & 5 deletions lib/types/student/textbook.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { expandedBase64 } from "..";
import { _failureRes } from "../failureRes";

export type assignement = {
id: number;
date: Date;
interro: boolean;
matiere: {
nom: string;
code: string;
};
prof: string;
aFaire?: {
id: number;
contenu: expandedBase64;
donneLe: Date;
rendreEnLigne: boolean;
effectue: boolean;
dernierContenuDeSeance: {
contenu: expandedBase64;
documents: unknown[];
};
};
contenuDeSeance: {
idDevoir: number;
contenu: {
original: string;
html: string;
text: string;
};
contenu: expandedBase64;
documents: unknown[];
};
_raw: _textbookDateAssignement;
Expand Down Expand Up @@ -47,6 +56,27 @@ export type _textbookDateAssignement = {
interrogation: boolean;
blogActif: boolean;
nbJourMaxRenduDevoir: number;
aFaire?: {
idDevoir: number;
/** BASE64 */
contenu: string;
rendreEnLigne: boolean;
donneLe: string;
effectue: boolean;
ressource: string;
ressourceDocuments: unknown[];
documents: unknown[];
elementsProg: unknown[];
liensManuel: unknown[];
documentsRendus: unknown[];
/** The last one */
contenuDeSeance: {
/** BASE64 */
contenu: string;
documents: unknown[];
};
};
/** The one of the day */
contenuDeSeance: {
idDevoir: number;
/** BASE64 */
Expand Down
5 changes: 5 additions & 0 deletions lib/types/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type expandedBase64 = {
original: string;
html: string;
text: string;
};

0 comments on commit e0ca706

Please sign in to comment.