Created
February 7, 2025 02:56
-
-
Save minhqnd/63480067e7f492d5c15cb25ce3b1a253 to your computer and use it in GitHub Desktop.
Convert FAP FPT Calendar to ics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import datetime, timedelta | |
def create_event(data): | |
"""Format each event for the iCalendar file.""" | |
event_template = """BEGIN:VEVENT | |
DTSTART;TZID=Asia/Ho_Chi_Minh:{start_time} | |
DTEND;TZID=Asia/Ho_Chi_Minh:{end_time} | |
SUMMARY:{summary} | |
LOCATION:{location} | |
DESCRIPTION:{description} | |
END:VEVENT""" | |
# Parse the date and slot time | |
date = datetime.strptime(data['date'], '%m/%d/%Y %I:%M:%S %p') | |
start_time, end_time = data['slotTime'][1:-1].split('-') | |
start_datetime = datetime.strptime(f"{date.date()} {start_time}", '%Y-%m-%d %H:%M') | |
end_datetime = datetime.strptime(f"{date.date()} {end_time}", '%Y-%m-%d %H:%M') | |
# Format event data | |
event_data = { | |
"start_time": start_datetime.strftime('%Y%m%dT%H%M%S'), | |
"end_time": end_datetime.strftime('%Y%m%dT%H%M%S'), | |
"summary": f"{data['subjectCode']}", | |
"location": data['roomNo'], | |
"description": f"{data['lecturer']} - {data['groupName']} - Session: {data['sessionNo']} - Online: {data['isOnline']}" | |
} | |
return event_template.format(**event_data) | |
def create_calendar(json_file, output_file="calendar.ics"): | |
"""Generate an iCalendar file from the JSON data.""" | |
with open(json_file, 'r', encoding='utf-8') as f: | |
json_data = json.load(f) | |
calendar_header = """BEGIN:VCALENDAR | |
VERSION:2.0 | |
PRODID:-//My Calendar//EN""" | |
calendar_footer = "END:VCALENDAR" | |
events = [] | |
for item in json_data['data']: | |
events.append(create_event(item)) | |
# Write the calendar file | |
with open(output_file, 'w', encoding='utf-8') as f: | |
f.write(calendar_header + "\n") | |
f.write("\n".join(events) + "\n") | |
f.write(calendar_footer) | |
print(f"Calendar saved as {output_file}") | |
# Replace 'cal.txt' with your actual file name | |
create_calendar("cal.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment