-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.py
190 lines (171 loc) · 4.73 KB
/
parse.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import json
import requests
# https://registrar.mit.edu/sites/default/files/2022-08/classrooms_with_lecture_capture_fall_2022.pdf
open_learning_rooms = {
"2-131",
"2-142",
"2-190",
"6-120",
"34-101",
"35-225",
"E25-111",
"E25-117"
}
is_and_t_rooms = {
"1-135",
"1-150",
"1-190",
"2-105",
"3-133",
"3-270",
"3-333",
"3-370",
"4-145",
"4-149",
"4-153",
"4-159",
"4-163",
"4-231",
"4-237",
"4-249",
"4-261",
"4-265",
"4-270",
"4-370",
"5-134",
"5-234",
"9-354",
"16-160",
"24-115",
"24-121",
"26-100",
"32-124",
"32-141",
"32-144",
"32-155",
"33-419",
"37-212",
"56-114",
"56-154",
"66-144",
"66-168"
"E51-057",
"E51-361"
}
sloan_rooms = {
"E51-145",
"E51-149",
"E51-151",
"E51-315",
"E51-325",
"E51-335",
"E51-345",
"E51-372",
"E51-376",
"E51-395"
}
# term = '2022SP'
term = '2023FA'
url = f'http://coursews.mit.edu/coursews/?term={term}'
raw_text = requests.get(url).text
# hilariously, the json is invalid thanks to one class
fixed_text = raw_text.replace('"Making"', '"Making"')
raw_classes = json.loads(fixed_text)['items']
def parse_name(name):
if name[0] == "null":
return "null"
else:
first = name[1]
last = name[0][:-1]
return first + " " + last
def course_to_int(course_number_string):
prefix = course_number_string.split(".")[0]
try:
num = int(prefix)
return num
except ValueError:
return 25 # rank last, highest integer course number is 24
def parse_interval(interval):
if "-" not in interval:
if "." in interval:
start_hour = int(interval.replace(".", ":").split(":")[0])
end_hour = int(start_hour) + 1
return str(start_hour) + ":30" + "-" + str(end_hour) + ":30"
else:
start_hour = int(interval)
end = start_hour + 1
return str(start_hour) + "-" + str(end)
else:
return interval.replace(".", ":")
def parse_time(time):
if "EVE" in time:
days = time[0]
interval = time[2][1:]
return days + interval + "PM"
else:
return time[0]
def main():
by_class = {}
for c in raw_classes:
t = c["type"]
if t == "LectureSession":
course_number = c["section-of"]
room = c["timeAndPlace"].split(" ")[-1]
time = c["timeAndPlace"].split(" ")[:-1]
time = parse_time(time)
if course_number not in by_class and room in open_learning_rooms:
by_class[course_number] = {
"room": room,
"time": time,
"setup": "OPEN_LEARNING"
}
if course_number not in by_class and room in is_and_t_rooms:
by_class[course_number] = {
"room": room,
"time": time,
"setup": "IS&T"
}
if course_number not in by_class and room in sloan_rooms:
by_class[course_number] = {
"room": room,
"time": time,
"setup": "SLOAN"
}
for c in raw_classes:
t = c["type"]
if t == "Class":
course_number = c["id"]
if course_number in by_class:
in_charge = c["in-charge"].split(" ")
name = parse_name(in_charge)
by_class[course_number]["in-charge"] = name
open_learning = []
is_and_t = []
sloan = []
for k, v in by_class.items():
if v["setup"] == "OPEN_LEARNING":
open_learning.append((k, v["room"], v["time"], v["in-charge"]))
elif v["setup"] == "IS&T":
is_and_t.append((k, v["room"], v["time"], v["in-charge"]))
elif v["setup"] == "SLOAN":
sloan.append((k, v["room"], v["time"], v["in-charge"]))
open_learning.sort(key=lambda x: course_to_int(x[0]))
is_and_t.sort(key=lambda x: course_to_int(x[0]))
sloan.sort(key=lambda x: course_to_int(x[0]))
# sloan.sort(key=lambda x: x[1])
print("Classes in Open Learning's auto-capture rooms:")
for c in open_learning:
num, loc, time, prof = c
print(f"{num : <10}{loc : <10}{time : <15}{prof : <30}")
print()
print("Classes in IS&T's auto-capture rooms:")
for c in is_and_t:
num, loc, time, prof = c
print(f"{num : <10}{loc : <10}{time : <15}{prof : <30}")
print()
print("Classes in Sloan's rooms:")
for c in sloan:
num, loc, time, prof = c
print(f"{num : <10}{loc : <10}{time : <15}{prof : <30}")
if __name__ == "__main__":
main()