From 2b6e5fdff9f4badc573b52694635c4ca3a4d975d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tjark=20Fr=C3=B6lje?= <40803319+Tenosiey@users.noreply.github.com> Date: Wed, 25 Jun 2025 11:25:23 +0200 Subject: [PATCH] Add ICS export for waste collection --- main.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.py b/main.py index 58e7a1e..a5aa57d 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import requests import json from datetime import datetime, timedelta +from uuid import uuid4 # URL to the JSON file url = "https://firebasestorage.googleapis.com/v0/b/abfall-ammerland.appspot.com/o/and%2F5%2Fawbapp.json?alt=media" @@ -78,9 +79,33 @@ def display_abfalltermine(collection_days): if collection_days: for type, date in sorted(collection_days, key=lambda x: x[1]): print(f"{type}: {date.strftime('%Y-%m-%d')}") + save_abfalltermine_to_ics(collection_days) else: print("No waste collection dates found.") +# Function to save waste collection dates to an ICS calendar file +def save_abfalltermine_to_ics(collection_days, filename="waste_collection.ics"): + header = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Waste Collection//EN\n" + events = [] + for abfall_type, date in sorted(collection_days, key=lambda x: x[1]): + start = date.strftime("%Y%m%d") + end = (date + timedelta(days=1)).strftime("%Y%m%d") + uid = str(uuid4()) + event = ( + "BEGIN:VEVENT\n" + f"UID:{uid}\n" + f"DTSTAMP:{datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')}\n" + f"SUMMARY:{abfall_type}\n" + f"DTSTART;VALUE=DATE:{start}\n" + f"DTEND;VALUE=DATE:{end}\n" + "END:VEVENT\n" + ) + events.append(event) + footer = "END:VCALENDAR\n" + with open(filename, "w") as f: + f.write(header + "".join(events) + footer) + print(f"Saved waste collection dates to {filename}") + # Function to retrieve waste collection dates from waste_entries def get_waste_dates(waste_entries): collection_days = []