-
Notifications
You must be signed in to change notification settings - Fork 368
/
main.py
316 lines (254 loc) · 11.3 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- coding: utf-8
from configparser import ConfigParser
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from argparse import ArgumentParser
import re
from urllib.parse import quote
from urllib import request
import os
import sys
import time
import warnings
import json
warnings.filterwarnings('ignore')
def dropdown_handler(driver, xpath: str):
"""
点击带有滚动条的菜单
ref: https://stackoverflow.com/questions/57303355
"""
wait = WebDriverWait(driver, 10)
ele = wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
ele.location_once_scrolled_into_view
ele.click()
time.sleep(0.1)
def login(driver, userName, password, retry=0):
if retry == 3:
raise Exception('门户登录失败')
print('门户登陆中...')
appID = 'portal2017'
iaaaUrl = 'https://iaaa.pku.edu.cn/iaaa/oauth.jsp'
appName = quote('北京大学校内信息门户新版')
redirectUrl = 'https://portal.pku.edu.cn/portal2017/ssoLogin.do'
driver.get('https://portal.pku.edu.cn/portal2017/')
driver.get(
f'{iaaaUrl}?appID={appID}&appName={appName}&redirectUrl={redirectUrl}')
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'logon_button')))
driver.find_element_by_id('user_name').send_keys(userName)
time.sleep(0.1)
driver.find_element_by_id('password').send_keys(password)
time.sleep(0.1)
driver.find_element_by_id('logon_button').click()
try:
WebDriverWait(driver,
10).until(EC.visibility_of_element_located((By.ID, 'all')))
print('门户登录成功!')
except:
print('Retrying...')
login(driver, userName, password, retry + 1)
def go_to_simso(driver):
button = driver.find_element_by_id('all')
driver.execute_script("$(arguments[0]).click()", button)
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'tag_s_stuCampusExEnReq')))
driver.find_element_by_id('tag_s_stuCampusExEnReq').click()
time.sleep(2)
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
def go_to_application_out(driver):
go_to_simso(driver)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
def go_to_application_in(driver, userName, password):
driver.back()
time.sleep(0.5)
driver.back()
try:
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-card__body')))
time.sleep(0.5)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
except:
print('检测到会话失效,重新登陆中...')
login(driver, userName, password)
go_to_simso(driver)
driver.find_element_by_class_name('el-card__body').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'el-select')))
def select_in_out(driver, way):
driver.find_element_by_class_name('el-select').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{way}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{way}"]').click()
def select_campus(driver, campus):
driver.find_elements_by_class_name('el-select')[1].click()
dropdown_handler(driver, f'//li/span[text()="{campus}"]')
def write_reason(driver, reason):
driver.find_elements_by_class_name('el-select')[2].click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, f'//li/span[text()="{reason}"]')))
driver.find_element_by_xpath(f'//li/span[text()="{reason}"]').click()
def select_destination(driver, destination):
driver.find_elements_by_class_name('el-select')[3].click()
dropdown_handler(driver, f'//li/span[text()="{destination}"]')
def select_district(driver, district):
driver.find_elements_by_class_name('el-select')[4].click()
dropdown_handler(driver, xpath=f'//li/span[text()="{district}"]')
def write_mail_address(driver, mail_address):
driver.find_elements_by_class_name('el-input__inner')[2].clear()
driver.find_elements_by_class_name('el-input__inner')[2].send_keys(
f'{mail_address}')
time.sleep(0.1)
def write_phone_number(driver, phone_number):
driver.find_elements_by_class_name('el-input__inner')[3].clear()
driver.find_elements_by_class_name('el-input__inner')[3].send_keys(
f'{phone_number}')
time.sleep(0.1)
def write_reason_detail(driver, detail):
driver.find_element_by_class_name('el-textarea__inner').send_keys(
f'{detail}')
time.sleep(0.1)
def write_track(driver, track):
driver.find_elements_by_class_name('el-textarea__inner')[1].send_keys(
f'{track}')
time.sleep(0.1)
def write_street(driver, street):
driver.find_elements_by_class_name('el-textarea__inner')[1].send_keys(
f'{street}')
time.sleep(0.1)
def click_check(driver):
driver.find_element_by_class_name('el-checkbox__label').click()
time.sleep(0.1)
def click_inPeking(driver):
driver.find_element_by_class_name('el-radio__inner').click()
time.sleep(0.1)
def submit(driver):
driver.find_element_by_xpath(
'//button/span[contains(text(),"保存")]').click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, '(//button/span[contains(text(),"提交")])[3]')))
driver.find_element_by_xpath(
'(//button/span[contains(text(),"提交")])[3]').click()
time.sleep(0.1)
def fill_out(driver, campus, mail_address, phone_number, reason, detail, destination, track):
print('开始填报出校备案')
print('选择出校/入校 ', end='')
select_in_out(driver, '出校')
print('选择校区 ', end='')
select_campus(driver, campus)
print('填写邮箱 ', end='')
write_mail_address(driver, mail_address)
print('填写手机号 ', end='')
write_phone_number(driver, phone_number)
print('填写出入校事由 ', end='')
write_reason(driver, reason)
print('填写出入校事由详细描述 ', end='')
write_reason_detail(driver, detail)
print('选择出校目的地 ', end='')
select_destination(driver, destination)
print('填写出校行动轨迹 ', end='')
write_track(driver, track)
click_check(driver)
submit(driver)
print('出校备案填报完毕!')
def fill_in(driver, campus, mail_address, phone_number, reason, detail, habitation, district, street):
print('开始填报入校备案')
print('选择出校/入校 ', end='')
select_in_out(driver, '入校')
print('选择校区 ', end='')
select_campus(driver, campus)
print('填写邮箱 ', end='')
write_mail_address(driver, mail_address)
print('填写手机号 ', end='')
write_phone_number(driver, phone_number)
print('填写出入校事由 ', end='')
write_reason(driver, reason)
print('填写出入校事由详细描述 ', end='')
write_reason_detail(driver, detail)
if habitation != '北京':
raise Exception('暂不支持京外入校备案,请手动填写')
print('选择居住地所在区 ', end='')
select_district(driver, district)
print('填写居住地所在街道 ', end='')
write_street(driver, street)
click_inPeking(driver)
click_check(driver)
submit(driver)
print('入校备案填报完毕!')
def wechat_notification_success(userName, sckey):
with request.urlopen(
quote('https://sctapi.ftqq.com/' + sckey + '.send?title=成功报备&desp=学号' +
str(userName) + '成功报备',
safe='/:?=&')) as response:
response = json.loads(response.read().decode('utf-8'))
def wechat_notification_failed(userName, sckey):
with request.urlopen(
quote('https://sctapi.ftqq.com/' + sckey + '.send?title=[请注意]报备失败&desp=学号' +
str(userName) + '报备失败,需要您手动报备或在github页面手动重新运行,您可以在主仓库issue下报告错误日志。',
safe='/:?=&')) as response:
response = json.loads(response.read().decode('utf-8'))
def exception_printer(e: Exception or None):
print_bold = lambda x: print('\033[1;31m' + x + '\033[0m', file=sys.stderr)
print_bold("多次重试失败,报备失败")
print_bold('请检查您的配置是否正确,或者稍后重试')
print_bold('如果错误依然存在,请在这里汇报Bug:https://github.com/xiazhongyv/PKUAutoSubmit_online/issues')
print_bold('错误详细信息:')
raise e
def run(driver, userName, password, campus, mail_address, phone_number, reason, detail, destination, track,
habitation, district, street, wechat, sckey):
for try_times in range(5):
try:
print("======= 第", try_times + 1, "次报备尝试 =======")
login(driver, userName, password)
go_to_application_out(driver)
fill_out(driver, campus, mail_address, phone_number, reason, detail, destination, track)
go_to_application_in(driver, userName, password)
fill_in(driver, campus, mail_address, phone_number, reason, detail, habitation, district, street)
print('\n报备成功')
break
except Exception as e:
print("\n报备失败")
if try_times == 4:
if wechat:
wechat_notification_failed(userName, sckey)
exception_printer(e)
if wechat:
wechat_notification_success(userName, sckey)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--ID', type=str)
parser.add_argument('--PASSWORD', type=str)
parser.add_argument('--MAIL_ADDRESS', type=str)
parser.add_argument('--PHONE_NUMBER', type=str)
parser.add_argument('--SENDKEY', type=str)
argconf = parser.parse_args()
chrome_options = Options()
chrome_options.add_argument("--headless")
driver_pjs = webdriver.Edge(
options=chrome_options,
executable_path='/usr/bin/chromedriver',
service_args=['--ignore-ssl-errors=true', '--ssl-protocol=TLSv1'])
print('Driver Launched\n')
conf = ConfigParser()
conf.read("config.ini", encoding='utf8')
campus, reason, detail = dict(conf['common']).values()
destination, track = dict(conf['out']).values()
habitation, district, street = dict(conf['in']).values()
wechat = conf.getboolean('wechat', '是否需要微信通知')
run(driver_pjs, argconf.ID, argconf.PASSWORD, campus, argconf.MAIL_ADDRESS, argconf.PHONE_NUMBER, reason, detail,
destination, track, habitation, district, street, wechat, argconf.SENDKEY)
driver_pjs.quit()