-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
116 lines (73 loc) · 3.16 KB
/
parser.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
import sys
import json
# Super janky script to parse right_student_schedule.jsp and save as readable json
def main():
outputContent = {}
try:
inputFile = sys.argv[0]
outputFile = sys.argv[1]
except:
inputFile = 'right_student_schedule.jsp'
outputFile = 'output.json'
with open(inputFile,'r') as f:
line = 0 # Start at line 0
hitSchedules = False
# Go through all lines of file
for x in f.readlines():
line += 1 # Increment line counter
if 'class="tab' in x: # Skip all the unnecessary stuff
hitSchedules = True
continue
# Skip other unnecessary stuff
if not hitSchedules or not 'href="right_student_schedule.jsp?lesson=' in x: continue
# Prepare entry to add to dict
entry = {}
# Extract weeks active
weeks = []
sectionStartIndex = x.find('Veckor') + 8 # Find start of week section
sectionEndIndex = x.find('<',sectionStartIndex) # Find end of week section
for y in x[sectionStartIndex:sectionEndIndex].split(', '): # Split string of extracted weeks
if '-' in y: # If processed week is a range, add all weeks in range
z = y.split('-')
for n in range(int(z[0]), int(z[1])+1):
weeks.append(n)
else: # If not a range, just add the week
weeks.append(int(y))
weeks.sort()
# Extract lesson ID
lesson_id = 0
sectionStartIndex = x.find('lesson=') + 7
sectionEndIndex = x.find('&',sectionStartIndex)
lesson_id = x[sectionStartIndex:sectionEndIndex]
# Extract class name
name = ''
sectionStartIndex = x.find('title="">') + 16
sectionEndIndex = x.find('<',sectionStartIndex)
name = x[sectionStartIndex:sectionEndIndex]
# Extract class times
classStart = '00:00'
classEnd = '00:00'
sectionStartIndex = x.find('>',sectionEndIndex) + 1
sectionEndIndex = x.find('<',sectionStartIndex)
y = x[sectionStartIndex:sectionEndIndex].split('-')
classStart = y[0]
classEnd = y[1]
# Extract room id
room = '0'
sectionStartIndex = x.find('>',sectionEndIndex) + 1
sectionEndIndex = len(x)
room = x[sectionStartIndex:sectionEndIndex].strip('\n')
if name == 'Lunch': room = '' # Prevent weird extraction of non-existent lunch room
# Add all extracted values to entry
entry['name'] = name
entry['room'] = room
entry['day'] = "" # No idea how to do this
entry['starts'] = classStart
entry['ends'] = classEnd
entry['weeks'] = weeks
outputContent[lesson_id] = entry
f.close()
with open(outputFile, 'w') as file:
json.dump(outputContent, file, indent=4)
if __name__ == '__main__':
main()