175 lines
5.1 KiB
JavaScript
175 lines
5.1 KiB
JavaScript
const axios = require("axios");
|
|
const EncryptionHelper = require("./EncryptionHelper");
|
|
|
|
class VirtuellerStundenplan {
|
|
baseUrl = "https://virtueller-stundenplan.de/Reservierung/";
|
|
|
|
constructor(username, password, key) {
|
|
this.username = username;
|
|
this.password = password;
|
|
if(key) this.key = key;
|
|
}
|
|
|
|
async fetchKey(returnKey=false) {
|
|
const url = new URL("RestGetKeyphrase.php", this.baseUrl);
|
|
|
|
try {
|
|
const req = await axios(url.href, {
|
|
headers: {
|
|
Authorization: "Basic " + Buffer.from(this.username + ":" + this.password).toString('base64'),
|
|
},
|
|
});
|
|
|
|
this.key = req.data.KeyPhrase;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
|
|
return returnKey ? this.key : true;
|
|
}
|
|
|
|
async getEnabledFunctions() {
|
|
return await this.request("RESTEnabledFunctions.php");
|
|
}
|
|
|
|
async getCourseList() {
|
|
return await this.request("RESTKursliste.php");
|
|
}
|
|
|
|
async getMessages() {
|
|
return await this.request("RESTMessages.php");
|
|
}
|
|
|
|
/**
|
|
* Returns all Homework (Classbook) entries.
|
|
* Date arguments are inclusive.
|
|
*
|
|
* @param {String|Date} from
|
|
* @param {String|Date} to
|
|
* @returns {Object}
|
|
*/
|
|
async getHomework(from=new Date(), to=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
query.set("KlaBuDateBis", this.getDateString(to));
|
|
return await this.request("RESTHausaufgaben.php", query);
|
|
}
|
|
|
|
async getSchoolYears() {
|
|
return await this.request("RESTSchuljahre.php");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string|number} schoolYear ID of the school year
|
|
* @returns
|
|
*/
|
|
async getMyGrades(schoolYear) {
|
|
const query = new URLSearchParams();
|
|
query.set("SJ", schoolYear);
|
|
return await this.request("RESTMeineNoten.php", query);
|
|
}
|
|
|
|
async getMyTimetable(from=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
return await this.request("RESTMeinStundenplan.php", query);
|
|
}
|
|
|
|
async getTimetable(Class, from=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
query.set("KLASSE", Class);
|
|
return await this.request("RESTKlassenStundenplan.php", query);
|
|
}
|
|
|
|
async getMyInfos() {
|
|
return await this.request("RESTSuSInfo.php");
|
|
}
|
|
|
|
async getMyPicture() {
|
|
return await this.request("RESTSuSMeinBild.php");
|
|
}
|
|
|
|
/**
|
|
* BBS ID QR
|
|
*/
|
|
async getMyBBSIDQR() {
|
|
return await this.request("RESTBBSIDQR.php");
|
|
}
|
|
|
|
/**
|
|
* BBS GUID QR
|
|
*/
|
|
async getMyBBSGUID() {
|
|
return await this.request("RESTBBSIDGUID.php");
|
|
}
|
|
|
|
async reportAsSick(from=new Date(), to=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
query.set("KlaBuDateBis", this.getDateString(to));
|
|
return await this.request("RESTSuSKrankmelden.php", query);
|
|
}
|
|
|
|
async getAbsentDays(from=new Date(), to=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
query.set("KlaBuDateBis", this.getDateString(to));
|
|
return await this.request("RESTFehltageliste.php", query);
|
|
}
|
|
|
|
async getExamList(from=new Date(), to=new Date()) {
|
|
const query = new URLSearchParams();
|
|
query.set("KlaBuDateVon", this.getDateString(from));
|
|
query.set("KlaBuDateBis", this.getDateString(to));
|
|
return await this.request("RESTKlausurliste.php", query);
|
|
}
|
|
|
|
async subscribeToNotifications(Token, Register, IOS) {
|
|
const query = new URLSearchParams();
|
|
query.set("Token", Token);
|
|
query.set("Register", Register);
|
|
query.set("IOS", IOS);
|
|
return await this.request("RESTDeviceToken.php", query);
|
|
}
|
|
|
|
getDateString(date) {
|
|
if(typeof date === "string" && /^\d{4}\-\d{2}\-\d{2}$/.test(date)) {
|
|
return date;
|
|
}
|
|
if(date instanceof Date) {
|
|
return date.toISOString().slice(0, 10);
|
|
}
|
|
throw new Error(`"${date}" is not a valid type for date.`);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} url
|
|
* @param {URLSearchParams} query Query parameters
|
|
* @returns {Any} Response from Server. Error when an error occoured.
|
|
*/
|
|
async request(url, query) {
|
|
if(!this.key && await this.fetchKey() === false) return false;
|
|
|
|
url = new URL(url, this.baseUrl);
|
|
if(query) url.search = query.toString();
|
|
url.searchParams.set("key", EncryptionHelper.makeEncryptedParameter(this.username, this.key));
|
|
|
|
let req;
|
|
try {
|
|
req = await axios(url.href, {
|
|
headers: {
|
|
Authorization: "Basic " + Buffer.from(this.username + ":" + this.password).toString('base64'),
|
|
},
|
|
});
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
|
|
return req.data;
|
|
}
|
|
}
|
|
|
|
module.exports = VirtuellerStundenplan |