RAJSONToCal/app.py

113 lines
3.2 KiB
Python
Raw Normal View History

2025-01-06 16:42:04 -08:00
import caldav
import json
2025-01-09 19:30:19 -08:00
import requests
import tzdata
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
2025-01-06 16:42:04 -08:00
from urllib.parse import urlparse
2025-01-09 19:30:19 -08:00
from urllib.request import urlopen
import urllib.error
2025-01-06 16:42:04 -08:00
# Function to create calendar events from JSON data
2025-01-09 19:30:19 -08:00
def create_calendar_events_from_json(caldav_url, username, password, json):
2025-01-06 16:42:04 -08:00
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
2025-01-09 19:30:19 -08:00
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)}")
2025-01-06 16:42:04 -08:00
# 4. Create events from JSON data
2025-01-09 19:30:19 -08:00
# Create event in iCal format
ical_data = f"""BEGIN:VCALENDAR
2025-01-06 16:42:04 -08:00
VERSION:2.0
BEGIN:VEVENT
2025-01-09 19:30:19 -08:00
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']}
2025-01-06 16:42:04 -08:00
END:VEVENT
END:VCALENDAR
"""
# Create the event
2025-01-09 19:30:19 -08:00
my_calendar.save_event(ical_data)
2025-01-06 16:42:04 -08:00
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__":
2025-01-09 19:30:19 -08:00
2025-01-09 19:44:13 -08:00
if len(sys.argv) != 5:
print("Usage: python3 app.py <caldav_url> <username> <password> <ra_api_key>")
sys.exit(1)
CALDAV_URL = sys.argv[1]
USERNAME = sys.argv[2]
PASSWORD = sys.argv[3]
RA_API = sys.argv[4]
2025-01-06 16:42:04 -08:00
# CalDAV server details
2025-01-09 19:44:13 -08:00
JSON = f"https://retroachievements.org/API/API_GetAchievementOfTheWeek.php?&y={RA_API}"
2025-01-09 19:30:19 -08:00
response = requests.get(JSON_FILE)
data = response.json()
2025-01-06 16:42:04 -08:00
2025-01-09 19:44:13 -08:00
create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON)