From 533bf9914b4931e86769e5ce34652f3fdb7484e3 Mon Sep 17 00:00:00 2001 From: Lino Schmidt Date: Thu, 14 Sep 2023 10:08:43 +0200 Subject: [PATCH] Show calendar houers per day --- src/getCalendar.py | 22 ++++++----------- src/server.js | 12 ++++------ src/static/index.html | 1 + src/static/index.js | 56 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/getCalendar.py b/src/getCalendar.py index 5fd8ecd..4ecf378 100644 --- a/src/getCalendar.py +++ b/src/getCalendar.py @@ -1,6 +1,8 @@ import datetime +import json from caldav.davclient import DAVClient import argparse +import time # Parse command-line arguments parser = argparse.ArgumentParser(description='Retrieve events from a CalDAV calendar.') @@ -38,21 +40,11 @@ if calendar: for result in results: event = result.instance.vevent summary = event.summary.value if event.summary else None - start_time = event.dtstart.value.strftime("%d.%m.%Y %H:%M:%S") - events.append((start_time, summary)) + start_time = time.mktime(event.dtstart.value.timetuple()) + end_time = time.mktime(event.dtend.value.timetuple()) + events.append({"stime": start_time, "etime": end_time, "summary": summary}) - # Sort the events by start time - events.sort(key=lambda x: datetime.datetime.strptime(x[0], "%d.%m.%Y %H:%M:%S")) - - # Remove Events with same summary - summarys = [] - for summary in events: - if summary[1] not in summarys: - summarys.append(summary[1]) - - # Print the sorted events - for summary in summarys: - print('-', summary) + print(json.dumps(events)) else: - print('Calendar not found.') \ No newline at end of file + print(json.dumps('CalendarNotFound')) diff --git a/src/server.js b/src/server.js index fc278eb..293f57f 100644 --- a/src/server.js +++ b/src/server.js @@ -48,14 +48,12 @@ function getCalendarEntries(weekDate) { 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"; + // Skript ausführen und Output als JSON ausgeben + try { + return JSON.parse(spawnSync(command, [scriptPath, ...args], { encoding: 'utf-8' }).stdout); + } catch (error) { + return []; } - - return result; } async function getUntisServer(school) { diff --git a/src/static/index.html b/src/static/index.html index eea2860..169cee0 100644 --- a/src/static/index.html +++ b/src/static/index.html @@ -21,6 +21,7 @@ +
diff --git a/src/static/index.js b/src/static/index.js index acc5896..283868b 100644 --- a/src/static/index.js +++ b/src/static/index.js @@ -12,6 +12,8 @@ const untisDiv = document.getElementById('untis-div'); const exportStatus = document.getElementById('export-status'); +const stundenTable = document.getElementById('stunden-table'); + fetch('/show') .then(response => { if (response.ok) { @@ -77,6 +79,7 @@ function loading() { exportStatus.innerHTML = 'Wird geladen'; calendarTextarea.value = ''; untisTextarea.value = ''; + stundenTable.innerHTML = ''; loadingInterval = setInterval(() => { if (exportStatus.innerHTML != 'Wird geladen...') { exportStatus.innerHTML += '.'; @@ -111,7 +114,58 @@ function exportReport() { // parse json const json = JSON.parse(text); - calendarTextarea.value = json.calendar; + let calendarSTR = ''; + if (json.calendar == "CalendarNotFound") { + calendarSTR = 'Kalender nicht gefunden'; + } else if (json.calendar.length == 0) { + calendarSTR = 'Keine Einträge im Kalender'; + } else { + let calendarTime = []; + const calendarSorted = []; + json.calendar.forEach((day) => { + const startTime = new Date(day.stime * 1000); + const date = startTime.toLocaleDateString(); + const push = [date, startTime, day.summary]; + if (!calendarSorted.includes(push)) { + calendarSorted.push(push); + } + + const endTime = new Date(day.etime * 1000); + const dateLength = endTime - startTime; + + let found = false; + calendarTime.forEach((time) => { + if (time[0] == date) { + time[1] += dateLength; + found = true; + } + }); + + if (!found) { + calendarTime.push([date, dateLength]); + } + }); + + // Sort by date + calendarSorted.sort(function(a, b) { + return a[1] - b[1]; + }); + + calendarTime.forEach((time) => { + stundenTable.innerHTML += '' + time[0] + ':' + Math.round(time[1] / 1000 / 60 / 60 * 10) / 10 + 'h'; + }); + + calendarSorted.forEach((day) => { + if (!calendarSTR.includes(day[2])) { + if (calendarSTR != '') { + calendarSTR += '\n'; + } + calendarSTR += '- ' + day[2]; + } + }); + } + + calendarTextarea.value = calendarSTR; calendarTextarea.style.height = "auto"; calendarTextarea.style.height = calendarTextarea.scrollHeight + "px";