forked from jansen-craft/cs2ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs2ics.py
47 lines (38 loc) · 1.68 KB
/
cs2ics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from icalendar import Calendar, Event
import requests
from datetime import datetime, timedelta
DAY_MAP = {"M": "MO", "T": "TU", "W": "WE", "R": "TH", "F": "FR"}
TERM = "Fall%20Semester%202024-25"
COOKIE = ""
response = requests.get(
f'https://ohio.collegescheduler.com/api/term-data/{TERM}',
headers={'Cookie': COOKIE}
)
if("text/html" in response.headers['Content-Type']):
raise Exception("Error: You need to refresh the COOKIE")
current_sections = response.json()["currentSections"]
calendar = Calendar()
for section in current_sections:
title = section["title"]
course = f'{section["subject"]} {section["course"]}'
instructor = section["instructor"][0]["name"] if section["instructor"] else "N/A"
meeting = section["meetings"][0]
days = meeting["daysRaw"]
startTime = datetime.strptime(str(meeting["startTime"]), "%H%M").time()
endTime = datetime.strptime(str(meeting["endTime"]), "%H%M").time()
startDate = datetime.strptime(str(meeting["startDate"]), "%Y-%m-%dT%H:%M:%S%z")
endDate = datetime.strptime(str(meeting["endDate"]), "%Y-%m-%dT%H:%M:%S%z")
room = meeting["building"]
start = datetime.combine(startDate, startTime)
end = datetime.combine(startDate, endTime)
bydays = [DAY_MAP[day] for day in DAY_MAP if day in days]
event = Event()
event.add('summary', f"{course} {title}")
event.add('dtstart', start)
event.add('dtend', end)
event.add('location', room)
event.add('description', f"Instructor: {instructor}")
event.add('RRULE', {'FREQ':["WEEKLY"], 'UNTIL':endDate + timedelta(days=1), 'BYDAY':bydays})
calendar.add_component(event)
with open('classes.ics', 'wb') as f:
f.write(calendar.to_ical())