-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathin_school.py
178 lines (152 loc) · 7.04 KB
/
in_school.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
import logging
from time import sleep
import time
import datetime
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
user = "" # 你的统一认证账号
passwd = "" # 你的统一认证密码json
position = ("39.97805900941237", "116.34515751812742") # 定位,经纬度, 这个经纬度是大运村一号楼
SCKEY = "" # 微信推送api,到http://sc.ftqq.com/ 免费申请,不需要请留空
set_time = [(18, 12)] # (小时,分钟), 如果多个时间可以写成[(h, m),(h, m)]的形式
max_attempt = 5 # 失败重复五次
def daka():
login_flag, browser = login()
if not login_flag:
return
browser.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){" +
"var position = {\"coords\" : {\"latitude\": \"" + position[0] + "\",\"longitude\": \""
+ position[1] + "\"}};" +
"success(position);}")
time.sleep(5)
# /html/body/div[1]/div[1]/div[2]/div[5]/div/div[2]/div[2]/div[2]/input
location_button = browser.find_element_by_css_selector('body > div.buaaStudentNcov > div.buaaStudentNcov-bg > div > div:nth-child(5) > div > div.warp-list-choose > div.warp-list.two-warp-list.warp-list-margin > div.title-input.title-input-mergin-left')
browser.save_screenshot('location_test.png')
ActionChains(browser).move_to_element(location_button).click(location_button).perform()
time.sleep(2)
location = location_button.get_attribute('value')
logger.info(f"成功输入经纬度,定位{location}")
# /html/body/div[1]/div[1]/div[2]/div[6]
# 点击提交
submit_button = browser.find_element_by_xpath(
'/html/body/div[1]/div[1]/div/div[6]/a')
submit_button.click()
browser.implicitly_wait(3)
while True:
try:
if submit_button.text.find("submitted") != -1:
result = "打卡失败,您已经提交过"
break
elif submit_button.text.find("未到填报时间") != -1:
result = "未到填报时间"
break
submit_button = browser.find_element_by_css_selector('body > #wapcf > div > div.wapcf-btn-box > div.wapcf-btn-ok')
submit_button.click()
body = browser.find_element_by_css_selector('body')
confirm_button = browser.find_element_by_css_selector('body > #wapcf > div > div.wapcf-btn-box > div')
result = '提交成功'
break
except Exception as e:
try:
confirm_button = browser.find_element_by_css_selector('#wapat > div > div.wapat-btn-box > div')
reason = browser.find_element_by_css_selector('#wapat > div > div.wapat-title').text
result = f'打卡失败,原因:{reason}'
break
except:
time.sleep(1)
# confirm_button.click()
logger.info(result)
datee = datetime.date.today()
send_message(f"{datee} {result}")
sleep(50)
browser.quit()
logger.info("流程结束")
def login():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome(chrome_options=chrome_options)
try:
url = "https://app.buaa.edu.cn/uc/wap/login?redirect=https%3A%2F%2Fapp.buaa.edu.cn%2Fsite%2FbuaaStudentNcov%2Findex"
browser.get(url)
# 账号密码
user_name_input = browser.find_element_by_css_selector('#app > div.content > div:nth-child(1) > input[type=text]')
user_name_input.send_keys(user)
user_pwd_input = browser.find_element_by_css_selector(
'#app > div.content > div:nth-child(2) > input[type=password]')
user_pwd_input.send_keys(passwd)
except:
logger.info("打开打卡网页失败,请确认网络")
send_message("打开打卡网页失败,请确认网络")
return False, None
logger.info("成功打开打卡网页")
# 点击登录按钮
login_button = browser.find_element_by_css_selector('#app > div.btn')
ActionChains(browser).move_to_element(login_button).click(login_button).perform()
browser.implicitly_wait(2)
# 跳转并点击获取位置按钮
# 这样写是为了等待跳转页面加载出来
fail_cnt = 0
while True:
location_button = browser.find_elements_by_css_selector('body > div.buaaStudentNcov > div.buaaStudentNcov-bg > div > div:nth-child(5) > div > div.warp-list-choose > div.warp-list.two-warp-list.warp-list-margin > div.title-input.title-input-mergin-left')
if len(location_button) > 0:
logger.info("登录成功")
return True, browser
else:
# 出现密码错误提示框
if len(browser.find_elements_by_css_selector('div.wapat-btn-box')) > 0:
send_message("打卡失败,用户名密码错误,程序已退出,请检查")
logger.info("打卡失败,用户名密码错误,请检查")
exit(0)
# 若只是反应慢,重试
if fail_cnt >= max_attempt:
send_message("登录超时超过最大尝试次数,请检查网络或打卡系统已崩溃")
logger.info("登录超时超过最大尝试次数")
return False, None
time.sleep(10)
browser.get("https://app.buaa.edu.cn/site/buaaStudentNcov/index")
logger.info("登录超时,正在重试")
fail_cnt += 1
def main(): # 0:05进行打卡
logger.info("正在进行验证...")
flag, browser = login() # 测试能否进入网页以及用户名密码是否正确
browser.quit()
if not flag:
exit(0)
while True:
while True:
# time_up = True # debug
time_up = False
now = datetime.datetime.now()
for hour, minute in set_time:
if now.hour == hour and now.minute == minute:
time_up = True
if time_up:
break
logger.debug(f"时间未到,当前时间 {now}")
sleep(20)
logger.info("时间已到,正在打卡")
daka()
def send_message(msg):
if SCKEY == "":
return
payload = {'text': msg}
requests.get(f"https://sc.ftqq.com/{SCKEY}.send", params=payload)
if __name__ == "__main__":
log_file = "log.log"
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
logger = logging.getLogger("main")
fh = logging.FileHandler(log_file, mode='w')
fh.setFormatter(formatter)
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
main()