-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_waitlists.py
executable file
·301 lines (252 loc) · 6.89 KB
/
get_waitlists.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from bs4 import BeautifulSoup
import json
def get_waitlists(code, course):
# Parsing HTML from requested class
soup = BeautifulSoup(code, "html.parser")
# Create list to store information
course_json = "./data/" + course + ".json"
classFile = open(course_json, 'w')
class_data = []
class_data.append(course)
# Obtain class data
for td in soup.findAll('td',{'class':'brdr'}):
text = td.text
# Taking care of special characters
if "\n" in text:
text = text.replace("\n", "")
if "\t" in text:
text = text.replace("\t", "")
# Adding data to list
text = text.strip()
class_data.append(text)
# Get rid of empty strings within
class_data = list(filter(None, class_data))
classFile.write('[{"name": ' + '"' + course + '", "sections": [{')
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
buildBool = False
roomBool = False
waitBool = False
idBool = False
profBool = False
cancelBool = False
lectureBool = True
sectionDone = False
discBool = False
finalBool = False
dateBool = False
for i, w in enumerate(class_data):
if i == 0:
continue
if w == "FI":
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
buildBool = False
roomBool = False
waitBool = False
idBool = False
profBool = False
cancelBool = False
lectureBool = False
sectionDone = False
discBool = False
classFile.write(', {"type": "Final"')
finalBool = True
continue
# Final parsing
if finalBool == True:
if dateBool == False:
class_data[i] = ', "Date": ' + '"' + w + '"'
dateBool = True
elif daysBool == False:
class_data[i] = ', "Day": ' + '"' + w + '"'
daysBool = True
elif timeBool == False:
class_data[i] = ', "Time": ' + '"' + w + '"'
timeBool = True
elif buildBool == False:
class_data[i] = ', "Building": ' + '"' + w + '"'
buildBool = True
elif roomBool == False:
classFile.write(', "Room": ' + '"' + w + '"}')
roomBool = False
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
buildBool = False
waitBool = False
idBool = False
profBool = False
cancelBool = False
lectureBool = False
sectionDone = False
discBool = False
finalBool = False
dateBool = False
sectionDone = True
continue
# Reset booleans if cancelled
if w == "Cancelled":
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
buildBool = False
roomBool = False
waitBool = False
idBool = False
cancelBool = True
lectureBool = False
classFile.write(', "Cancelled": "True"')
continue
if w == "SE":
if idBool == False:
class_data[i] = '"type": "Seminar"'
print "memes"
else:
class_data[i] = ', "type": "Seminar"'
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
buildBool = False
roomBool = False
waitBool = False
profBool = False
lectureBool = False
# Set lecture boolean to skip some categories
elif w == "LE":
if idBool == False and sectionDone == False:
class_data[i] = '"type": "Lecture"'
elif sectionDone == True:
class_data[i] = ', { "type": "Lecture"'
sectionDone = False
else:
class_data[i] = ', "type": "Lecture"'
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
roomBool = False
waitBool = False
profBool = False
lectureBool = True
buildBool = False
##### MAYBE HAVE A DISCUSSION CHECK FOR NO INSTRUCTOR
elif w == "DI":
# Following an ID
if idBool == True:
class_data[i] = ', "type": "Discussion"'
# Starting new section
elif sectionDone == True or discBool == True:
class_data[i] = '}, {"type": "Discussion"'
lectureBool = False
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
roomBool = False
waitBool = False
profBool = False
buildBool = False
discBool = True
elif w == "LA":
if idBool == False:
class_data[i] = '"type": "Lab"'
else:
class_data[i] = ', "type": "Lab"'
lectureBool = False
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
waitBool = False
profBool = False
buildBool = False
elif finalBool == False:
# Section ID code requirements
if w.isdigit() and len(w) == 6 and idBool == False:
# After a discussion
if roomBool == True:
classFile.write("}, {")
# After a lecture or cancellation
elif cancelBool == True or lectureBool == True:
classFile.write(", {")
cancelBool = False
class_data[i] = '"sectionID": ' + '"' + w + '"'
idBool = True
elif len(w) == 3 and sectionBool == False:
class_data[i] = ', "Section": ' + '"' + w + '"'
sectionBool = True
elif daysBool == False:
class_data[i] = ', "Days": ' + '"' + w + '"'
daysBool = True
elif timeBool == False:
class_data[i] = ', "Time": ' + '"' + w + '"'
timeBool = True
elif buildBool == False:
class_data[i] = ', "Building": ' + '"' + w + '"'
buildBool = True
elif roomBool == False:
class_data[i] = ', "Room": ' + '"' + w + '"'
roomBool = True
# If reading a lecture, reset for next JSON category
elif profBool == False:
classFile.write(', "Instructor": ' + '"' + w + '"')
if lectureBool == True:
idBool = False
sectionBool = False
roomBool = True
daysBool = False
timeBool = False
waitBool = False
profBool = False
buildBool = False
sectionDone = True
else:
profBool = True
continue
elif waitBool == False:
# Extract actual numbers if class is full
if "FULL" in w:
count = w[w.find("(")+1:w.find(")")]
class_data[i] = ', "Waitlist": ' + '"' + count + '"'
else:
class_data[i] = ', "Available Seats": ' + '"' + w +'"'
waitBool = True
else:
class_data[i] = ', "Seat Limit": ' + '"' + w + '"}'
cancelBool = True
idBool = False
sectionBool = False
roomBool = False
daysBool = False
timeBool = False
roomBool = False
waitBool = False
profBool = False
buildBool = False
sectionDone = True
classFile.write(class_data[i])
# Fill JSON file with data
#json.dump(class_data, classFile)
classFile.write("]}]")
classFile.close()
''' Might actually not need this...
# Remove line break that contains the waitlist
# information for data extraction
for br in soup.findAll('br'):
br.extract()
# Getting full Waitlist string
for span in soup.findAll('span'):
st = span.text
# Obtaining waitlist count
if "Waitlist(" in st:
count = st[st.find("(")+1:st.find(")")]
print count
'''