This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syllabus.py
345 lines (299 loc) · 12 KB
/
syllabus.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------
# Syllabus module of Nobo.
# Fang2hou @ 2/7/2018
# github: https://github.com/fang2hou/Nobo
# ----------------------------------------
import requests
import re
import json
from bs4 import BeautifulSoup as bs
from .lib import fixja
class syllabusUser(object):
"""syllabus Class"""
# Setting for syllabus
syllabusHomePagePath = "https://campusweb.ritsumei.ac.jp/syllabus/sso/KJgSearchTop.do"
def __init__(self, username, password):
self.rainbowID = username
self.rainbowPassword = password
self.webSession = requests.Session()
self.syllabusList = {}
def login(self):
# Load first page using the webSession initialized before
firstPage = self.webSession.get(self.syllabusHomePagePath)
# Initialize the post data for second page
secondPagePostData = {
"USER": self.rainbowID,
"PASSWORD": self.rainbowPassword
}
# Update the post data using the given information
for inputElement in bs(firstPage.text, "html.parser").find_all('input'):
tempAttrDict = inputElement.attrs
if "name" in tempAttrDict and "value" in tempAttrDict:
if tempAttrDict["name"] == "target":
secondPagePostData["target"] = tempAttrDict["value"]
elif tempAttrDict["name"] == "smauthreason":
secondPagePostData["smauthreason"] = tempAttrDict["value"]
elif tempAttrDict["name"] == "smquerydata":
secondPagePostData["smquerydata"] = tempAttrDict["value"]
elif tempAttrDict["name"] == "postpreservationdata":
secondPagePostData["postpreservationdata"] = tempAttrDict["value"]
# Load second page
secondPage = self.webSession.post("https://sso.ritsumei.ac.jp/cgi-bin/pwexpcheck.cgi", secondPagePostData)
# Get the redirect path
thirdPagePath = "https://sso.ritsumei.ac.jp" + bs(secondPage.text, "html.parser").find('form').attrs["action"]
# Create the post data for second page
thirdPagePostData = {
"USER": self.rainbowID,
"PASSWORD": self.rainbowPassword,
"smquerydata": secondPagePostData["smquerydata"],
}
# Load third page
self.webSession.post(thirdPagePath, thirdPagePostData)
def getSyllabusById(self, courseYear, courseID):
self.syllabusList = {}
syllabusAllInfoPagePath = "https://syllabus.ritsumei.ac.jp/syllabus/sso/SyShowAll.do"
# Get all information about this course with (sylNendo, jyugyocd, kinouID)
postData = {
"mkjgkbcd": "",
"daibuncd": "",
"tyubuncd": "",
"shobuncd": "",
"sylNendo": courseYear,
"jyugyocd": courseID,
"kinouID": 'SKJ070',
}
# Save page source
syllabusPage = self.webSession.post(syllabusAllInfoPagePath, postData)
syllabusPageHeadTag = bs(syllabusPage.text, "html.parser")
# FOR DEBUG
# ---------------------------------
# # get file and save -------------
# syllabusPage = self.webSession.post(syllabusAllInfoPagePath, postData)
# with open("temp.html", "w+") as f:
# f.write(syllabusPage.text)
# return
# # fake data ---------------------
# with open("temp.html", "r") as inputFile:
# syllabusPageHeadTag = bs(inputFile.read(), "html.parser")
# Start of processing
# Basic
basicInfoTag = syllabusPageHeadTag.select(".jugyo_table")[0].find("tr").find_next_sibling("tr").find("td")
courseNameString = fixja.remove_last_space(basicInfoTag.get_text().strip("\t\n "))
courseNameString = fixja.convet_to_half_width(courseNameString)
basicInfo = []
if " " in courseNameString:
courseNames = courseNameString.split(" ")
for courseName in courseNames:
courseClass = re.findall(r"\([A-Za-z][0-9]\)", courseName)[0].strip("()")
courseName = courseName.split(" (")[0]
basicInfo.append({
"name": courseName,
"class": courseClass
})
else:
courseName = courseNameString
courseClass = re.findall(r"\([A-Za-z][0-9]\)", courseName)[0].strip("()")
courseName = courseName.split(" (")[0]
basicInfo.append({
"name": courseName,
"class": courseClass
})
# Semester
semesterTag = basicInfoTag.find_next_sibling("td")
semesterString = semesterTag.get_text()
if "前期" in semesterString:
courseSemester = "spring"
elif "後期" in semesterString:
courseSemester = "fall"
else:
courseSemester = "unknown"
# Course Time
courseTimeTag = semesterTag.find_next_sibling("td")
courseTimeString = courseTimeTag.get_text().replace("\t", "").replace("\n", "")
courseWeekday, courseArtPeriod, courseSciPeriod = re.findall(r"([月|火|水|木|金])([0-9]-[0-9]|[0-9])(\([0-9]-[0-9]\))", courseTimeString)[0]
courseWeekday = fixja.convert_week_to_en(courseWeekday)
courseSciPeriod = courseSciPeriod.strip("()")
courseTime = {
"year": courseYear,
"semester": courseSemester,
"weekday": courseWeekday,
"period_art": courseArtPeriod,
"period_sci": courseSciPeriod,
}
# Credit
creditTag = courseTimeTag.find_next_sibling("td")
credit = int(creditTag.get_text())
# Teacher
courseTeacherTag = creditTag.find_next_sibling("td")
courseTeacherString = courseTeacherTag.get_text()
courseTeacherString = fixja.remove_last_space(courseTeacherString.replace("\t", "").replace("\n", "").strip(" "))
# Confirm if there are several teachers in list
if "、" in courseTeacherString:
hasSeveralTeachers = True
else:
hasSeveralTeachers = False
if hasSeveralTeachers:
courseTeacher = courseTeacherString.split("、")
else:
courseTeacher = [courseTeacherString]
# Course Outline and Method
outlineTag = syllabusPageHeadTag.find("dl")
outline = fixja.remove_last_space(outlineTag.find("dd").get_text())
# Student Attainment Objectives
objectivesTag = outlineTag.find_next_sibling("dl")
objectives = ""
for subString in objectivesTag.find("dd").find_next_sibling("dd").contents:
if str(subString) != "<br/>":
objectives += subString.replace("\n", "").replace("\t", "")
else:
objectives += "\n"
# Recommended Preparatory Course
preCourseTag = objectivesTag.find_next_sibling("dl")
precourse = fixja.remove_last_space(preCourseTag.find("dd").get_text())
# Course Schedule
scheduleTableTag = preCourseTag.find_next_sibling("dl")
tempScheduleTableTag = scheduleTableTag.find("table").find("tr").find_next_sibling("tr")
scheduleList = []
while tempScheduleTableTag:
# Lecture
tempSchedule = {"lecture": int(tempScheduleTableTag.find("td").get_text()), "theme": fixja.remove_last_space(
fixja.convet_to_half_width(tempScheduleTableTag.find("td").find_next_sibling("td").get_text()))}
# Theme
tempScheduleTableTag = tempScheduleTableTag.find_next_sibling("tr")
# Keyword, References and Supplementary Information
tempSchedule["references"] = fixja.remove_last_space(fixja.convet_to_half_width(tempScheduleTableTag.find("td").get_text()))
tempScheduleTableTag = tempScheduleTableTag.find_next_sibling("tr")
scheduleList.append(tempSchedule)
# Recommendations for Private Study
recommendationTag = scheduleTableTag.find_next_sibling("dl")
recommendation = fixja.remove_last_space(recommendationTag.find("dd").get_text())
# Grade Evaluation Method
gradeEvaluationTag = recommendationTag.find_next_sibling("dl")
gradeEveluationTableTag = gradeEvaluationTag.find("table")
gradeEvaluation = {
"note": "",
"data": [],
}
gradeType = [
"End of Semester Examination (Written)",
"Report Examination",
"Other"
]
gradeTypeIndex = 0
for row in gradeEveluationTableTag.find_all("tr")[1:]:
percentage = int(row.select(".percentage")[0].get_text().replace("%", ""))
gradeNote = fixja.remove_last_space(row.select(".top")[0].get_text())
gradeEvaluation["data"].append({
"type": gradeType[gradeTypeIndex],
"percentage": percentage,
"note": gradeNote
})
gradeTypeIndex += 1
gradeEvaluationNoteTag = gradeEveluationTableTag.find_next_sibling("dl")
gradeEvaluationNote = gradeEvaluationNoteTag.find("dd").get_text()
gradeEvaluationNote = fixja.remove_last_space(gradeEvaluationNote)
gradeEvaluation["note"] = gradeEvaluationNote
# Advice to Students on Study and Research Methods
adviceTag = gradeEvaluationTag.find_next_sibling("dl")
advice = fixja.remove_last_space(adviceTag.find("dd").get_text())
# Textbooks
textBookTag = adviceTag.find_next_sibling("dl")
textBooks = {
"note": "",
"book": []
}
textBooksTable = textBookTag.find("table")
if textBooksTable:
for row in textBooksTable.find_all("tr")[1:]:
tempTextBook = {}
tempTag = row
tempTag = tempTag.find("td")
tempTextBook["title"] = fixja.remove_last_space(tempTag.get_text()).replace("\n", "")
tempTag = tempTag.find_next_sibling("td")
tempTextBook["author"] = fixja.remove_last_space(tempTag.get_text())
tempTag = tempTag.find_next_sibling("td")
tempTextBook["publisher"] = fixja.remove_last_space(tempTag.get_text())
tempTag = tempTag.find_next_sibling("td")
tempTextBook["ISBN"] = fixja.remove_last_space(tempTag.get_text()).replace("ISBN","")
tempTag = tempTag.find_next_sibling("td")
tempTextBook["comment"] = fixja.remove_last_space(tempTag.get_text())
textBooks["book"].append(tempTextBook)
textBooks["note"] = fixja.remove_last_space(textBookTag.find_all("dd", class_="nest")[0].get_text())
# Reference Books
refBookTag = textBookTag.find_next_sibling("dl")
refBooks = {
"note": "",
"book": []
}
refBooksTable = refBookTag.find("table")
if refBooksTable:
for row in refBooksTable.find_all("tr")[1:]:
tempRefBook = {}
tempTag = row
tempTag = tempTag.find("td")
tempRefBook["title"] = fixja.remove_last_space(tempTag.get_text()).replace("\n", "")
tempTag = tempTag.find_next_sibling("td")
tempRefBook["author"] = fixja.remove_last_space(tempTag.get_text())
tempTag = tempTag.find_next_sibling("td")
tempRefBook["publisher"] = fixja.remove_last_space(tempTag.get_text())
tempTag = tempTag.find_next_sibling("td")
tempRefBook["ISBN"] = fixja.remove_last_space(tempTag.get_text()).replace("ISBN","")
tempTag = tempTag.find_next_sibling("td")
tempRefBook["comment"] = fixja.remove_last_space(tempTag.get_text())
refBooks["book"].append(tempRefBook)
refBooks["note"] = fixja.remove_last_space(refBookTag.find_all("dd", class_="nest")[0].get_text())
# Web Pages for Reference
refPageTag = refBookTag.find_next_sibling("dl")
refPageStrings = refPageTag.find("dd").contents
refPage = ""
for refPageString in refPageStrings:
afterString = fixja.remove_last_space(str(refPageString))
if afterString != "":
if "<a" in afterString:
refPage += re.findall(r">(.+)<", afterString)[0]
refPage += " "
elif afterString == "<br/>":
refPage = refPage[:-1] + "\n"
else:
refPage += afterString
refPage += " "
refPage = fixja.remove_last_space(refPage)
# How to Communicate with the Instructor In and Out of Class(Including Instructor Contact Information)
contactTag = refPageTag.find_next_sibling("dl")
contactString = contactTag.find("dd")
contactMethods = []
for contactMethod in re.findall(r"<b>(.*)/", str(contactString)):
contactMethods.append(fixja.remove_last_space(contactMethod))
# Other Comments
otherCommentsTag = contactTag.find_next_sibling("dl")
otherComments = fixja.remove_last_space(otherCommentsTag.find("dd").get_text())
# Save as dictionary
self.syllabusList = {
"basic": basicInfo,
"time": courseTime,
"teacher": courseTeacher,
"credit": credit,
"outline": outline,
"objectives": objectives,
"precourse": precourse,
"schedule": scheduleList,
"recommendation": recommendation,
"grade_evluation": gradeEvaluation,
"advice": advice,
"text_books": textBooks,
"ref_books": refBooks,
"ref_pages": refPage,
"contact_methods": contactMethods,
"other_comments": otherComments,
}
def outputAsJSON(self, outputPath):
if len(self.syllabusList) > 0:
# Output data if the user has got information of all courses
with open(outputPath, 'w+', encoding='utf8') as outfile:
# Fix Kanji issue, set indent as 4
json.dump(self.syllabusList, outfile, ensure_ascii=False, indent=4)
else:
# Notify when output without information of courses
print("Use the getSyllabusById() method to get data first.")