Show calendar houers per day

This commit is contained in:
2023-09-14 10:08:43 +02:00
parent 0be5f88c3b
commit 533bf9914b
4 changed files with 68 additions and 23 deletions

View File

@@ -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.')
print(json.dumps('CalendarNotFound'))

View File

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

View File

@@ -21,6 +21,7 @@
<button id="calendar-copy-button" onclick="copyToClipboard(calendarcalendarCopyButton, calendarTextarea)">Kopieren</button>
</div>
<textarea id="calendar-textarea"></textarea>
<table id="stunden-table"></table>
</div>
<div id="untis-div">

View File

@@ -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 += '<tr><td>' + time[0] + ':</td><td>' + Math.round(time[1] / 1000 / 60 / 60 * 10) / 10 + 'h</td></tr>';
});
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";