-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
121 lines (94 loc) · 4.13 KB
/
main.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
import pickle
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
import secrets
from utils import load_main_page, wait, click
from caesar_authentication import authenticate, is_authenticated
def load_cookies(driver):
driver.get('https://www.northwestern.edu/')
wait(driver, 'department', 5)
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
if '.northwestern.edu' == cookie['domain']:
driver.add_cookie(cookie)
def authenticate_if_needed(driver):
if not is_authenticated(driver):
authenticate(driver, secrets.netid, secrets.password)
def open_manage_classes(driver):
load_main_page(driver, 10)
button = wait(driver, 'win0divPTNUI_LAND_REC_GROUPLET$6', 15)
click(driver, button)
sleep(1)
def parse_shopping_cart(driver):
driver.get('https://caesar.ent.northwestern.edu/psc/CS857PRD/EMPLOYEE/SA/c/SA_LEARNER_SERVICES_2.SSR_SSENRL_CART.GBL?pslnkid=NW_SS_SA_SHOPPING_CART&Page=SSR_SSENRL_CART&ICAGTarget=start')
button = wait(driver, 'SSR_DUMMY_RECV1$sels$1$$0', 5)
click(driver, button)
button = wait(driver, 'DERIVED_SSS_SCT_SSR_PB_GO', 10)
click(driver, button)
sleep(1)
iframe = wait(driver, 'ptifrmtgtframe', 10)
driver.get(iframe.get_attribute("src"))
sleep(1)
table = wait(driver, 'SSR_REGFORM_VW$scroll$0', 10)
outer_table = table.find_element_by_tag_name('table')
rows = outer_table.find_elements_by_tag_name('tr')
results = []
for row in rows:
if 'trSSR_REGFORM_VW' not in row.get_attribute('id'):
continue
try:
elements = row.find_elements_by_tag_name('td')
number = elements[1].text.split('\n (')[-1][:-1]
results.append(number)
except:
pass
print('class numbers: ' + str(results))
return results
def open_search_classes_results(driver, class_num):
driver.get('https://caesar.ent.northwestern.edu/psc/CS857PRD/EMPLOYEE/SA/c/SA_LEARNER_SERVICES.CLASS_SEARCH.GBL?pslnkid=NW_SS_SA_CLASS_SEARCH&ICAGTarget=start')
button = wait(driver, 'DERIVED_CLSRCH_SSR_EXPAND_COLLAPS$149$$1', 10)
click(driver, button)
class_field = wait(driver, 'SSR_CLSRCH_WRK_CLASS_NBR$8', 10)
class_field.send_keys(str(class_num))
button = wait(driver, 'CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH', 5)
click(driver, button)
button = wait(driver, 'MTG_CLASSNAME$0', 10)
click(driver, button)
def parse_class_page(driver):
table = wait(driver, 'ACE_SSR_CLS_DTL_WRK_GROUP3', 10)
rows = table.find_elements(By.TAG_NAME, "tr")
results = {}
for row in rows:
try:
elements = row.find_elements(By.TAG_NAME, "td")
key = elements[1].text.replace('\n', '')
value = elements[2].text.replace('\n', '')
results[key] = int(value)
except:
pass
title = driver.find_element_by_id('DERIVED_CLSRCH_DESCR200').text.replace('\xa0', '')
time = driver.find_element_by_id('MTG_SCHED$0').text.replace('\xa0', '')
units = driver.find_element_by_id('win0divSSR_CLS_DTL_WRK_UNITS_RANGE').text.replace('\n', '')
grading = driver.find_element_by_id('win0divGRADE_BASIS_TBL_DESCRFORMAL').text.replace('\n', '')
description = '{}\n{}, {}, Grading: {}'.format(title, time, units, grading)
if 'Available Seats' not in results:
print('{}\nmissing available seats\n'.format(description))
return
seats = results['Available Seats']
if 'Class Capacity' in results:
print('{}\nOpen Seats: {} / {}\n'.format(description, seats, results['Class Capacity']))
elif 'Combined Section Capacity' in results:
print('{}\nOpen Seats: {} / {}\n'.format(description, seats, results['Combined Section Capacity']))
else:
print('{}\nmissing capacity\n'.format(description))
driver = webdriver.Safari()
load_cookies(driver)
authenticate_if_needed(driver)
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
class_numbers = parse_shopping_cart(driver)
print('results:\n')
for class_num in class_numbers:
open_search_classes_results(driver, class_num)
parse_class_page(driver)
driver.quit()