updated with todos and sysarg implement
parent
8466b9acbc
commit
ad609e913e
93
app.py
93
app.py
|
@ -2,6 +2,7 @@ import caldav
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import tzdata
|
import tzdata
|
||||||
|
import sys
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
@ -21,92 +22,90 @@ def create_calendar_events_from_json(caldav_url, username, password, json):
|
||||||
|
|
||||||
# 2. Get principal and calendar
|
# 2. Get principal and calendar
|
||||||
my_principal = client.principal()
|
my_principal = client.principal()
|
||||||
my_calendar = my_principal.calendar(name="Achievement of the Week") # Replace with your calendar name
|
my_calendar = my_principal.calendar(name="Achievement of The Week") # Replace with your calendar name
|
||||||
|
|
||||||
# 3. Read JSON data
|
# 3a. Read JSON data
|
||||||
response = requests.get(json)
|
response = requests.get(json)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
# 3a. Parse JSON + Time Calculator
|
# 3b. Parse JSON + Time Calculator
|
||||||
events_data = response.json()
|
events_data = response.json()
|
||||||
|
|
||||||
|
# 3c. Convert UTC to PST
|
||||||
start_time = datetime.fromisoformat(events_data['StartAt']).replace(tzinfo=ZoneInfo('UTC')).astimezone(ZoneInfo('America/Los_Angeles'))
|
start_time = datetime.fromisoformat(events_data['StartAt']).replace(tzinfo=ZoneInfo('UTC')).astimezone(ZoneInfo('America/Los_Angeles'))
|
||||||
end_time = start_time + timedelta(weeks=1)
|
end_time = start_time + timedelta(weeks=1)
|
||||||
|
|
||||||
# 3b. Delete old data if it's the same.
|
# 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.
|
||||||
for event in my_calendar.events():
|
for event in my_calendar.events():
|
||||||
if events_data['Achievement']['Title'] in event.data:
|
if events_data['Achievement']['Title'] in event.data:
|
||||||
event.delete()
|
event.delete()
|
||||||
|
|
||||||
# Optional: Verify deletion
|
#print(f"Checking and removing duplicates...")
|
||||||
remaining = [e for e in my_calendar.events() if events_data['Achievement']['Title'] in e.data]
|
remaining = [e for e in my_calendar.events() if events_data['Achievement']['Title'] in e.data]
|
||||||
print(f"Remaining duplicates: {len(remaining)}")
|
#print(f"Remaining duplicates: {len(remaining)}")
|
||||||
|
|
||||||
# 4. Create events from JSON data
|
# 4. Create events from JSON data
|
||||||
# Create event in iCal format
|
print(f"Updating calendar...")
|
||||||
ical_data = f"""BEGIN:VCALENDAR
|
ical_data_event = f"""BEGIN:VCALENDAR
|
||||||
VERSION:2.0
|
VERSION:2.0
|
||||||
BEGIN:VEVENT
|
BEGIN:VEVENT
|
||||||
SUMMARY:{events_data['Achievement']['Title']}
|
SUMMARY:{events_data['Achievement']['Title']}
|
||||||
DTSTART:{start_time.strftime('%Y%m%dT%H%M%S')}
|
DTSTART:{start_time.strftime('%Y%m%dT%H%M%S')}
|
||||||
DTEND:{end_time.strftime('%Y%m%dT%H%M%S')}
|
DTEND:{end_time.strftime('%Y%m%dT%H%M%S')}
|
||||||
DESCRIPTION:{events_data['Achievement']['Description']}
|
DESCRIPTION:{events_data['Achievement']['Description']}\\n\\nhttps://retroachievements.org/achievement/{events_data['Achievement']['ID']}
|
||||||
LOCATION:{events_data['Console']['Title']} - {events_data['Game']['Title']}
|
LOCATION:{events_data['Game']['Title']} for the {events_data['Console']['Title']}
|
||||||
|
CATEGORIES:RetroAchievements
|
||||||
END:VEVENT
|
END:VEVENT
|
||||||
END:VCALENDAR
|
END:VCALENDAR
|
||||||
"""
|
"""
|
||||||
# Create the event
|
print(f"Updating reminders...")
|
||||||
my_calendar.save_event(ical_data)
|
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.")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {str(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
|
# Usage example
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
if len(sys.argv) != 5:
|
if len(sys.argv) != 6:
|
||||||
print("Usage: python3 app.py <caldav_url> <username> <password> <ra_api_key>")
|
print("Usage: python3 app.py <caldav_url> <username> <password> <ra_api_key>")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
CALDAV_URL = sys.argv[1]
|
CALDAV_URL = sys.argv[1]
|
||||||
USERNAME = sys.argv[2]
|
USERNAME = sys.argv[2]
|
||||||
PASSWORD = sys.argv[3]
|
PASSWORD = sys.argv[3]
|
||||||
|
RA_USER = sys.argv[5]
|
||||||
RA_API = sys.argv[4]
|
RA_API = sys.argv[4]
|
||||||
|
|
||||||
# CalDAV server details
|
# CalDAV server details
|
||||||
JSON = f"https://retroachievements.org/API/API_GetAchievementOfTheWeek.php?&y={RA_API}"
|
JSON = f"https://retroachievements.org/API/API_GetAchievementOfTheWeek.php?&y={RA_API}"
|
||||||
response = requests.get(JSON_FILE)
|
response = requests.get(JSON)
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON)
|
create_calendar_events_from_json(CALDAV_URL, USERNAME, PASSWORD, JSON)
|
||||||
|
|
Loading…
Reference in New Issue