From 61249a69859b83ea11a5e8c496a621cd9ba2c040 Mon Sep 17 00:00:00 2001 From: Lino Schmidt Date: Tue, 12 Sep 2023 10:53:59 +0200 Subject: [PATCH] Auto search untis server --- README.md | 5 +++-- example.env | 3 +-- src/server.js | 58 ++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 053d49b..4089248 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ services: - 'UNTIS_SCHOOLS=School1,School2' # Comma seperated list of schools (find in URL when logging in to untis, replace + with space) - 'UNTIS_USERNAMES=USERNAME1,USERNAME2' # Usernames for untis schools - 'UNTIS_PASSWORDS=PASSWORD1,PASSWORD2' # Passwords for untis schools - - 'UNTIS_SERVERS=cissa.webuntis.com,neilo.webuntis.com' # Servers for untis chools ``` + (Nicht benötigte Umgebungsvariablen können entfernt werden) -* `docker-compose up -d` im Verzeichnis ausführen \ No newline at end of file + +* `docker-compose up -d` im Verzeichnis ausführen diff --git a/example.env b/example.env index 16b8311..1a449c6 100644 --- a/example.env +++ b/example.env @@ -5,5 +5,4 @@ CALDAV_PASSWORD=PASSWORD # Password for the caldav server CALDAV_CALENDAR=CALENDARNAME # Calendar name from the caldav server UNTIS_SCHOOLS=School1,School2 # Comma seperated list of schools (find in URL when logging in to untis, replace + with space) UNTIS_USERNAMES=USERNAME1,USERNAME2 # Usernames for untis schools -UNTIS_PASSWORDS=PASSWORD1,PASSWORD2 # Passwords for untis schools -UNTIS_SERVERS=server1.webuntis.com,server2.webuntis.com # Servers for untis chools \ No newline at end of file +UNTIS_PASSWORDS=PASSWORD1,PASSWORD2 # Passwords for untis schools \ No newline at end of file diff --git a/src/server.js b/src/server.js index 60c18f7..a0795d5 100644 --- a/src/server.js +++ b/src/server.js @@ -58,24 +58,48 @@ function getCalendarEntries(weekDate) { return result; } -async function getUntis(weekDate) { - let untisSchools = []; - let untisUsernames = []; - let untisPasswords = []; - let untisServers = []; +async function getUntisServer(school) { + const searchSchool = await fetch("https://mobile.webuntis.com/ms/schoolquery2", { + "method": "POST", + "body": JSON.stringify({ + "id": "wu_schulsuche-" + (new Date()).getTime(), + "method": "searchSchool", + "params": [ + { + "search": school + } + ], + "jsonrpc": "2.0" + }) + }); - 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; - } + const searchSchoolJson = await searchSchool.json(); - if (!optionsSuccess) { - return "Keine Untis-Optionen gefunden"; + return searchSchoolJson.result.schools[0].server; +} + +const untisServers = []; +let untisSchools = []; +let untisUsernames = []; +let untisPasswords = []; +async function getUntis(weekDate) { + if (untisServers.length == 0) { + let optionsSuccess = true; + try { + untisSchools = process.env.UNTIS_SCHOOLS.split(","); + untisUsernames = process.env.UNTIS_USERNAMES.split(","); + untisPasswords = process.env.UNTIS_PASSWORDS.split(","); + } catch (error) { + optionsSuccess = false; + } + + if (!optionsSuccess) { + return "Keine Untis-Optionen gefunden"; + } + + for (school of untisSchools) { + untisServers.push(await getUntisServer(school)); + } } let teachingContent = ""; @@ -182,7 +206,7 @@ app.get('/show', (req, res) => { if (process.env.CALDAV_URL == undefined || process.env.CALDAV_USER == undefined || process.env.CALDAV_PASSWORD == undefined || process.env.CALDAV_CALENDAR == undefined) { showCalendar = false; } - if (process.env.UNTIS_SCHOOLS == undefined || process.env.UNTIS_USERNAMES == undefined || process.env.UNTIS_PASSWORDS == undefined || process.env.UNTIS_SERVERS == undefined) { + if (process.env.UNTIS_SCHOOLS == undefined || process.env.UNTIS_USERNAMES == undefined || process.env.UNTIS_PASSWORDS == undefined) { showUntis = false; } }