2022-01-31 22:32:29 +00:00
|
|
|
import Undici from "undici";
|
|
|
|
|
import { RequestOptions } from "undici/types/dispatcher";
|
2021-10-28 20:40:04 +01:00
|
|
|
import {
|
2022-01-31 22:32:29 +00:00
|
|
|
ActivityResponse,
|
2022-02-06 15:45:28 +00:00
|
|
|
AnnouncementsResponse,
|
2022-01-31 23:13:05 +00:00
|
|
|
BadgesResponse,
|
2022-01-31 22:32:29 +00:00
|
|
|
BehaviourResponse,
|
2022-02-06 10:32:35 +00:00
|
|
|
DetentionsResponse,
|
2022-01-31 22:32:29 +00:00
|
|
|
GetActivityOptions,
|
|
|
|
|
GetBehaviourOptions,
|
|
|
|
|
GetHomeworkOptions,
|
|
|
|
|
GetLessonsOptions,
|
|
|
|
|
Homework,
|
|
|
|
|
HomeworksResponse,
|
|
|
|
|
LessonsResponse,
|
|
|
|
|
Student,
|
|
|
|
|
} from "./types";
|
|
|
|
|
import { API_BASE, BASE_URL } from "./consts";
|
2021-10-29 15:46:33 +01:00
|
|
|
/**
|
|
|
|
|
* The base client
|
|
|
|
|
*/
|
2021-10-28 16:51:07 +01:00
|
|
|
export class ClasschartsClient {
|
2022-01-31 22:32:29 +00:00
|
|
|
public studentCode = "";
|
|
|
|
|
public dateOfBirth = "";
|
|
|
|
|
public studentId = 0;
|
|
|
|
|
public studentName = "";
|
|
|
|
|
private authCookies: Array<string> | undefined;
|
|
|
|
|
private sessionId = "";
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param studentCode Classcharts student code
|
|
|
|
|
* @param dateOfBirth Student's date of birth
|
|
|
|
|
*/
|
|
|
|
|
constructor(studentCode: string, dateOfBirth?: string) {
|
|
|
|
|
this.studentCode = String(studentCode);
|
|
|
|
|
this.dateOfBirth = String(dateOfBirth);
|
|
|
|
|
}
|
|
|
|
|
public async makeAuthedRequest(
|
|
|
|
|
path: string,
|
|
|
|
|
options: Omit<RequestOptions, "origin" | "path">
|
|
|
|
|
) {
|
|
|
|
|
if (!this.authCookies) throw new Error("Not authenticated");
|
|
|
|
|
const requestOptions: Omit<RequestOptions, "origin" | "path"> = {
|
|
|
|
|
...options,
|
|
|
|
|
headers: {
|
|
|
|
|
Cookie: this.authCookies.join(";"),
|
|
|
|
|
authorization: "Basic " + this.sessionId,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const request = await Undici.request(path, requestOptions);
|
|
|
|
|
let responseJSON;
|
|
|
|
|
try {
|
|
|
|
|
responseJSON = await request.body.json();
|
2022-02-11 23:46:48 +00:00
|
|
|
} catch {
|
|
|
|
|
throw new Error("Invalid JSON response recieved");
|
2022-01-31 22:32:29 +00:00
|
|
|
}
|
|
|
|
|
if (responseJSON.success == 0) {
|
|
|
|
|
throw new Error(responseJSON.error);
|
|
|
|
|
}
|
|
|
|
|
return responseJSON.data;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Initialises the client and authenticates with classcharts
|
|
|
|
|
*/
|
2022-01-31 23:13:05 +00:00
|
|
|
async login(): Promise<void> {
|
2022-01-31 22:32:29 +00:00
|
|
|
if (!this.studentCode) throw new Error("Student Code not inputted");
|
|
|
|
|
const formData = new URLSearchParams();
|
|
|
|
|
formData.append("_method", "POST");
|
|
|
|
|
formData.append("code", this.studentCode.toUpperCase());
|
|
|
|
|
formData.append("dob", this.dateOfBirth);
|
|
|
|
|
formData.append("remember_me", "1");
|
|
|
|
|
formData.append("recaptcha-token", "no-token-avaliable");
|
|
|
|
|
const request = await Undici.request(BASE_URL + "/student/login", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData.toString(),
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (request.statusCode != 302 || !request.headers["set-cookie"])
|
|
|
|
|
throw new Error("Unauthenticated: Classcharts returned an error");
|
2022-01-31 22:38:17 +00:00
|
|
|
const cookies = request.headers["set-cookie"];
|
2022-01-31 22:32:29 +00:00
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
|
cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";"));
|
|
|
|
|
}
|
2022-01-31 23:13:05 +00:00
|
|
|
this.authCookies = cookies;
|
2022-01-31 22:38:17 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-01-31 22:32:29 +00:00
|
|
|
let sessionID: any = decodeURI(cookies[2])
|
|
|
|
|
.replace(/%3A/g, ":")
|
|
|
|
|
.replace(/%2C/g, ",");
|
|
|
|
|
sessionID = JSON.parse(
|
|
|
|
|
sessionID.substring(sessionID.indexOf("{"), sessionID.length)
|
|
|
|
|
);
|
|
|
|
|
this.sessionId = sessionID.session_id;
|
|
|
|
|
const user = await this.getStudentInfo();
|
|
|
|
|
this.studentId = user.id;
|
|
|
|
|
this.studentName = user.name;
|
|
|
|
|
}
|
2022-02-06 10:16:42 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated Use .login() instead
|
|
|
|
|
*/
|
|
|
|
|
async init(): Promise<void> {
|
|
|
|
|
console.warn(".init() is deprecated, please use .login()");
|
|
|
|
|
await this.login();
|
|
|
|
|
}
|
2022-01-31 22:32:29 +00:00
|
|
|
/**
|
|
|
|
|
* Gets general information about the logged in student
|
|
|
|
|
* @returns Student object
|
|
|
|
|
*/
|
|
|
|
|
async getStudentInfo(): Promise<Student> {
|
|
|
|
|
const data = await this.makeAuthedRequest(API_BASE + "/ping", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: "include_date=true",
|
|
|
|
|
});
|
|
|
|
|
return data?.user;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Get's the logged in student's general activity
|
|
|
|
|
* @param options GetActivityOptions
|
|
|
|
|
* @returns Activity data
|
|
|
|
|
*/
|
|
|
|
|
async getActivity(options?: GetActivityOptions): Promise<ActivityResponse> {
|
|
|
|
|
const params = new URLSearchParams();
|
2022-02-06 10:32:35 +00:00
|
|
|
options?.from && params.append("from", options?.from);
|
2022-01-31 22:32:29 +00:00
|
|
|
options?.to && params.append("to", options?.to);
|
2022-02-06 11:13:23 +00:00
|
|
|
options?.last_id && params.append("last_id", options?.last_id);
|
2022-01-31 22:32:29 +00:00
|
|
|
return this.makeAuthedRequest(
|
2022-02-11 23:46:48 +00:00
|
|
|
API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
|
2022-01-31 22:32:29 +00:00
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Gets the logged in students behaviour points
|
|
|
|
|
* @param options GetBehaviourOptions
|
|
|
|
|
* @returns Array of behaviour points
|
|
|
|
|
*/
|
|
|
|
|
async getBehaviour(
|
|
|
|
|
options?: GetBehaviourOptions
|
|
|
|
|
): Promise<BehaviourResponse> {
|
|
|
|
|
const params = new URLSearchParams();
|
2022-02-06 10:32:35 +00:00
|
|
|
options?.from && params.append("from", options?.from);
|
2022-01-31 22:32:29 +00:00
|
|
|
options?.to && params.append("to", options?.to);
|
|
|
|
|
return await this.makeAuthedRequest(
|
|
|
|
|
API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Gets a list of the logged in student's homeworks
|
|
|
|
|
* @param options GetHomeworkOptions
|
|
|
|
|
* @returns Array of homeworks
|
|
|
|
|
*/
|
|
|
|
|
async listHomeworks(
|
|
|
|
|
options?: GetHomeworkOptions
|
|
|
|
|
): Promise<HomeworksResponse> {
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
if (options?.displayDate)
|
|
|
|
|
params.append("display_date", String(options?.displayDate));
|
2022-02-06 10:32:35 +00:00
|
|
|
|
2022-01-31 22:32:29 +00:00
|
|
|
options?.fromDate && params.append("from", String(options?.fromDate));
|
|
|
|
|
options?.toDate && params.append("to", String(options?.toDate));
|
2022-01-31 22:38:17 +00:00
|
|
|
const data: Array<Homework> = await this.makeAuthedRequest(
|
2022-01-31 22:32:29 +00:00
|
|
|
API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-02-06 10:32:35 +00:00
|
|
|
|
2022-01-31 22:32:29 +00:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2022-02-06 10:32:35 +00:00
|
|
|
data[i].description_raw = data[i].description;
|
|
|
|
|
|
2022-01-31 22:32:29 +00:00
|
|
|
// homework.lesson.replace(/\\/g, '')
|
|
|
|
|
data[i].description = data[i].description.replace(/(<([^>]+)>)/gi, "");
|
|
|
|
|
data[i].description = data[i].description.replace(/ /g, "");
|
|
|
|
|
data[i].description = data[i].description.trim();
|
|
|
|
|
}
|
2022-02-06 10:32:35 +00:00
|
|
|
|
2022-01-31 22:32:29 +00:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Gets the logged in student's lessons for a day
|
|
|
|
|
* @param options GetLessonsOptions
|
|
|
|
|
* @returns Array of lessons
|
|
|
|
|
*/
|
|
|
|
|
async getLessons(options?: GetLessonsOptions): Promise<LessonsResponse> {
|
|
|
|
|
if (!options?.date) throw new Error("No date specified");
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.append("date", String(options?.date));
|
|
|
|
|
return await this.makeAuthedRequest(
|
|
|
|
|
API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-31 23:13:05 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a list of the students badges
|
|
|
|
|
* @returns Array of badges
|
|
|
|
|
*/
|
|
|
|
|
async getBadges(): Promise<BadgesResponse> {
|
|
|
|
|
return await this.makeAuthedRequest(
|
|
|
|
|
API_BASE + "/eventbadges/" + this.studentId,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-02-06 15:14:48 +00:00
|
|
|
/**
|
2022-02-06 15:45:28 +00:00
|
|
|
* Lists the logged in student's announcements
|
|
|
|
|
* @returns Array of announcements
|
2022-02-06 15:14:48 +00:00
|
|
|
*/
|
2022-02-06 15:45:28 +00:00
|
|
|
async listAnnouncements(): Promise<AnnouncementsResponse> {
|
2022-02-06 15:14:48 +00:00
|
|
|
return await this.makeAuthedRequest(
|
2022-02-06 15:45:28 +00:00
|
|
|
API_BASE + "/announcements/" + this.studentId,
|
2022-02-06 15:14:48 +00:00
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-02-06 10:32:35 +00:00
|
|
|
/**
|
|
|
|
|
* Gets the logged in student's detentions
|
|
|
|
|
* @returns Array of detentions
|
|
|
|
|
*/
|
|
|
|
|
async getDetentions(): Promise<DetentionsResponse> {
|
|
|
|
|
return await this.makeAuthedRequest(
|
|
|
|
|
API_BASE + "/detentions/" + this.studentId,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-10-28 16:51:07 +01:00
|
|
|
}
|