This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.py
128 lines (112 loc) · 4.44 KB
/
App.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import sys
import time
from json.decoder import JSONDecodeError
from shutil import which
from selenium import webdriver
def get_exec_path():
driver_name = 'chromedriver'
path = which(driver_name)
if path is None:
path = which(driver_name, path='.')
if path is None:
print('No chrome driver...')
sys.exit(1)
return path
def get_driver():
# Chrome options
op = webdriver.ChromeOptions()
# 关掉浏览器左上角的通知提示
op.add_argument("--disable-notifications")
# 关闭 chrome 正受到自动测试软件的控制'提示
op.add_argument("disable-infobars")
op.add_argument("--start-maximized")
# No gui
op.add_argument("--headless")
# Run under root user
op.add_argument("--no-sandbox")
op.add_argument("--disable-dev-shm-usage")
op.add_argument("--disable-gpu")
# Modify User agent
# op.add_argument(
# "user-agent='"
# "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) "
# "Chrome/84.0.4147.125 Mobile Safari/537.36'")
driver = webdriver.Chrome(executable_path=get_exec_path(), options=op)
return driver
def log(single, addition):
info = "[{time_tag} 姓名:{yzxx} 学号: {loginName}] {add}"
print(info.format(time_tag=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
yzxx=single.__getitem__('yzxx'),
loginName=single.__getitem__('loginName'), add=addition)
)
def task(driver, single):
driver.get("https://fxgl.jx.edu.cn/4136010406/public/homeQd?loginName="
+ single.__getitem__('loginName')
+ "&loginType=" + str(single.__getitem__('loginType')))
# 稍微等久一点,等待 js、css 加载
time.sleep(5)
log(single, '自动签到中...')
js = 'async function(){let t=\'REPLACE\';return t=JSON.parse(t),await async function(t){return await new Promise(' \
'n=>{$.ajax({url:"https://fxgl.jx.edu.cn/4136010406/studentQd/saveStu",method:"post",data:t,' \
'success:function(t){return n(JSON.stringify(t))}})})}(t)}(); '
js = js.replace("REPLACE", json.dumps(single.__getitem__('checkIn')))
print(driver.execute_script('return ' + js))
# time.sleep(3)
# log(single, '自动填写问卷中...')
# js = 'async function(){var t=\'REPLACE\',n="https://fxgl.jx.edu.cn/4136010406/";return 0==(t=JSON.parse(' \
# 't)).sf?n+="dcwjEditNew/dcwjSubmit2":n+="dcwjEditNew/dcwjTsubmit2",await async function(t,n){return await ' \
# 'new Promise(i=>{$.ajax({type:"post",url:t,data:{dcwj:JSON.stringify(n)},success:function(t){return i(' \
# 'JSON.stringify(t))}})})}(n,t)}(); '
# js = js.replace("REPLACE", json.dumps(single.__getitem__('paper')))
# print(driver.execute_script('return ' + js))
print()
def main():
json_filename = "./config.json"
try:
with open(json_filename, 'r', encoding='utf-8') as f:
data = json.load(f)
driver = get_driver()
success = 0
fail = []
index = -1
for single in data:
index += 1
try:
task(driver, single)
time.sleep(1)
success += 1
except Exception as e:
print(e)
fail += [index]
# Restart driver is too slow
driver.refresh()
driver = get_driver()
driver.quit()
# Error info
if len(fail) == 0:
for i in fail:
log(i, 'Error')
print('Total: ' + str(len(data))
+ '\nSuccess: ' + str(success)
+ '\nFail: ' + str(len(fail)) + '\n')
except FileNotFoundError:
print("File is not found: " + os.path.abspath(json_filename))
print("Creating file...")
with open(json_filename, 'w', encoding='utf-8') as f:
json.dump([{
"loginName": "student id",
"yzxx": "name",
"loginType": 0,
"checkIn": {},
"paper": {}
}], f)
except PermissionError:
print("No permission: " + os.path.abspath(json_filename))
except JSONDecodeError:
print("Error file data...")
if __name__ == '__main__':
main()