finished initial setup

master
Chris Nutter 2025-01-09 19:30:19 -08:00
parent 31b85bc8d3
commit a2dfc18358
3 changed files with 59 additions and 14 deletions

19
.gitignore vendored Normal file
View File

@ -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

View File

@ -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)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
caldav
requests
tzdata