-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
406 lines (304 loc) · 12.4 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
import requests
import uuid
import json
import os
# Proxy
proxies={
'http':None,
'https':None
}
def getUUID():
return uuid.uuid1()
def getParentDir(path=None, offset=-1):
result = path if path else __file__
for i in range(abs(offset)):
result = os.path.dirname(result)
return result
# 临时修复,等待新 API
def getCourseId(url):
cutUrl = url.split('li=')
cutUrl = cutUrl[1].split('&')
return cutUrl[0]
def getSubStr(s, start_str, stop_str):
start_pos = s.find(start_str)
if start_pos == -1:
return None
start_pos += len(start_str)
stop_pos = s.find(stop_str, start_pos)
if stop_pos == -1:
return None
return s[start_pos:stop_pos]
def loginAccount(username, password):
url = 'https://appapi.xueanquan.com/usercenter/api/v1/account/PostLogin'
data = '{"EquipmentId":"' + str(getUUID()) + '","Password":"' + password + '","Username":"' + username + '"}'
headers = {
'Accept': 'application/json',
'User-Agent': 'safetreeapp/1.8.7',
'Content-Type': 'application/json'
}
requestData = requests.post(url, data=data, headers=headers, proxies=proxies)
if(requestData.status_code == 200):
return requestData.text
else:
return '-1'
def getHomeworkList(UserID, ServerSide):
url = 'https://applet.xueanquan.com/pt/zhejiang/safeapph5/api/v1/homework/homeworklist'
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
requestData = requests.get(url, cookies=cookies, headers=headers, proxies=proxies)
if(requestData.status_code == 200):
return requestData.text
else:
return '-1'
def watchVideo(courseId, UserID, ServerSide, gradeId):
videoSignUrl = 'https://yyapi.xueanquan.com/zhejiang/api/v1/StudentHomeWork/VideoPlayRecordSave?courseId=' + courseId + '&gradeId=' + gradeId
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
videoSignData = requests.post(videoSignUrl, cookies=cookies, headers=headers, proxies=proxies)
try:
videoSignMsg = json.loads(videoSignData.text)
except:
print('! 模块二执行失败')
return False
isVideoSignSuccess = videoSignMsg ['success']
if(isVideoSignSuccess):
return True
else:
return False
def doSkillTest(courseId, UserID, ServerSide, cityCode, schoolId, classroom, grade):
testUrl = 'https://yyapi.xueanquan.com/zhejiang/api/v1/StudentHomeWork/GetSkillTestPaper?courseId=' + courseId
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
skillTestData = requests.get(testUrl, cookies=cookies, headers=headers, proxies=proxies)
try:
skillTestMsg = json.loads(skillTestData.text)
except:
print('! 模块二执行失败')
return False
skillTestResult = skillTestMsg ['result']
skillTestFid = skillTestResult ['fid']
skillTestWordId = skillTestResult ['workId']
if(skillTestFid == '' or skillTestWordId == ''):
print('! 模块二执行失败')
return False
testSignUrl = 'https://yyapi.xueanquan.com/zhejiang/api/v1/StudentHomeWork/HomeWorkSign'
headers = {
'Accept': '*/*',
'Content-Type': 'application/json;charset=UTF-8',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
data = '{"workId":' + str(skillTestWordId) + ',"fid":' + str(skillTestFid) + ',"testinfo":"已掌握技能","testanswer":"0|0|0","testMark":100,"testResult":1,"courseId":"' + str(courseId) + '","grade":' + str(grade) + ',"cityCode":' + str(cityCode) + ',"schoolId":' + str(schoolId) + ',"classroom":' + str(classroom) + '}'
requestData = requests.post(testSignUrl, data=data.encode('utf-8'), cookies=cookies, headers=headers, proxies=proxies)
try:
requestMsg = json.loads(requestData.text)
except:
print('! 模块二执行失败')
return False
isSkillTestSuccess = requestMsg ['success']
if(isSkillTestSuccess):
return True
else:
return False
def doHomework(courseId, UserID, ServerSide, cityCode, schoolId, classroom, grade, gradeId):
print('+ 正在自动完成模块一 (视频)...')
if(watchVideo(courseId, UserID, ServerSide, gradeId)):
print('+ 已完成模块一 (视频)')
else:
print('+ 未完成模块一 (视频)')
return False
print('+ 正在自动完成模块二 (答题)...')
if(doSkillTest(courseId, UserID, ServerSide, cityCode, schoolId, classroom, grade)):
print('+ 已完成模块二 (答题)')
return True
else:
print('+ 未完成模块二 (答题)')
return False
def doSpecialSign(specialId, UserID, ServerSide):
# TO-DO 通用 API 替换
# /Topic/topic/main/api/v1/
url = 'https://huodongapi.xueanquan.com/p/zhejiang/Topic/topic/platformapi/api/v1/records/sign'
for currentWork in range(1, 3):
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
data = '{"specialId":' + specialId + ',"step":' + str(currentWork) + '}'
requestData = requests.post(url, data=data, headers=headers, cookies=cookies, proxies=proxies)
try:
specialSignMsg = json.loads(requestData.text)
except:
print('! 执行失败')
return False
isSignSeccuss = specialSignMsg ['result']
signMsg = specialSignMsg ['msg']
if(isSignSeccuss):
print('+ 已完成第 ' + str(currentWork) + ' 个模块,' + signMsg)
else:
print('! 不能完成第 ' + str(currentWork) + ' 个模块,' + signMsg)
def getspecialId(url):
parentUrl = getParentDir(url)
jsUrl = parentUrl + "/style/common.js"
requestData = requests.get(jsUrl, proxies=proxies)
requestText = requestData.text
releaseData = getSubStr(requestText, "release:{", "}")
return getSubStr(releaseData, "specialId: ", ",")
def doSpecial(url, UserID, ServerSide):
print('+ 正在自动完成专题活动...')
specialId = getspecialId(url)
print("* 活动 ID: " + specialId)
if(specialId == '' or specialId == '0'):
print("! 执行失败,活动不存在")
return
doSpecialSign(specialId, UserID, ServerSide)
def doHolidaySign(specialId, UserID, ServerSide):
# TO-DO 通用 API 替换
# /Topic/topic/main/api/v1/
url = 'https://huodongapi.xueanquan.com/p/zhejiang/Topic/topic/platformapi/api/v1/holiday/sign'
for currentWork in range(1, 3):
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'safetreeapp/1.8.7',
}
cookies = {
'UserID': UserID,
'EquipmentId': str(getUUID()),
'ServerSide': ServerSide
}
# TO-DO
data = '{"schoolYear":2023,"semester":1,"specialId":' + specialId + ',"step":' + str(currentWork) + '}'
requestData = requests.post(url, data=data, headers=headers, cookies=cookies, proxies=proxies)
try:
specialSignMsg = json.loads(requestData.text)
except:
print('! 执行失败')
return False
isSignSeccuss = specialSignMsg ['result']
signMsg = specialSignMsg ['msg']
if(isSignSeccuss):
print('+ 已完成第 ' + str(currentWork) + ' 个模块,' + signMsg)
else:
print('! 不能完成第 ' + str(currentWork) + ' 个模块,' + signMsg)
def doHolidaySpecial(url, UserID, ServerSide):
print('+ 正在自动完成假期专题活动...')
specialId = getspecialId(url)
print("* 活动 ID: " + specialId)
if(specialId == '' or specialId == '0'):
print("! 执行失败,活动不存在")
return
doHolidaySign(specialId, UserID, ServerSide)
def doWorkUtil(username, password):
accountLogin = loginAccount(username, password)
print('\n- 当前账号: ' + username)
print('- 正在登录...\n')
try:
accountMsg = json.loads(accountLogin)
except:
print('! 登录失败,返回信息有误')
return False
accountData = accountMsg ['data']
try:
webUrl = accountData ['webUrl']
UserId = accountData ['accessCookie']
grade = accountData ['grade']
cityId = accountData ['cityId']
schoolId = accountData ['schoolId']
classroomId = accountData ['classroomId']
except:
print('! 登录失败,请检查账号信息是否有误')
return False
if(grade == '' or cityId == '' or schoolId == '' or classroomId == ''):
print('! 登录失败,请检查账号信息是否有误')
return False
homeworkList = getHomeworkList(UserId, webUrl)
try:
workListData = json.loads(homeworkList)
except:
print('! 登录失败,返回信息有误')
return False
print('- 正在解析任务列表...')
numOfWork = len(workListData)
print('= 获取到了 ' + str(numOfWork) + ' 个任务\n')
for currentWork in range(numOfWork):
print('> 正在解析第 ' + str(currentWork + 1) + '/' + str(numOfWork) + ' 个任务 ===================')
currentWorkData = workListData [currentWork]
currentWorkTitle = currentWorkData ['title']
currentWorkUrl = currentWorkData ['url']
workStatus = currentWorkData ['workStatus']
currentWorkType = currentWorkData ['sort']
print('= 当前课程标题: ' + currentWorkTitle)
print('= 当前课程链接: ' + currentWorkUrl)
if(currentWorkType == 'Skill'):
print('= 当前课程类型: ' + '普通课程')
elif(currentWorkType == 'Special'):
print('= 当前课程类型: ' + '专题活动')
elif(currentWorkType == 'SummerWinterHoliday'):
print('= 当前课程类型: ' + '寒暑假专题')
else:
print('= 当前课程类型: ' + '未知 (' + currentWorkType + ')')
if(workStatus == 'Finished'):
print('= 当前课程完成情况: ' + '已完成')
elif(workStatus == 'UnFinish'):
print('= 当前课程完成情况: ' + '未完成')
if(currentWorkType == 'Skill'):
courseId = getCourseId(currentWorkUrl)
gradeId = getSubStr(currentWorkUrl, 'gid=', '&')
print('= 当前课程编号: ' + courseId)
doHomework(courseId, UserId, webUrl, cityId, schoolId, classroomId, grade, gradeId)
elif(currentWorkType == 'Special'):
specialUrl = str(currentWorkUrl).replace('index', 'jiating')
doSpecial(specialUrl, UserId, webUrl)
elif(currentWorkType == 'SummerWinterHoliday'):
holidayUrl = str(currentWorkUrl).replace('index', 'shipin')
doHolidaySpecial(holidayUrl, UserId, webUrl)
else:
print('! 未知错误')
else:
print('= 当前课程完成情况: ' + '未知')
print('< 完成解析第 ' + str(currentWork + 1) + '/' + str(numOfWork) + ' 个任务 ===================\n')
if __name__ == '__main__':
currentPath = os.getcwd()
pathArg = os.sep
configFilePath = currentPath + pathArg + 'account.txt'
configFile = open(configFilePath, 'r', encoding = 'UTF-8')
configFileContent = configFile.read()
configArray = configFileContent.split('\n')
numOfAccount = len(configArray)
failWork = 0
print('\n- 共 ' + str(numOfAccount) + ' 个账号')
for currentAccount in range(numOfAccount):
splitAccount = configArray [currentAccount].split(',')
username = splitAccount [0]
password = splitAccount [1]
doWorkUtil(username, password)