-
Notifications
You must be signed in to change notification settings - Fork 0
/
course-taking-bot.py
138 lines (111 loc) · 5.97 KB
/
course-taking-bot.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
""" Libraries """
import os
import time
import random
from seleniumwire import webdriver
from selenium.common.exceptions import WebDriverException
from utils import (
BrowserStuckError,
beep_sound, my_time_str, read_account,
wait_until_9_am, wait_to_click, wait_and_find_element_by_id, wait_element_text_by_id,
wait_for_validate_code_img, get_validate_code_img, wait_for_validate_code_button,
my_predict, process_validate_code,
login,
)
from model import load_MyModel
""" Functions """
def course_taking(driver, model, course_ids, course_names=None):
start_time = time.time()
while True:
print(f"{my_time_str(start_time)} - The course ids list now:", course_ids, '\n')
try:
for course_id in course_ids:
wait_and_find_element_by_id(driver, "serialNo-inputEl").clear()
wait_and_find_element_by_id(driver, "serialNo-inputEl").send_keys(course_id)
time.sleep(0.2)
# 驗證碼: 正確 或 錯誤
while True:
# 驗證碼破圖
validate_code_img_broken_time = 0
while True:
wait_to_click(driver.find_element_by_id("button-1060-btnEl")) # 「開課序號直接加選儲存」按鈕
wait_for_validate_code_img(driver)
validate_code_img = get_validate_code_img(driver)
if validate_code_img is not None:
break
else:
wait_to_click(wait_for_validate_code_button(driver, "cancel")) # 「取消」按鈕
wait_to_click(wait_and_find_element_by_id(driver, "button-1005-btnIconEl")) # 「OK」按鈕
retry_time = validate_code_img_broken_time * 2 + 3
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code image broken. Retry in {retry_time} seconds.")
time.sleep(retry_time)
validate_code_img_broken_time += 1
validate_code = my_predict(model, validate_code_img)
validate_code = process_validate_code(validate_code)
wait_and_find_element_by_id(driver, "valid-inputEl").send_keys(validate_code)
wait_to_click(wait_for_validate_code_button(driver, "confirm")) # 「確認」按鈕
condition = wait_element_text_by_id(driver, "messagebox-1001-displayfield-inputEl", ["驗證碼錯誤", "額滿", "衝堂", "重複登記", "儲存成功"])
if condition == 0: # 驗證碼錯誤
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code '{validate_code}' incorrect. Retry in 3 seconds.")
wait_to_click(wait_and_find_element_by_id(driver, "button-1005-btnIconEl")) # 「OK」按鈕
time.sleep(3)
elif condition == 1: # 額滿
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code '{validate_code}' correct. Full.")
break
elif condition == 2: # 衝堂
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code '{validate_code}' correct. Conflict.")
beep_sound()
course_ids.remove(course_id)
break
elif condition == 3: # 重複登記
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code '{validate_code}' correct. Duplicated.")
beep_sound()
course_ids.remove(course_id)
break
elif condition == 4: # 儲存成功
print(f"{my_time_str(start_time)} - Course {course_id}: Validate code '{validate_code}' correct. Success!")
beep_sound()
course_ids.remove(course_id)
break
wait_to_click(wait_and_find_element_by_id(driver, "button-1005-btnIconEl")) # 「OK」按鈕
random_second = 5 + 5 * random.random()
print(f"{my_time_str(start_time)} - Sleep for {random_second:.2} seconds.\n")
time.sleep(random_second)
if time.time() - start_time > 1170: break # 19min 30sec
if len(course_ids) == 0: break
if time.time() - start_time > 1170: break # 19min 30sec
except BrowserStuckError:
break
except WebDriverException:
break
return course_ids
def main():
# Check chromedriver
if "chromedriver.exe" not in os.listdir("chromedriver_win32"):
print("\nPlease download the chromedriver with corresponding version with " +
"your Google Chrome at here:\nhttps://chromedriver.chromium.org/downloads\n"+
"And put the 'chromedriver.exe'. in 'chromedriver_win32'\n")
return
# Read username, password and course ids
try: username, password, course_ids, course_names = read_account()
except: return
# Load predict model
model = load_MyModel()
model.summary()
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--auto-open-devtools-for-tabs")
while True:
wait_until_9_am()
driver = webdriver.Chrome("chromedriver_win32/chromedriver.exe", chrome_options=options)
login(driver, username, password, model)
course_ids = course_taking(driver, model, course_ids, course_names)
driver.delete_all_cookies()
driver.close()
if len(course_ids) == 0: break
time.sleep(10)
print(f"\n{my_time_str()} - Restart a turn.\n")
print("\n\nSuccess! Finish all course taking tasks!\n\n")
""" Execution """
if __name__ == "__main__":
main()