-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubc.py
More file actions
263 lines (218 loc) · 8.67 KB
/
ubc.py
File metadata and controls
263 lines (218 loc) · 8.67 KB
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
"""Provides various functions for handling UBC course schedule HTML scraping"""
import re
import bs4
import requests
COURSE_RE = re.compile(r'course=(\d{2,3}\w?)')
DEPARTMENT_RE = re.compile(r'dept=(\w{3,4})')
CREDITS_RE = re.compile(r'Credits:\s+(\d{1,2})')
PREREQ_RE = re.compile(r'Pre-reqs:\s+(.+)')
COREQ_RE = re.compile(r'Co-reqs:\s+(.+)')
def extract_field(field, url_endpoint, default=None):
"""Extrat a field from an URL endpoint.
Args:
field (str): Name of the field to extract. Supports 'course', 'department'.
url_endpoint (str): URL endpoint.
Returns:
str: Value of the 'field' or default if not found.
"""
if field is 'course':
match = COURSE_RE.search(url_endpoint)
elif field is 'department':
match = DEPARTMENT_RE.search(url_endpoint)
else:
return default
return match.group(1) if match else default
class CourseScrapper(object):
"""UBC Course Schedule Scrapping Session."""
BASE_URL = 'https://courses.students.ubc.ca'
REST_BASE_URL = BASE_URL + '/cs/courseschedule?'
PNAME = 'subjarea'
TNAME = 'subj-course'
def __init__(self, sessyr, sesscd):
"""
Args:
sessyr (str): Academic year.
sesscd (str): Academic session - 'W' for Winter, 'S' for Summer.
"""
self.rsession = requests.Session()
self.rsession.params = {
'pname': self.PNAME,
'tname': self.TNAME,
'sessyr': sessyr,
'sesscd': sesscd
}
def is_full(self, course):
"""Checks if a course with section is full or not.
Args:
course (str): In the form of <DEPARTMENT> <COURSE #> <SECTION #>.
e.g. CPSC 221 101
Returns:
boolean: True if the course is full, False otherwise.
Raises:
ValueError: 'course' is in an invalid format.
HttpError: Error on HTTP request.
"""
parsed = course.split()
# TODO: better error checking here
if len(parsed) != 3:
raise ValueError(
"String must be in the form of '<DEPARTMENT> <COURSE #> <SECTION #>'")
url_params = {
'dept': parsed[0],
'course': parsed[1],
'section': parsed[2]
}
response = self.rsession.get(self.REST_BASE_URL, params=url_params)
response.raise_for_status()
if 'Total Seats Remaining' not in response.text:
raise Exception(
"URL doesn't contain seat information: {}".format(response.url))
return 'Note: this section is full' in response.text
def dept_links(self):
"""Get the deparments that are offering courses in this year and session.
Returns:
list: List of links to UBC departments.
"""
url_params = {'tname': 'subj-all-departments'}
response = self.rsession.get(self.REST_BASE_URL, params=url_params)
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text, "lxml")
return [self.BASE_URL + a['href']
for a in soup.find_all('a', href=True)
if extract_field('department', a['href'])]
def course_links_from_dept(self, department, in_format='name'):
"""See what courses a department offers.
Args:
department (str): Department name or link.
in_format (str): Format of 'deparment' param. Supports 'name' or 'link'.
Returns:
list: List of links to UBC courses.
Raises:
ValueError: Invalid 'in_format'.
"""
if in_format == 'name':
url_params = {
'tname': 'subj-department',
'dept': department
}
response = self.rsession.get(self.REST_BASE_URL, params=url_params)
elif in_format == 'link':
response = self.rsession.get(department)
else:
raise ValueError('in_format not implemented')
soup = bs4.BeautifulSoup(response.text, "lxml")
return [self.BASE_URL + a['href']
for a in soup.find_all('a', href=True)
if extract_field('course', a['href'])]
def extract_course_meta(self, course_link):
"""Extracts title and credits about a course such as CPSC 221.
Args:
course (str): Course link.
Returns:
dict: Course meta information (title and credits).
"""
response = self.rsession.get(course_link)
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text, "lxml")
return self._extract_course_meta_from_soup(soup)
def extract_course_info(self, course, in_format='name'):
"""Extracts information about a course such as CPSC 221.
Args:
course (str): Course name or link.
in_format (str): Format of 'course' param. Supports 'name' or 'link'.
Returns:
tuple: (course_name, dict)
inner dict of (section_name, dict of
status, activity, term, interval, days, start_time, end_time)
Raises:
ValueError: Invalid 'in_format'.
"""
if in_format == 'name':
course_name = course
tmp = course.split()
query_params = {
'tname': 'subj-course',
'dept': tmp[0],
'course': tmp[1]
}
response = self.rsession.get(
self.REST_BASE_URL, params=query_params)
elif in_format == 'link':
course_name = '{0} {1}'.format(
extract_field('department', course),
extract_field('course', course)
)
response = self.rsession.get(course)
else:
raise ValueError('in_format not implemented')
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text, "lxml")
trs = soup.find_all(
'tr', attrs={"class": ["section3", "section2", "section1", "section"]})
# tuple: (status, section, activity, term, interval, days, start_time, end_time, comments)
course_sections_info = [tuple(td.text.strip()
for td in tr.find_all('td')) for tr in trs]
parsed_info = self._parse_course_sections(course_sections_info)
parsed_info.update(self._extract_course_meta_from_soup(soup))
return (course_name, parsed_info)
def _extract_course_meta_from_soup(self, soup):
"""Extracts title, credits, pre-reqs, co-reqs from soup.
Args:
soup (BeautifulSoup): The HTML.
Returns:
dict: Course meta information (title and credits).
"""
title = soup.find('h4').text
paragraphs = soup.find_all('p')
creds = pre_reqs = co_reqs = None
for p in paragraphs:
text = p.text
match = CREDITS_RE.search(text)
if match:
creds = int(match.group(1))
continue
match = PREREQ_RE.search(text)
if match:
pre_reqs = match.group(1).strip()
continue
match = COREQ_RE.search(text)
if match:
co_reqs = match.group(1).strip()
return {
'title': title,
'credits': creds,
'pre_reqs': pre_reqs,
'co_reqs': co_reqs
}
def _parse_course_sections(self, course_sections_info):
"""
Args:
course_sections_info (list): List of tuples of course section information.
Returns:
dict: dictionary of 'section name (str)' to data about the section.
"""
course_info = {}
for section_info in course_sections_info:
(status, section, activity, term, interval, days,
start_time, end_time, comments) = section_info
# new section
if section != '':
last_section = section
course_info[last_section] = {
'status': status,
'activity': [activity],
'term': [term],
'interval': interval,
'days': [days],
'start_time': [start_time],
'end_time': [end_time]
}
# attached to the last section
else:
section_info = course_info[last_section]
section_info['activity'].append(activity)
section_info['term'].append(term)
section_info['days'].append(days)
section_info['start_time'].append(start_time)
section_info['end_time'].append(end_time)
return course_info