Compare commits

..

3 commits

Author SHA1 Message Date
Falko Zurell
2841207137 saving uid to json file for updates. 2024-11-17 19:55:44 +01:00
Falko Zurell
a5367e238f tried to add UIDs and DATESTAMPS 2024-11-17 19:00:12 +01:00
Falko Zurell
48e601f916 changed Host to Gastgeber 2024-11-17 18:40:28 +01:00

View file

@ -1,15 +1,34 @@
import csv import csv
import os import os
import uuid
import json
from ics import Calendar, Event from ics import Calendar, Event
from datetime import datetime from datetime import datetime
import pytz import pytz
UID_STORE_FILE = "event_uids.json"
def load_uids():
"""Load the UID mappings from a JSON file."""
if os.path.exists(UID_STORE_FILE):
with open(UID_STORE_FILE, "r") as file:
return json.load(file)
return {}
def save_uids(uids):
"""Save the UID mappings to a JSON file."""
with open(UID_STORE_FILE, "w") as file:
json.dump(uids, file)
def generate_ical_from_csv(csv_file): def generate_ical_from_csv(csv_file):
# Load existing UIDs
uids = load_uids()
# Generate the output .ics file name # Generate the output .ics file name
ical_file = os.path.splitext(csv_file)[0] + '.ics' ical_file = os.path.splitext(csv_file)[0] + '.ics'
# Define CET time zone # Define CET time zone
cet = pytz.timezone('Europe/Berlin') # CET is commonly represented by Berlin cet = pytz.timezone('Europe/Berlin')
# Create a new calendar # Create a new calendar
calendar = Calendar() calendar = Calendar()
@ -21,33 +40,37 @@ def generate_ical_from_csv(csv_file):
for row in reader: for row in reader:
try: try:
# Parse the data from CSV # Parse the data from CSV
host = row['Host'] host = row['Gastgeber']
description = row['Description'] description = row['Description']
date = row['Date'].strip() # Expected format: DD.MM.YYYY date = row['Date'].strip() # Expected format: DD.MM.YYYY
start_time = row['start time'].strip() # Expected format: HH:MM start_time = row['start time'].strip() # Expected format: HH:MM
end_time = row['end time'].strip() # Expected format: HH:MM end_time = row['end time'].strip() # Expected format: HH:MM
location = row['Location'] location = row['Location']
# Combine date and time using the updated date format and apply CET # Combine date and time using the updated date format and apply CET
start_datetime = cet.localize(datetime.strptime(f"{date} {start_time}", "%d.%m.%Y %H:%M")) start_datetime = cet.localize(datetime.strptime(f"{date} {start_time}", "%d.%m.%Y %H:%M"))
end_datetime = cet.localize(datetime.strptime(f"{date} {end_time}", "%d.%m.%Y %H:%M")) end_datetime = cet.localize(datetime.strptime(f"{date} {end_time}", "%d.%m.%Y %H:%M"))
# Create a unique key for the event
event_key = f"{description}_{date}_{start_time}_{location}"
# Retrieve or generate a UID
uid = uids.get(event_key, str(uuid.uuid4()))
uids[event_key] = uid
# Create a new event # Create a new event
event = Event() event = Event()
event.name = f"{description} (Host: {host})" # Include the host in the event name event.name = f"{description} (Gastgeber: {host})"
event.begin = start_datetime event.begin = start_datetime
event.end = end_datetime event.end = end_datetime
event.location = location event.location = location
event.uid = uid # Set the unique identifier
event.last_modified = datetime.now() # Timestamp for updates
# Add the event to the calendar # Add the event to the calendar
calendar.events.add(event) calendar.events.add(event)
except Exception as e: except Exception as e:
print(f"Error processing row: {row}. Error: {e}") print(f"Error processing row: {row}. Error: {e}")
# Write the calendar to an iCal file # Write the calendar to an iCal file
with open(ical_file, mode='w', encoding='utf-8') as file: with open(ical_file, mode='w', encoding='utf-8') as file:
file.writelines(calendar) file.writelines(calendar)
# Save UIDs for future use
save_uids(uids)
print(f"iCal file created: {ical_file}") print(f"iCal file created: {ical_file}")