import caldav import json import requests import tzdata from datetime import datetime, timedelta from zoneinfo import ZoneInfo from urllib.parse import urlparse from urllib.request import urlopen import urllib.error # Function to create calendar events from JSON data def create_calendar_events_from_json(caldav_url, username, password, json): 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 response = requests.get(json) response.raise_for_status() # 3a. Parse JSON + Time Calculator events_data = response.json() start_time = datetime.fromisoformat(events_data['StartAt']).replace(tzinfo=ZoneInfo('UTC')).astimezone(ZoneInfo('America/Los_Angeles')) end_time = start_time + timedelta(weeks=1) # 3b. Delete old data if it's the same. for event in my_calendar.events(): if events_data['Achievement']['Title'] in event.data: event.delete() # Optional: Verify deletion remaining = [e for e in my_calendar.events() if events_data['Achievement']['Title'] in e.data] print(f"Remaining duplicates: {len(remaining)}") # 4. Create events from JSON data # Create event in iCal format ical_data = f"""BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT SUMMARY:{events_data['Achievement']['Title']} DTSTART:{start_time.strftime('%Y%m%dT%H%M%S')} DTEND:{end_time.strftime('%Y%m%dT%H%M%S')} DESCRIPTION:{events_data['Achievement']['Description']} LOCATION:{events_data['Console']['Title']} - {events_data['Game']['Title']} 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__": if len(sys.argv) != 5: print("Usage: python3 app.py ") sys.exit(1) CALDAV_URL = sys.argv[1] USERNAME = sys.argv[2] PASSWORD = sys.argv[3] RA_API = sys.argv[4] # CalDAV server details JSON = f"https://retroachievements.org/API/API_GetAchievementOfTheWeek.php?&y={RA_API}" response = requests.get(JSON_FILE) data = response.json() create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON)