This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
160 lines (112 loc) · 3.8 KB
/
app.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
# Import request module
import urllib
from bs4 import BeautifulSoup
import json
from flask import Flask
import time
app = Flask(__name__)
def parse_peroid(peroid):
# Get all the detialed data from the given period
type = peroid["type"]
instructor = peroid["instructor"]
start = peroid["start"]
end = peroid["end"]
location = peroid["location"]
days = peroid.find_all("day")
# Deal with instructors.
instructor = instructor.split("/")
result = list()
for day in days:
# Create a Dictionary to store the data for different days.
D = dict()
day = int(day.text)+1 # Remember to increase the day value by 1
D["type"] = type
D["instructor"] = instructor
D["start"] = start
D["end"] = end
D["location"] = location
D["day"] = day
# Append to the result list
result.append(D)
return result
def parse_section(section):
crn = section["crn"]
shortname = section["num"]
seats_taken = section["students"]
seats = section["seats"]
# Find all the sub-periods
periods = section.find_all("period")
# Create a list to store the periods after analyzing
periods_list = list()
# Store the professors that teach this course.
instructors_set = set()
for period in periods:
# Fetch and save all the period information.
parse_peroid_result = parse_peroid(period)
periods_list = periods_list + parse_peroid_result
# Deal with the professor set.
for single_period in parse_peroid_result:
for instructor in single_period["instructor"]:
if(instructor != "Staff"):
instructors_set.add(instructor)
# Turn set to a list finnally.
instructors_list = list(instructors_set)
result = dict()
result["crn"] = crn
result["shortname"] = shortname
result["seats_taken"] = seats_taken
result["seats"] = seats
result["instructors"] = instructors_list
result["periods"] = periods_list
return result
def parse_course(course):
longname = course["name"]
short_name = course["num"]
min_credits = course["credmin"]
max_credits = course["credmax"]
# Find all the sub sections
sections = course.find_all("section")
# create a list to store the parsed section information
sections_list = list()
for section in sections:
sections_list.append(parse_section(section))
result = dict()
result["longname"] = longname
result["shortname"] = short_name
result["min_credits"] = min_credits
result["max_credits"] = max_credits
result["sections"] = sections_list
return result
def fetch_data(URL):
# Try to fetch the data from the given URL,
# If the URL doesn't exisit, return "Incorrect URL"
try:
data=urllib.request.urlopen(URL).read()
except urllib.error.HTTPError as error:
return "Incorrect URL"
# Create a soup for the whole page.
soup = BeautifulSoup(data, 'lxml')
# Store all the sub-courses to courses.
courses = soup.find_all("course")
D = dict()
subjects = list()
# Deal with every course
for course in courses:
shortname = course["dept"]
if shortname not in D:
D[shortname] = list()
D[shortname].append(parse_course(course))
# Transform data type
for shortname in D:
subjects.append({"shortname": shortname, "listings": D[shortname]})
result = {"subjects": subjects}
return json.dumps(result)
@app.route('/<semester>')
def return_result(semester):
# Check if the semester is made of digits
if not semester.isdigit():
return "Incorrect URL"
URL = "https://sis.rpi.edu/reg/rocs/{}.xml".format(semester)
return fetch_data(URL)
if __name__ == '__main__':
app.run()