From 61a788961d7df59f5625d3981c40a19597d69e8e Mon Sep 17 00:00:00 2001 From: "Lennart J. Kurzweg (Nx2)" Date: Mon, 31 Mar 2025 01:26:42 +0200 Subject: [PATCH] calendar lr + dicos description + cleanup --- configuration.nix | 1 + system-modules/calendar-dicos.nix | 13 ++++-- system-modules/calendar-lec.nix | 8 ---- system-modules/calendar-lr.nix | 74 +++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 system-modules/calendar-lr.nix diff --git a/configuration.nix b/configuration.nix index 0afddfa..6dc0e36 100644 --- a/configuration.nix +++ b/configuration.nix @@ -56,6 +56,7 @@ ./system-modules/calendar-publish.nix ./system-modules/calendar-lec.nix + ./system-modules/calendar-lr.nix ./system-modules/calendar-dicos.nix ] else []); diff --git a/system-modules/calendar-dicos.nix b/system-modules/calendar-dicos.nix index ac91913..2960bb4 100644 --- a/system-modules/calendar-dicos.nix +++ b/system-modules/calendar-dicos.nix @@ -56,7 +56,7 @@ def modify_event(event): global latest_week global deficit - if event.name and "DICOS" in event.name: + if event.name is not None and "DICOS" in event.name: length = (event.end - event.begin).seconds / 3600 money_made = divmod(length * NETTO_STUNDE, 1) @@ -78,9 +78,14 @@ def modify_event(event): latest_goal = goal latest_week = week - progress_line = f"({fraction_to_unicode(progress)}/{fraction_to_unicode(goal)})" + new_description = [event.description.split("\n")[0] or "::"] + new_description.append("") + new_description.append(f"Netto: {money_made[0]:.0f},{int(money_made[1] * 10):02d}€") + new_description.append(f"This weeks porgress: ({fraction_to_unicode(progress)}/{fraction_to_unicode(goal)})") + new_description.append(f"This weeks deficit: {fraction_to_unicode(deficit)}") + + event.description = "\n".join(new_description) - event.description = f"Netto: {money_made[0]:.0f},{int(money_made[1] * 10):02d}€\nThis weeks porgress: {progress_line}\nThis weeks deficit: {fraction_to_unicode(deficit)}" event.name = f"DICOS {fraction_to_unicode(length)}" return event @@ -92,7 +97,7 @@ def process_ics_file(filepath): modified = False for event in calendar.events: - if 'DICOS' in event.name: + if event.name is not None and 'DICOS' in event.name: event = modify_event(event) modified = True diff --git a/system-modules/calendar-lec.nix b/system-modules/calendar-lec.nix index 0fd5742..280c13a 100644 --- a/system-modules/calendar-lec.nix +++ b/system-modules/calendar-lec.nix @@ -21,18 +21,10 @@ ]; flakeIgnore = [ "E302" "E305" "E226" "E501" ]; } /*python */ '' -import hashlib from ics import Calendar import requests from datetime import timedelta -def get_event_hash(event): - """ - Generate a unique hash for an event based on its details. - """ - event_data = f"{event.name}{event.begin}{event.end}{event.description}" - return hashlib.md5(event_data.encode('utf-8')).hexdigest() - def adjust_events(events): """ Adjust overlapping events to ensure they do not conflict. diff --git a/system-modules/calendar-lr.nix b/system-modules/calendar-lr.nix new file mode 100644 index 0000000..d9678ed --- /dev/null +++ b/system-modules/calendar-lr.nix @@ -0,0 +1,74 @@ +{ config, pkgs, domain, ... }: +{ + systemd.timers."nx_cal_lr" = { + enable = true; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "41m"; + OnUnitActiveSec = "24h"; + Unit = "nx_cal_lr.service"; + }; + }; + + systemd.services."nx_cal_lr" = { + script = let + nx_cal_lr = (pkgs.writers.writePython3Bin "nx_cal_lr" { + libraries = with pkgs.python3Packages; [ + ics + requests + ]; + flakeIgnore = [ "E302" "E305" "E226" "E501" ]; + } /*python */ '' +from ics import Calendar +import requests + +def filter_events(events): + return [event for event in events if ("LR" in event.name) or ("TBD" in event.name)] + +def fetch_and_save_ical_events(ical_url, save_path): + """ + Fetch events from an iCal URL and save them as a single combined calendar. + """ + try: + # Fetch the iCal data + response = requests.get(ical_url) + response.raise_for_status() + + # Parse the iCal data + calendar = Calendar(response.text) + + # Adjust events + adjusted_events = filter_events(list(calendar.events)) + + # Create a new combined calendar + combined_calendar = Calendar() + for event in adjusted_events: + combined_calendar.events.add(event) + + # Save the combined calendar to a single .ics file + with open(save_path, 'w') as file: + file.writelines(combined_calendar.serialize_iter()) + + print(f"Saved combined calendar to {save_path}") + + except requests.exceptions.RequestException as e: + print(f"Error fetching iCal data: {e}") + except Exception as e: + print(f"Error processing iCal data: {e}") + +if __name__ == "__main__": + # Replace with your iCal URL and target file path + ICAL_URL = "https://zlypher.github.io/lol-events/cal/league-of-legends-nlc.ical" + SAVE_PATH = "${config.services.nginx.virtualHosts."${domain}".root}/lr.ics" + + fetch_and_save_ical_events(ICAL_URL, SAVE_PATH) +''); + in '' + ${nx_cal_lr}/bin/nx_cal_lr + ''; + serviceConfig = { + Type = "oneshot"; + User = "nx2"; + }; + }; +}