From a2dfc183580da2cdb001618e1cc74b350d85ccef Mon Sep 17 00:00:00 2001 From: Chris Nutter Date: Thu, 9 Jan 2025 19:30:19 -0800 Subject: [PATCH] finished initial setup --- .gitignore | 19 +++++++++++++++++ script.py => app.py | 51 ++++++++++++++++++++++++++++++++------------- requirements.txt | 3 +++ 3 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 .gitignore rename script.py => app.py (56%) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e79935 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Created by venv; see https://docs.python.org/3/library/venv.html +# Virtual Environment +venv/ +env/ +.env/ +.venv/ + +# Python +__pycache__/ +*.py[cod] +*$py.class + +# IDE +.idea/ +.vscode/ + +# Misc +.DS_Store +*.log diff --git a/script.py b/app.py similarity index 56% rename from script.py rename to app.py index 2d86fec..a5a0db1 100644 --- a/script.py +++ b/app.py @@ -1,10 +1,16 @@ import caldav import json -from datetime import datetime +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_file_path): +def create_calendar_events_from_json(caldav_url, username, password, json): try: # 1. Connect to CalDAV server client = caldav.DAVClient( @@ -18,25 +24,38 @@ def create_calendar_events_from_json(caldav_url, username, password, json_file_p 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) - + 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 - for event in events_data: - # Create event in iCal format - ical_data = f"""BEGIN:VCALENDAR + # 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', '')} +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) + my_calendar.save_event(ical_data) except Exception as e: print(f"Error: {str(e)}") @@ -75,10 +94,14 @@ END:VCALENDAR # 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" + response = requests.get(JSON_FILE) + data = response.json() create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON_FILE) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..26ac45d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +caldav +requests +tzdata \ No newline at end of file