RAJSONToCal/app.py

112 lines
3.7 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
import sys
2025-01-09 19:30:19 -08:00
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
2025-01-06 16:42:04 -08:00
# 3a. Read JSON data
2025-01-09 19:30:19 -08:00
response = requests.get(json)
response.raise_for_status()
# 3b. Parse JSON + Time Calculator
2025-01-09 19:30:19 -08:00
events_data = response.json()
# 3c. Convert UTC to PST
2025-01-09 19:30:19 -08:00
start_time = datetime.fromisoformat(events_data['StartAt']).replace(tzinfo=ZoneInfo('UTC')).astimezone(ZoneInfo('America/Los_Angeles'))
end_time = start_time + timedelta(weeks=1)
# 3d. Check if Username is on unlocks
status = "NEEDS-ACTION"
if "Unlocks" in events_data:
for unlock in events_data["Unlocks"]:
if unlock["User"] == "{RA_USER}": # Replace with your username
status = "COMPLETED"
break
# 3e. Delete old data if it's the same.
2025-01-09 19:30:19 -08:00
for event in my_calendar.events():
if events_data['Achievement']['Title'] in event.data:
event.delete()
#print(f"Checking and removing duplicates...")
2025-01-09 19:30:19 -08:00
remaining = [e for e in my_calendar.events() if events_data['Achievement']['Title'] in e.data]
#print(f"Remaining duplicates: {len(remaining)}")
2025-01-09 19:30:19 -08:00
2025-01-06 16:42:04 -08:00
# 4. Create events from JSON data
print(f"Updating calendar...")
ical_data_event = 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']}\\n\\nhttps://retroachievements.org/achievement/{events_data['Achievement']['ID']}
LOCATION:{events_data['Game']['Title']} for the {events_data['Console']['Title']}
CATEGORIES:RetroAchievements
2025-01-06 16:42:04 -08:00
END:VEVENT
END:VCALENDAR
"""
print(f"Updating reminders...")
ical_data_todo = f"""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VTODO
SUMMARY:{events_data['Achievement']['Title']}
DUE:{end_time.strftime('%Y%m%dT%H%M%S')}
STATUS:{status}
PRIORITY:1
DESCRIPTION:{events_data['Achievement']['Description']}\\n\\nhttps://retroachievements.org/achievement/{events_data['Achievement']['ID']}
LOCATION:{events_data['Game']['Title']} for the {events_data['Console']['Title']}
CATEGORIES:RetroAchievements
END:VTODO
END:VCALENDAR
"""
my_calendar.save_event(ical_data_event)
print(f"Calendar was updated.")
my_calendar.save_todo(ical_data_todo)
print(f"Reminders were updated.")
2025-01-06 16:42:04 -08:00
except Exception as e:
print(f"Error: {str(e)}")
# Usage example
if __name__ == "__main__":
2025-01-09 19:30:19 -08:00
if len(sys.argv) != 6:
2025-01-09 19:44:13 -08:00
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_USER = sys.argv[5]
2025-01-09 19:44:13 -08:00
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}"
response = requests.get(JSON)
2025-01-09 19:30:19 -08:00
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)