-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcgillTranscript.py
More file actions
97 lines (75 loc) · 2.57 KB
/
mcgillTranscript.py
File metadata and controls
97 lines (75 loc) · 2.57 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
import mechanicalsoup
import re
import shutil
import datetime
def generateDelta():
# Try to open file in append mode
# and open grade files to read
try:
delta_file = open("delta.txt", "a")
grade_file = open("grades.txt", "r")
grade_old_file = open("grades_old.txt", "r")
except IOError:
print("ERROR: IOError raised in generateDelta()")
exit(1)
grades = grade_file.readlines()
grades_old = grade_old_file.readlines()
# Remove common strings in the lists
# We are certainly not interested in those
delta_list = list(set(grades)^set(grades_old))
if delta_list:
delta_file.write(str(delta_list) + " " + str(datetime.datetime.now()) + '\n')
print("NEW GRADE: " + str(delta_list))
def getTranscript(browser, us, ps):
# Request login page
login_page = browser.get("https://horizon.mcgill.ca/pban1/twbkwbis.P_WWWLogin")
# Grab the form
login_form = login_page.soup.select('form[name="loginform1"]')[0]
# Set the fields
login_form.select("#mcg_un")[0]['value'] = us
login_form.select("#mcg_pw")[0]['value'] = ps
# Submit the form
formSubmit = browser.submit(login_form, login_page.url)
# Verify whether we are logged in
# The error message has an img with a name attribute of
# "web_stop"
assert not formSubmit.soup.select('img[name="web_stop"]')
# Goto the transcript page
t_pg = browser.get("https://horizon.mcgill.ca/pban1/bzsktran.P_Display_Form?user_type=S")
# Return the transcript page
return t_pg
def parseCourses(page):
pass
# Get the username and password
with open("ignore/data.txt", "r") as data:
# Exclude the last character - it is a '\n'
username = data.readline()[:-1]
password = data.readline()[:-1]
data.close()
# Main program
browser = mechanicalsoup.Browser()
t_page = getTranscript(browser, username, password)
# Verify correct response
if (t_page.status_code != 200):
print("ERROR: Cannot access the transcript page")
exit(1)
courses = []
# All the relevant data is stored in span tags with
# attribute fieldmediumtext
spans = t_page.soup.select("span.fieldmediumtext")
# Extract the text without the tags and replace \xa0 character with ''
textlist = [x.getText().replace('\xa0', '') for x in spans]
# Copy old file
try:
shutil.copyfile('grades.txt', 'grades_old.txt')
except IOError:
print("No old grade file found")
# Open file to save all the grades
grade_file = open("grades.txt", "w")
# Loop through all the lines of data
for i in range(len(textlist)):
if re.search(r'\b[A-Z]{4}\s[0-9]{3}\b', textlist[i]):
gradestring = textlist[i] + " " + textlist[i+4]
grade_file.write(gradestring + "\n")
grade_file.close()
generateDelta()