This repository has been archived on 2025-06-19. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Timetable-V2-Notify/timetable.js
minie4 9a4a4a2f08 🎉 Mostly working matrix bot
(timetable group handling missing)
2023-06-23 18:56:59 +02:00

75 lines
2.1 KiB
JavaScript

import fetch from "node-fetch";
export class TimetableClient {
constructor(apiEndpoint, token) {
this.apiEndpoint = apiEndpoint;
this.token = token;
}
login(password) {
fetch(
this.apiEndpoint + `/token?password=${encodeURIComponent(password)}`,
{ method: "post" }
)
.then((e) => {
if (e.status == 401) {
console.warn("Invalid Timetable V2 password!");
return;
} else if (e.status != 200) {
console.log("Failed requesting Timetable V2 API-Token!");
return;
}
return e.json();
})
.then((token) => (this.token = token.token));
}
getTimetables(forClass) {
return new Promise((resolve, reject) => {
fetch(
this.apiEndpoint +
`/timetable?token=${encodeURIComponent(
this.token
)}&class=${encodeURIComponent(forClass)}`
)
.then((e) => {
if (e.status != 200) reject();
else return e.json();
})
.then((timetables) => resolve(timetables));
});
}
async getGroups(timetableClass, timetableId) {
const timetables = await fetch(
this.apiEndpoint +
`/timetable?token=${encodeURIComponent(
this.token
)}&class=${encodeURIComponent(timetableClass)}`
);
const timetable = (await timetables.json()).timetables.find(
(e) => e.id == timetableId
);
const foundTimetableGroups = [];
if (!timetable.data) return [];
// Make a list of all possible timetable groups
// found in the current timetable
for (const day of timetable.data) {
for (const lesson of day) {
if (Array.isArray(lesson) && lesson.length > 1) {
for (const group of lesson) {
if (group.group && !foundTimetableGroups.includes(group.group)) {
foundTimetableGroups.push(group.group);
}
}
}
}
}
return foundTimetableGroups;
}
async getUpdates() {
const history = await fetch(
this.apiEndpoint + "/history" + `?token=${encodeURIComponent(this.token)}`
);
return await history.json();
}
}