-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
292 lines (261 loc) · 9.13 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
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
import os.path
import time
import json
import base64
import atexit
import requests
import logging
import threading
from urllib.parse import urlparse, parse_qsl
from flask import Flask, session, request, render_template
from apscheduler.schedulers.background import BackgroundScheduler
# Requests 库SSL校验 用于Debug
VERIFY = True
# task与日志保存路径
tasks_file = './tasks.json'
log_file = './logs.json'
# 轮询间隔时间
timers = 30
# 此处需要抓取问卷submit提交数据,填入request header中的Cpdaily-Extension字段内容
cpdaily_extension = 'To Request Heaedr Cpdaily-Extension Value'
lock = threading.Lock()
app = Flask(__name__)
app.config['SESSION_TYPE'] = 'memcached'
app.config['SECRET_KEY'] = '123456'
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
time_format = '%Y-%m-%d %I:%M:%S %p'
tasks = {
'idx': 0,
'data': []
}
logs = []
# Load Tasks
if os.path.isfile(tasks_file):
with open(tasks_file, 'r') as f:
tasks = json.load(f)
# load logs
if os.path.isfile(log_file):
with open(log_file, 'r') as f:
logs = json.load(f)
def loge(idx, status):
logs.append({
'idx': idx,
'status': status,
'time': time.strftime(time_format),
})
with open(log_file, 'w') as fd:
json.dump(logs, fd)
def save_tasks():
with open(tasks_file, 'w') as fd:
json.dump(tasks, fd)
def auto_post(task, fwid, wid):
# 检查地址
if len(task['address'].strip()) == 0:
print("Unknow address")
loge(task['idx'], '未填写地址')
return
try:
# 请求问卷内容
r = requests.post(
'https://ustl.cpdaily.com/wec-counselor-collector-apps/stu/collector/getFormFields',
json={
'pageSize': 10,
'pageNumber': 1,
'formWid': fwid,
'collectorWid': wid
}, headers={
'Cookie': task['cookie']
}, timeout=5, verify=VERIFY)
frj = r.json()
if frj['code'] != '0':
loge(task['idx'], '请求问卷失败')
return
# 填写
for row in frj['datas']['rows']:
if row['fieldType'] != 1:
task['status'] = '存在未知问题格式'
task['lastupd'] = time.strftime(time_format)
save_tasks()
return
if row['title'].find('是否') == -1:
task['status'] = '模棱两可的问题'
task['lastupd'] = time.strftime(time_format)
save_tasks()
return
row['value'] = "否"
# 提交
r = requests.post(
'https://ustl.cpdaily.com/wec-counselor-collector-apps/stu/collector/submitForm',
data=json.dumps({
'formWid': fwid,
'address': task['address'].strip(),
'collectWid': wid,
'schoolTaskWid': None,
'form': frj['datas']['rows'],
}, ensure_ascii=False).encode('utf-8'),
headers={
'Content-type': 'application/json; charset=utf-8',
'Cookie': task['cookie'],
'CpdailyStandAlone': '0',
'extension': '1',
'Cpdaily-Extension': cpdaily_extension
}, timeout=5, verify=VERIFY)
rj = r.json()
if rj['code'] != '0':
loge(task['idx'], '提交问卷失败,%s' % rj['message'])
task['status'] = '提交问卷失败'
task['lastupd'] = time.strftime(time_format)
save_tasks()
return
task['status'] = '提交成功'
task['lastupd'] = time.strftime(time_format)
save_tasks()
except requests.exceptions.Timeout:
loge(task['idx'], '问卷请求或提交超时')
except:
loge(task['idx'], 'auto_post错误')
def auto_poll():
global tasks
global logs
with lock:
for task in tasks['data']:
try:
r = requests.post(
'https://ustl.cpdaily.com/wec-counselor-collector-apps/stu/collector/queryCollectorProcessingList',
json={
'pageSize': 6,
'pageNumber': 1
}, headers={
'Cookie': task['cookie']
}, timeout=5, verify=VERIFY)
rj = r.json()
task['lastupd'] = time.strftime(time_format)
# 请求失败
if rj['code'] != '0':
task['status'] = rj['message']
loge(task['idx'], rj['message'])
save_tasks()
continue
save_tasks()
# 是否存在数据
if rj['datas']['totalSize'] <= 0:
continue
for row in rj['datas']['rows']:
if row['isHandled'] == 0:
auto_post(task, row['formWid'], row['wid'])
except requests.exceptions.Timeout:
loge(task['idx'], '请求超时')
except:
loge(task['idx'], 'auto_poll错误')
scheduler = BackgroundScheduler()
scheduler.add_job(func=auto_poll, trigger="interval", seconds=timers)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
@app.route('/')
def index():
with lock:
return render_template('index.html', tasks=tasks, logs=logs)
@app.route('/newTask')
def new_task():
with lock:
return render_template('newTask.html', tasks=tasks)
@app.route('/getCode')
def get_code():
try:
# 请求clientId
r = requests.get('https://ustl.cpdaily.com/wec-counselor-stu-apps/stu/mobile/index.html#/forAppNotice',
timeout=5, verify=VERIFY)
session['cookie'] = r.request.headers['Cookie']
query = dict(parse_qsl(urlparse(r.url).query))
session['jslogin'] = {
'clientId': query['client_id'],
'redirectUri': query['redirect_uri'],
'scope': query['scope'],
'responseType': query['response_type'],
'state': query['state'],
'supportFreeLogin': '',
}
# 请求二维码id
r = requests.post('https://www.cpdaily.com/connect/qrcode/jsLogin', json=session['jslogin'],
headers={'Cookie': session['cookie']},
timeout=5, verify=VERIFY)
rj = r.json()
# 请求失败
if rj['errCode'] != 0:
return {
'status': 1,
'errMsg': '%d,%s' % (rj['errCode'], rj['errMsg'])
}
# 下载二维码图像
r = requests.get('https://www.cpdaily.com/connect/qrcode/image/%s' % rj['data']['qrId'],
headers={'Cookie': session['cookie']},
timeout=5, verify=VERIFY)
session['qrid'] = rj['data']['qrId']
return {
'status': 0,
'data': base64.b64encode(r.content).decode('ascii')
}
except requests.exceptions.Timeout:
return {
'status': 1,
'errMsg': '服务器请求二维码超时'
}
@app.route('/validation')
def get_validation():
global tasks
try:
if 'cookie' in session and 'qrid' in session and 'jslogin' in session and 'address' in session:
r = requests.post('https://www.cpdaily.com/connect/qrcode/validation/%s' % session['qrid'],
json=session['jslogin'], headers={'Cookie': session['cookie']}, verify=VERIFY)
rj = r.json()
if rj['data']['status'] == 4:
# 获取 MOD_AUTH_CAS
r = requests.get(rj['data']['redirectUrl'], verify=VERIFY)
with lock:
tasks['idx'] += 1
tasks['data'].append({
'cookie': r.request.headers['Cookie'],
'address': session['address'],
'lastupd': 'N/A',
'idx': tasks['idx'],
'status': '正常',
})
session.pop('cookie', None)
session.pop('qrid', None)
session.pop('jslogin', None)
return {
'status': 0,
'data': rj['data']['status']
}
else:
return {
'status': 1,
'errMsg': '请刷新二维码'
}
except requests.exceptions.Timeout:
return {
'status': 1,
'errMsg': '服务器异常'
}
@app.route('/setAddress', methods=['GET'])
def set_address():
session['address'] = request.args.get('addr')
return {
'status': 0
}
@app.route('/delTask', methods=['GET'])
def del_task():
task_idx = request.args.get('idx')
with lock:
for idx in range(len(tasks['data'])):
if tasks['data'][idx]['idx'] == int(task_idx):
del(tasks['data'][idx])
save_tasks()
return {
'status': 0
}
if __name__ == '__main__':
# Flask
app.run(host='0.0.0.0', port=7920)