Added untis
This commit is contained in:
@@ -3,6 +3,7 @@ const path = require('path');
|
||||
const app = express();
|
||||
const { spawnSync } = require('child_process');
|
||||
const moment = require('moment');
|
||||
const { WebUntis } = require('webuntis');
|
||||
|
||||
// Befehl für die Skriptausführung
|
||||
const command = process.platform === 'win32' ? 'python' : 'python3';
|
||||
@@ -14,14 +15,6 @@ const static = path.join(__dirname, 'static');
|
||||
// Verwende EJS als Vorlagen-Engine
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
// Funktion zum Formatieren des Datums
|
||||
function formatDate(date) {
|
||||
const day = date.getDate().toString().padStart(2, '0');
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
||||
const year = date.getFullYear();
|
||||
return `${day}.${month}.${year}`;
|
||||
}
|
||||
|
||||
// Funktion zum Erstellen der Liste der letzten 50 Wochen
|
||||
function getLast50Weeks() {
|
||||
let currentWeek = moment().startOf('isoWeek');
|
||||
@@ -37,14 +30,7 @@ function getLast50Weeks() {
|
||||
return last50Weeks;
|
||||
}
|
||||
|
||||
function convertToJsEscape(str) {
|
||||
return str.replace(/[\u00A0-\u9999]/g, function (match) {
|
||||
return '\\u' + ('0000' + match.charCodeAt(0).toString(16)).slice(-4);
|
||||
});
|
||||
}
|
||||
|
||||
function getCalendarEntries(week) {
|
||||
const weekDate = getLast50Weeks()[week];
|
||||
function getCalendarEntries(weekDate) {
|
||||
const [startDateStr, endDateStr] = weekDate.split(" - ");
|
||||
|
||||
// Argumente für das Kalender-Skript
|
||||
@@ -67,6 +53,66 @@ function getCalendarEntries(week) {
|
||||
return result;
|
||||
}
|
||||
|
||||
async function getUntis(weekDate) {
|
||||
const untisSchools = process.env.UNTIS_SCHOOLS.split(",");
|
||||
const untisUsernames = process.env.UNTIS_USERNAMES.split(",");
|
||||
const untisPasswords = process.env.UNTIS_PASSWORDS.split(",");
|
||||
const untisServers = process.env.UNTIS_SERVERS.split(",");
|
||||
|
||||
let teachingContent = "";
|
||||
|
||||
await Promise.all(untisSchools.map(async (school, index) => {
|
||||
if(untisUsernames[index].length > 0 && untisPasswords[index].length > 0 && untisServers[index].length > 0) {
|
||||
const client = new WebUntis(school, untisUsernames[index], untisPasswords[index], untisServers[index]);
|
||||
|
||||
// Anmelden
|
||||
await client.login();
|
||||
|
||||
const cookie = await client._buildCookies();
|
||||
const jwtToken = await client._getJWT();
|
||||
const authorization = `Bearer ${jwtToken}`;
|
||||
|
||||
const [startDateDay, startDateMonth, startDateYear] = weekDate.split(" - ")[0].split(".")
|
||||
let startDate = new Date(`${startDateYear}-${startDateMonth}-${startDateDay}T00:00:00.000Z`);
|
||||
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const timetable = await client.getOwnClassTimetableFor(startDate).catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
try {
|
||||
const lessonid = timetable[0].kl[0].id;
|
||||
|
||||
startDateStr = startDate.toISOString().split("T")[0];
|
||||
|
||||
await fetch(`https://${untisServers[index]}/WebUntis/api/rest/view/v1/calendar-entry/detail?elementId=${lessonid}&elementType=1&endDateTime=${startDateStr}T23%3A59%3A59&startDateTime=${startDateStr}T00%3A00%3A00`, {
|
||||
"headers": {
|
||||
"authorization": authorization,
|
||||
"cookie": cookie,
|
||||
},
|
||||
"method": "GET"
|
||||
}).then(res => res.json()).then(json => {
|
||||
for (entry of json.calendarEntries) {
|
||||
if (!teachingContent.includes(entry.teachingContent)) {
|
||||
if (teachingContent != "") teachingContent += '\n';
|
||||
teachingContent += "- "+entry.teachingContent;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {}
|
||||
|
||||
startDate.setDate(startDate.getDate() + i);
|
||||
}
|
||||
|
||||
// Abmelden
|
||||
await client.logout();
|
||||
}
|
||||
}));
|
||||
|
||||
if(teachingContent == "") teachingContent = "Keine Unterrichtsinhalte gefunden";
|
||||
return teachingContent;
|
||||
}
|
||||
|
||||
app.use("/", express.static(static));
|
||||
|
||||
// Definiere eine Route für die Startseite
|
||||
@@ -75,8 +121,12 @@ app.get('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Definiere eine Route für die Ausgabe der Daten
|
||||
app.get('/getreport', (req, res) => {
|
||||
res.send(getCalendarEntries(req.query.week));
|
||||
app.get('/getreport', async (req, res) => {
|
||||
const weekDate = getLast50Weeks()[req.query.week];
|
||||
res.send({
|
||||
calendar: getCalendarEntries(weekDate),
|
||||
untis: await getUntis(weekDate)
|
||||
});
|
||||
});
|
||||
|
||||
// Starte den Webserver
|
||||
|
||||
Reference in New Issue
Block a user