🎉 Mostly working matrix bot
(timetable group handling missing)
This commit is contained in:
74
timetable.js
Normal file
74
timetable.js
Normal file
@ -0,0 +1,74 @@
|
||||
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user