-
Notifications
You must be signed in to change notification settings - Fork 2
/
reg_parse.py
executable file
·104 lines (78 loc) · 2.64 KB
/
reg_parse.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
# reg_parse.py: Interpret results and errors from the Quick Add/Drop interface
# This file is from Minervac, a command-line client for Minerva
# <http://npaun.ca/projects/minervac>
# (C) Copyright 2016-2017 Nicholas Paun
from bs4 import BeautifulSoup
import urllib
import sys
import codecs
from minerva_common import *
def quick_add_insert(text,crns):
html = BeautifulSoup(text,'html5lib')
forms = html.body.find_all('form')
reg = forms[1]
inputs = reg.find_all(['input','select'])
request = []
for input in inputs:
if not input.has_attr('name'):
if input.has_attr('id'):
print "A problem occurred:"
else:
continue
if input.has_attr('value'): #This should always fail for a select.
val = input['value']
else:
val = ''
if val == 'Class Search': #We want to register and not search,
continue
if crns and input['name'] == 'CRN_IN' and val == '': # Shove our CRN in the first blank field
val = crns.pop(0)
try:
request.append((input['name'], val))
except KeyError:
sys.exit(quick_add_issue("Wrong McGill ID or password."))
return urllib.urlencode(request)
def quick_add_status(text):
html = BeautifulSoup(text,'html5lib')
errtable = html.body.find('table',{'summary':'This layout table is used to present Registration Errors.'})
if errtable is not None:
error = errtable.findAll('td',{'class': "dddefault"})[0].a.text
if error.startswith("Open"):
print "* Must enter the waitlist section."
return MinervaError.reg_wait
else:
print "\033[1m* Failed to register: \033[0m " + str(error)
return MinervaError.reg_fail
print "\033[1m* Registration probably succeeded.\033[0m"
return MinervaError.reg_ok
def quick_add_issue(message):
print "\033[1m* Failed to register: \033[0m " + message + " [Message generated by Minervaclient.]"
return MinervaError.reg_fail
def quick_add_wait(text):
html = BeautifulSoup(text,'html5lib')
forms = html.body.find_all('form')
try:
reg = forms[1]
except IndexError:
sys.exit(quick_add_issue("Registration not open yet."))
inputs = reg.find_all(['input','select'])
request = []
actual_wait = False
for input in inputs:
if not input.has_attr('name'):
if input.has_attr('id'):
print "A problem occurred:"
else:
continue
if input.has_attr('value'): #This should always fail for a select.
val = input['value']
else:
val = ''
if input.has_attr('id') and input['id'].startswith('waitaction'):
val = 'LW'
actual_wait = True
request.append((input['name'], val))
if actual_wait:
return urllib.urlencode(request)
else:
return False