import caldav import json from datetime import datetime from urllib.parse import urlparse # Function to create calendar events from JSON data def create_calendar_events_from_json(caldav_url, username, password, json_file_path): try: # 1. Connect to CalDAV server client = caldav.DAVClient( url=caldav_url, username=username, password=password ) # 2. Get principal and calendar my_principal = client.principal() my_calendar = my_principal.calendar(name="Achievement of the Week") # Replace with your calendar name # 3. Read JSON data with open(json_file_path, 'r') as file: events_data = json.load(file) # 4. Create events from JSON data for event in events_data: # Create event in iCal format ical_data = f"""BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT SUMMARY:{event['Title']} DTSTART:{datetime.fromisoformat(event['StartAt']).strftime('%Y%m%dT%H%M%S')} DTEND:{datetime.fromisoformat(event['end_time']).strftime('%Y%m%dT%H%M%S')} DESCRIPTION:{event.get('description', 'Description')} LOCATION:{event.get('location', '')} END:VEVENT END:VCALENDAR """ # Create the event my_calendar.save_event(ical_data) except Exception as e: print(f"Error: {str(e)}") # Example JSON format: """ [ { "Achievement": { "ID": 354286, "Title": "Adventure Man: Action Force Victory", "Description": "Complete the Dr X Battle on Adventure Mode (No Passwords)", "Points": 10, "TrueRatio": 17, "Type": "win_condition", "Author": null, "BadgeName": "399242", "BadgeURL": "/Badge/399242.png", "DateCreated": "2023-09-08", "DateModified": "2023-09-11" }, "Console": { "ID": 5, "Title": "Game Boy Advance" }, "ForumTopic": { "ID": 29289 }, "Game": { "ID": 9848, "Title": "Action Man: Robot Atak" }, "StartAt": "2025-01-06T00:00:00.000000Z", ] """ # Usage example if __name__ == "__main__": # CalDAV server details CALDAV_URL = "https://cal.cdnutter.org/dav.php/calendars/cdnutter/qesvjipbzdtcahrz/" USERNAME = "cdnutter" PASSWORD = "ZRGiXtDHP3jvXq0d" JSON_FILE = "https://retroachievements.org/API/API_GetAchievementOfTheWeek.php?&y=oHdON3RERFPnHV1J6Oiax5F85Wkh5Msq" create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON_FILE)