const express = require('express'); const path = require('path'); const app = express(); const { spawnSync } = require('child_process'); const moment = require('moment'); const { WebUntis } = require('webuntis'); var cron = require('node-cron'); // Befehl für die Skriptausführung const command = process.platform === 'win32' ? 'python' : 'python3'; // Pfad zum Kalender-Skript const scriptPath = path.resolve(__dirname, 'getCalendar.py'); // Pfad zum Ordner mit den Webseiten-Dateien const static = path.join(__dirname, 'static'); // Verwende EJS als Vorlagen-Engine app.set('view engine', 'ejs'); function getlast50weeks() { let currentWeek = moment().startOf('isoWeek'); let last50WeeksCache = []; for (let i = 0; i < 50; i++) { const startOfWeek = currentWeek.clone().format('DD.MM.YYYY'); const endOfWeek = currentWeek.clone().add(6, 'days').format('DD.MM.YYYY'); last50WeeksCache.push(startOfWeek + " - " + endOfWeek); currentWeek.subtract(7, 'days'); } return last50WeeksCache; } let last50Weeks = getlast50weeks(); cron.schedule('0 6 * * *', () => { last50Weeks = getlast50weeks(); }); function getCalendarEntries(weekDate) { const [startDateStr, endDateStr] = weekDate.split(" - "); // Argumente für das Kalender-Skript const args = [ process.env.CALDAV_URL, process.env.CALDAV_USER, process.env.CALDAV_PASSWORD, process.env.CALDAV_CALENDAR, startDateStr, endDateStr ]; // Skript ausführen und Output in einer Variable speichern const result = spawnSync(command, [scriptPath, ...args], { encoding: 'utf-8' }).stdout; if (result === "") { return "Keine Termine gefunden"; } return result; } async function getUntis(weekDate) { let untisSchools = []; let untisUsernames = []; let untisPasswords = []; let untisServers = []; let optionsSuccess = true; try { untisSchools = process.env.UNTIS_SCHOOLS.split(","); untisUsernames = process.env.UNTIS_USERNAMES.split(","); untisPasswords = process.env.UNTIS_PASSWORDS.split(","); untisServers = process.env.UNTIS_SERVERS.split(","); } catch (error) { optionsSuccess = false; } if (!optionsSuccess) { return "Keine Untis-Optionen gefunden"; } 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 app.get('/', (req, res) => { res.render(path.join(static, 'index'), {options:last50Weeks }); }); app.get('/getlast50weeks', (req, res) => { res.send(last50Weeks); }); // Definiere eine Route für die Ausgabe der Daten app.get('/getreport', async (req, res) => { const weekDate = last50Weeks[req.query.week]; res.send({ calendar: getCalendarEntries(weekDate), untis: await getUntis(weekDate) }); }); // Starte den Webserver app.listen(3000, () => { console.log('Webserver läuft auf Port 3000'); });