-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
124 lines (109 loc) · 3.81 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
122
123
124
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import requests
from selenium import webdriver
import time
import sys
import getpass
mode = 1
dept = 'F7'
course = '160'
account = '123'
password = '123'
line_token = '123'
# send line message
def lineNotifyMessage(msg):
global line_token
headers = {
"Authorization": "Bearer " + line_token,
"Content-Type" : "application/x-www-form-urlencoded"
}
payload = {'message': msg}
r = requests.post("https://notify-api.line.me/api/notify", headers = headers, params = payload)
return r.status_code
# find remain course and use same driver
def find_remain_only(driver):
global mode, dept, course
# goto course main page
driver.get('https://course.ncku.edu.tw/index.php?c=qry_all')
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR, f'li[data-dept="{dept}"]').click()
time.sleep(2)
isRemain = False
# already into subject page
for i in range(1, 10000):
text = driver.find_element(By.CSS_SELECTOR, f'#A9-table > tbody > tr:nth-child({i}) > td:nth-child(2) > div').text
if(text == dept+'-'+course):
ele = driver.find_element(By.XPATH, f'//*[@id="A9-table"]/tbody/tr[{i}]/td[8]').text
if(ele.find('額滿') < 0):
isRemain = True
break
if isRemain:
if mode == 1:
message = dept+'-'+course +' 有餘額'
lineNotifyMessage(message)
elif mode == 2:
# login and choose course
login(driver)
message = dept+'-'+course +' 選課成功'
lineNotifyMessage(message)
driver.close()
sys.exit()
# choose course
def choose(driver):
global dept, course
# goto 預排選課 page
# COS NUMBER MAYBE GET SOME ERROR BUT I CANNOT RECOGNIZE
driver.get('https://course.ncku.edu.tw/index.php?c=cos21322')
time.sleep(2)
s = dept+course
try:
driver.find_element(By.CSS_SELECTORf, '#main-table > tbody > tr.course_tr.course_{s}.td_bg1 > td:nth-child(10) > button').click()
except:
pass
time.sleep(0.5)
try:
driver.find_element(By.CSS_SELECTOR, f'#main-table > tbody > tr.course_tr.course_{s}.td_bg2 > td:nth-child(10) > button').click()
except:
pass
# login course website
def login(driver):
global account, password
driver.set_window_size(1400, 800)
driver.get('https://course.ncku.edu.tw/index.php?c=auth')
driver.find_element(By.XPATH, '//*[@id="loginbg"]/div/div/div[2]/a').click()
time.sleep(2)
# input username && passsword
driver.find_element(By.XPATH, '//*[@id="userNameInput"]').send_keys(account)
driver.find_element(By.XPATH, '//*[@id="passwordInput"]').send_keys(password)
driver.find_element(By.XPATH, '//*[@id="submitButton"]').click()
time.sleep(2)
# force login
try:
driver.find_element(By.XPATH, '//*[@id="error"]/div/form/span/div/div[2]/p/a').click()
except:
pass
time.sleep(2)
choose(driver)
def main():
global mode, dept, course, account, password, line_token
# mode 1: remain tracking
# mode 2: auto choosing course
mode = eval(input("choose mode, 1 is notify, 2 is auto: "))
dept = input('department number: ')
course = input('course number: ')
line_token = input('line_token: ')
if mode == 2:
account = input('account: ')
password = getpass.getpass('password: ')
# create a chrome driver ##
driverpath = ChromeDriverManager().install()
service = webdriver.chrome.service.Service(executable_path=driverpath)
driver = webdriver.Chrome(service=service)
driver.set_window_size(1400, 800)
time.sleep(0.5)
# track course remain
while True:
find_remain_only(driver)
time.sleep(5)
main()