-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.py
152 lines (124 loc) · 5.45 KB
/
run.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
# UIUC COURSE GOD
# course registration cheat for UIUC
# support multiple crn for different courses
# author: Tianhao Chi
# usage: python run.py semester netid password crn1 crn2 ...
# use semester in this format: YYYY-spring, YYYY-summer, YYYY-fall. Example: 2021-spring
# note: do not log in into the system by yourself while using this program
# required package: bs4, selenium, chromedriver
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time
import sys
try:
import urllib.request as urllib2
except ImportError:
import urllib2
def construct_term_in(semester):
# This is dark magic.
year = semester[:semester.find('-')]
season = semester[semester.find('-')+1:]
if season == 'winter':
season_num = 0
if season == 'spring':
season_num = 1
if season == 'summer':
season_num = 5
if season == 'fall':
season_num = 8
return 120000 + int(str(year)[-2:])*10 + season_num
def get_remaining_seat(soup, cross_list):
if cross_list:
try:
# for cross list courses
remaining_seat = soup('th', attrs={'scope': 'row'})[
3].find_next_siblings('td')[2].string
except IndexError:
remaining_seat = soup('th', attrs={'scope': 'row'})[
1].find_next_siblings('td')[2].string
else:
remaining_seat = soup('th', attrs={'scope': 'row'})[
1].find_next_siblings('td')[2].string
return int(remaining_seat)
def refresh_course_website(driver, crn_arr, cross_list, term_in):
remaining_seat = 0
print("start refreshing ...")
# keep refreshing until find empty space
while True:
for crn in crn_arr:
# this link needs to be updated each semester!
url = 'https://ui2web1.apps.uillinois.edu/BANPROD1/bwckschd.p_disp_detail_sched?term_in=%d&crn_in=%s' % (
term_in, crn)
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
remaining_seat = get_remaining_seat(soup, cross_list)
if remaining_seat > 0:
print("refreshing done!")
return crn
def register(driver, crn):
# register single course
crn_blank = driver.find_element(By.ID, "crn_id1")
crn_blank.send_keys(crn)
driver.find_element(By.XPATH, "//input[@value='Submit Changes']").click()
driver.save_screenshot('screen.png')
def log_in(username, password, driver):
driver.get(login_url)
user_field = driver.find_element("name", "USER")
password_field = driver.find_element("name", "PASSWORD")
user_field.send_keys(username)
password_field.send_keys(password)
driver.find_element("name", "BTN_LOGIN").click()
return driver
# Semester needs to be updated each semester!
def navigate(driver, username, password, crn):
year = semester[:semester.find('-')]
season = semester[semester.find('-')+1:]
season = season[0].capitalize() + season[1:]
semester_str = season + ' ' + year + ' - Urbana-Champaign'
# this url might need update
url = "https://ui2web1.apps.uillinois.edu/BANPROD1/twbkwbis.P_GenMenu?name=bmenu.P_StuMainMnu"
driver.get(url)
driver.find_element('link text', 'Classic Registration').click()
driver.find_element('link text', 'Add/Drop Classes').click()
driver.find_element('link text', 'I Agree to the Above Statement').click()
# go to register page
options = Select(driver.find_element(By.ID, 'term_id'))
options.select_by_visible_text(semester_str)
path = '//input[@type="submit" and @value="Submit"]'
driver.find_element(By.XPATH, path).click()
# ============================================ main ===================================
# put the crn numbers into the array
crn_arr = []
for i in range(4, len(sys.argv)):
crn_arr.append(sys.argv[i])
if len(crn_arr) < 1:
print("crn index error")
# login url may change and might need update in the future
login_url = 'https://login.uillinois.edu/auth/SystemLogin/sm_login.fcc?TYPE=33554433&REALMOID=06-a655cb7c-58d0-4028-b49f-79a4f5c6dd58&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-dr9Cn7JnD4pZ%2fX9Y7a9FAQedR3gjL8aBVPXnJiLeXLOpk38WGJuo%2fOQRlFkbatU7C%2b9kHQgeqhK7gmsMW81KnMmzfZ3v0paM&TARGET=-SM-HTTPS%3a%2f%2fwebprod%2eadmin%2euillinois%2eedu%2fssa%2fservlet%2fSelfServiceLogin%3fappName%3dedu%2euillinois%2eaits%2eSelfServiceLogin%26dad%3dBANPROD1'
# semester in this format: YYYY-spring/YYYY-summer/YYYY-fall
semester = sys.argv[1]
username = sys.argv[2] # netid
password = sys.argv[3] # password
# please change to true if the course is crosslisted
cross_list = False
start = time.time()
term_in = construct_term_in(semester)
while len(crn_arr) != 0:
# the driver for refresh
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver = log_in(username, password, driver)
crn_success = ""
crn_success = refresh_course_website(driver, crn_arr, cross_list, term_in)
# if empty seat found. the driver for register
navigate(driver, username, password, crn_success)
register(driver, crn_success)
msg = "time spent: %s" % (time.time() - start)
print(msg)
print("crn: " + str(crn_success) + " is done!!!!!!!!!!!!!!!!!")
crn_arr.remove(crn_success)
driver.quit()