Added untis

This commit is contained in:
2023-06-14 22:00:46 +02:00
parent 5574b31a5f
commit 2bedbf19ca
7 changed files with 434 additions and 41 deletions

View File

@@ -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

View File

@@ -15,9 +15,14 @@
<% }); %>
</select>
<button id="export-button" onclick="exportReport()">Export</button>
<button id="copy-button" onclick="copyToClipboard()">Copy</button>
</div>
<textarea id="export-textarea"></textarea>
<h4>Kalendereinträge</h4>
<button id="calendar-copy-button" onclick="copyToClipboard(calendarcalendarCopyButton, calendarTextarea)">Copy</button>
<textarea id="calendar-textarea"></textarea>
<h4>Untis Einträge</h4>
<button id="untis-copy-button" onclick="copyToClipboard(untisCopyButton, untisTextarea)">Copy</button>
<textarea id="untis-textarea"></textarea>
<script src="index.js"></script>
</body>

View File

@@ -1,31 +1,37 @@
const exportTextarea = document.getElementById('export-textarea');
const calendarTextarea = document.getElementById('calendar-textarea');
const untisTextarea = document.getElementById('untis-textarea');
const calendarcalendarCopyButton = document.getElementById('calendar-copy-button');
const untisCopyButton = document.getElementById('untis-copy-button');
const weekList = document.getElementById('week-list');
const exportButton = document.getElementById('export-button');
const copyButton = document.getElementById('copy-button');
let loadingInterval;
function copyToClipboard() {
navigator.clipboard.writeText(exportTextarea.value).then(() => {
copyButton.innerText = 'Copied!';
function copyToClipboard(button, textarea) {
navigator.clipboard.writeText(textarea.value).then(() => {
button.innerText = 'Copied!';
setTimeout(() => {
copyButton.innerText = 'Copy';
button.innerText = 'Copy';
}, 2000);
}).catch(() => {
copyButton.innerText = 'Error!';
button.innerText = 'Error!';
setTimeout(() => {
copyButton.innerText = 'Copy';
button.innerText = 'Copy';
}, 2000);
});
}
function loading() {
exportButton.disabled = true;
exportTextarea.value = 'Loading';
calendarTextarea.value = 'Loading';
untisTextarea.value = '';
loadingInterval = setInterval(() => {
if (exportTextarea.value != 'Loading...') {
exportTextarea.value += '.';
if (calendarTextarea.value != 'Loading...') {
calendarTextarea.value += '.';
} else {
exportTextarea.value = 'Loading';
calendarTextarea.value = 'Loading';
}
}, 500);
}
@@ -33,7 +39,7 @@ function loading() {
function loaded() {
exportButton.disabled = false;
clearInterval(loadingInterval);
exportTextarea.value = '';
calendarTextarea.value = '';
}
function exportReport() {
@@ -46,17 +52,25 @@ function exportReport() {
if (response.ok) {
return response.text();
} else {
exportTextarea.value = 'Error while fetching report';
calendarTextarea.value = 'Error while fetching report';
throw new Error('Error while fetching report');
}
}).then(text => {
loaded();
exportTextarea.value = text;
exportTextarea.style.height = "auto";
exportTextarea.style.height = exportTextarea.scrollHeight + "px";
// parse json
const json = JSON.parse(text);
calendarTextarea.value = json.calendar;
calendarTextarea.style.height = "auto";
calendarTextarea.style.height = calendarTextarea.scrollHeight + "px";
untisTextarea.value = json.untis;
untisTextarea.style.height = "auto";
untisTextarea.style.height = untisTextarea.scrollHeight + "px";
}).catch(error => {
loaded();
exportTextarea.value = 'Error while fetching report';
calendarTextarea.value = 'Error while fetching report';
console.error(error);
});
}

View File

@@ -12,6 +12,12 @@ h1 {
margin-bottom: 10px;
}
h4 {
margin: 0;
margin-top: 20px;
margin-bottom: 5px;
}
.selector {
margin-bottom: 10px;
}
@@ -44,7 +50,7 @@ button:hover {
cursor: pointer;
}
#export-textarea {
textarea {
font-size: 16px;
width: 100%;
resize: none;