This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcqooc.py
609 lines (525 loc) · 24.7 KB
/
cqooc.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# -*- coding: utf-8 -*-
import os.path
import requests
import time
import json
import re
################### Config #############################
# Windows推荐直接双击脚本运行,或使用CMD/PowerShell运行
# 不推荐使用Python IDLE运行(执行输出会乱码)
# 如果使用CMD/PowerShell运行过程中,"卡"住,请多敲几下回车即可
cookie_xsid = ''
########################################################
def getTs():
return int(time.time() * 1000)
class AutoCompletPapers():
def __init__(self, session, courseId, title):
"""
自动完成小节习题(仅支持已过提交日期的习题)
:return:
"""
self.mid = None
self.Session = session
self.courseId = courseId
self.cookieXsidUser = None
self.title = title
self.answerTable = {
"-1": '未作答',
"0": 'A',
"1": 'B',
"2": 'C',
"3": 'D',
"4": 'E',
"5": 'F',
"6": 'G',
}
self.judge = {
"-1": '未作答',
"1": 'A',
"2": 'B',
}
self.sectionList = \
self.get('https://www.cqooc.com/json/chapter/lessons?courseId=' + self.courseId).json()['data'][0]['body']
try:
self.name = self.get(f'https://www.cqooc.com/account/session/api/profile/get?ts={getTs()}').json().get(
'name')
except:
self.name = input("名字获取失败!请输入你的名字(真实名字): ")
def get(self, url, headers=None):
# 防止请求异常抛出,异常自动重新请求
while True:
try:
return self.Session.get(url, headers=headers)
except:
print("请求异常,重试中...")
continue
def post(self, url, json=None, headers=None, data=None):
while True:
try:
return self.Session.post(url, json=json, headers=headers, data=data)
except:
print("请求异常,重试中...")
continue
def getIds(self, paperId):
# Referer
response = self.get(f'https://www.cqooc.com/test/api/paper/info?id={paperId}&ts={getTs()}')
data = response.json()
return {"cid": data.get('parentId'),
"sid": self.sectionList.get(data.get('parentId'))[0]
}
def saveAnswersFromDue(self, papersIds):
file = open(f'{self.title}.txt', 'w+')
print(f"开始导出,共 {len(papersIds)} 次测试")
for id_index, id in enumerate(papersIds):
req_url = 'https://www.cqooc.com/test/api/paper/get?id=' + str(id) + '&ts=1581869807566'
response = self.get(req_url, headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={id}&id={self.courseId}&sid=381090&cid=158386&mid=12166414',
})
submitEnd = response.json()['submitEnd']
if submitEnd > time.time() * 1000:
print(f"未过期,跳过! {papersIds.get(id)}")
continue
if '503 Service Temporarily' in response.text:
print(str(id) + " 失败!!")
continue
body = response.json()['body']
if body is None: continue
for i in body:
if i['questions'] != []:
if i['desc'] == '判断题':
questions_type = "[判断题] "
elif i['desc'] == '多选题':
questions_type = "[多选题] "
elif i['desc'] == '单选题':
questions_type = "[单选题] "
else:
questions_type = ''
for question in i['questions']:
# 提取问题
if '<p>' in question['question']:
question_re = re.findall('>(.*?)<', question['question'], re.S)
question_plus = ''
for t in question_re:
question_plus += t.replace(' ', '')
file.write(questions_type + question_plus.lstrip() + '\n')
else:
question_plus = question['question'].replace('\n', '').replace('\r', '')
file.write(questions_type + question_plus.lstrip() + '\n')
# 处理判断题
if i['desc'] == '判断题':
file.write('答案: ')
file.write(self.judge[question['body']['answer'][0]])
# 非判断题
else:
# 遍历选项并写入
for index, j in enumerate(question['body']['choices']):
file.write(' ' + self.answerTable[str(index)] + '、' + j + '\n')
file.write('答案: ')
# 写入答案,多选题需遍历
for x in question['body']['answer']:
file.write(self.answerTable[x] + ' ')
file.write('\n\n')
# 请求过快会503
print(f"[{id_index + 1}/{len(papersIds)}] {papersIds.get(id)}")
time.sleep(1)
print("写入完成!", os.path.abspath(f'{self.title}.txt'))
def saveAnswersFromUser(self, papersIds):
# 保存已作答题目的答案
file = open(f'{self.title}.txt', 'w+')
print(f"开始导出,共 {len(papersIds)} 次测试")
for id_index, id in enumerate(papersIds):
data = self.get(f'https://www.cqooc.com/json/test/result/search?testID={id}', headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={id}&id={self.courseId}'
f'&sid=360456&cid=149658&mid=12158213',
})
time.sleep(0.5)
req_url = f'https://www.cqooc.com/test/api/paper/get?id={id}'
response = self.get(req_url, headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={id}&id={self.courseId}'
f'&sid=360456&cid=149658&mid=12158213',
})
body = response.json()['body']
if body is None: continue
for i in body:
if i['questions']:
if i['desc'] == '判断题':
questions_type = "[判断题] "
elif i['desc'] == '多选题':
questions_type = "[多选题] "
elif i['desc'] == '单选题':
questions_type = "[单选题] "
else:
questions_type = ''
for question in i['questions']:
# 提取问题
if '<p>' in question['question']:
question_re = re.findall('>(.*?)<', question['question'], re.S)
question_plus = ''
for t in question_re:
question_plus += t.replace(' ', '')
file.write(questions_type + question_plus.lstrip() + '\n')
else:
question_plus = question['question'].replace('\n', '').replace('\r', '')
file.write(questions_type + question_plus.lstrip() + '\n')
if not data.json()['data']:
answers = '-1'
else:
answers = data.json()['data'][0]['body'][0]['q' + question['id']]
if i['desc'] == '判断题':
answer = self.judge[answers]
# 写入答案
file.write('答案: ')
file.write(answer)
# 非判断题
else:
# 遍历选项并写入
if data.json()['data'] == []:
answer = ['-1']
else:
answer = answers
tempAnswer = ""
for tag in answer:
tempAnswer += self.answerTable[str(tag)]
for index, j in enumerate(question['body']['choices']):
file.write(' ' + self.answerTable[str(index)] + '、' + j.lstrip() + '\n')
file.write('答案: ')
# 写入答案,多选题需遍历
file.write(tempAnswer + ' ')
file.write('\n\n')
print(f"[{id_index + 1}/{len(papersIds)}] {papersIds.get(id)}")
time.sleep(1)
file.write('\n\n')
print("写入完成!", os.path.abspath(f'{self.title}.txt'))
def getAnswers(self, paperId):
# 获取答案
req_url = f'https://www.cqooc.com/test/api/paper/get?id={paperId}&ts={getTs()}'
try:
ids = self.getIds(paperId)
except TypeError:
return None
response = self.get(req_url, headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={paperId}&id={self.courseId}'
f'&sid={ids.get("sid")}&cid={ids.get("cid")}&mid={self.mid}',
})
submitEnd = response.json()['submitEnd']
if submitEnd > time.time() * 1000:
return -1
body = response.json()['body']
answers = {}
try:
for i in body:
if i['questions'] != []:
for question in i['questions']:
answer = question['body']['answer']
if len(answer) == 1:
answer = answer[0]
answers["q" + question['id']] = answer
return answers
except:
return None
def getAnswersFromUser(self, paperId):
# 获取答案(从另一个用户)
self.cookieXsidUser = input(
"请输入已作答用户的cookie(xsid): ") if self.cookieXsidUser is None else self.cookieXsidUser
# ids = self.getIds(paperId)
session = requests.session()
session.headers[
'User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
session.headers['Connection'] = 'close'
session.headers['cookie'] = 'xsid={}; player=1'.format(self.cookieXsidUser)
data = session.get('https://www.cqooc.com/json/test/result/search?testID=' + str(paperId), headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={paperId}&id={self.courseId}&sid=360456&cid=149658&mid={self.mid}',
})
time.sleep(0.5)
req_url = 'https://www.cqooc.com/test/api/paper/get?id=' + str(paperId)
response = session.get(req_url, headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?do?tid={paperId}&id={self.courseId}&sid=360456&cid=149658&mid=12158213',
})
if response.json().get('code') == 401:
input("xsid有误,请检查!")
return -2
body = response.json()['body']
answers = {}
try:
for i in body:
if i['questions'] != []:
for question in i['questions']:
answer = data.json()['data'][0]['body'][0]['q' + question['id']]
if len(answer) == 1:
answer = answer[0]
answers["q" + question['id']] = answer
return answers
except:
return None
def sendAnswers(self, mode=None):
"""
:param mode: due 已过期题目获取答案提交 非due 从另一个用户获取答案提交
:return:
"""
info = self.get('https://www.cqooc.com/user/session?xsid=' + cookie_xsid).json()
self.mid = \
self.get(f'https://www.cqooc.com/json/mcs?ownerId={info["id"]}&courseId={self.courseId}&ts={getTs()}',
headers={
"Referer": f'https://www.cqooc.com/learn/mooc/structure?id={self.courseId}'
}).json()['data'][0]['id']
papersList = self.get(
f'https://www.cqooc.com/json/exam/papers?limit=200&start=1&courseId={self.courseId}&select=id,title&ts={getTs()}')
papersInfo = {}
for i in papersList.json()['data']:
papersInfo[str(i.get('id'))] = i.get('title')
print("\n[{}] 共 {} 题".format('过期题目作答' if mode == 'due' else '拷贝答案作答', len(papersInfo)))
for index, id in enumerate(papersInfo):
answers = self.getAnswers(id) if mode == 'due' else self.getAnswersFromUser(id)
if answers == -1:
print("{}/{} [{}] 未过提交日期,跳过!".format(len(papersInfo), index + 1, papersInfo[id]))
time.sleep(1)
continue
elif answers is None:
print(
f"[{index + 1}/{len(papersInfo)}] {'无测试题目,跳过!' if mode == 'due' else '未获取到答案,跳过!'} {papersInfo[id]} ")
time.sleep(1)
continue
elif answers == -2:
return
# 检查是否已经作答
isAnswer = self.get(f'https://www.cqooc.com/json/test/result/search?testID={id}&ts={getTs()}', headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={id}&id={self.courseId}&sid=488839&cid=197038&mid={self.mid}'
}).json()
if isAnswer['data']:
print(f"[{index + 1}/{len(papersInfo)}] 已作答,跳过! {papersInfo[id]}")
# print("{}/{} [{}] 已作答,跳过!".format(len(papersInfo), index + 1, papersInfo[id]))
time.sleep(1)
continue
response = self.post('https://www.cqooc.com/test/api/result/add', headers={
'Referer': f'https://www.cqooc.com/learn/mooc/testing/do?tid={id}&id={self.courseId}&sid=307978&cid=131676&mid={self.mid}',
}, data=json.dumps({
"ownerId": info.get('id'),
"username": info.get('username'),
"name": self.name,
"paperId": str(id),
"courseId": self.courseId,
"answers": answers,
"classId": ""
}))
if response.json().get('code') == 100:
print(f"[{index + 1}/{len(papersInfo)}] 已超过提交最大次数! {papersInfo[id]}")
# print("{}/{} [{}] 已超过提交最大次数!".format(len(papersInfo), index + 1, papersInfo[id]))
else:
print(f"[{index + 1}/{len(papersInfo)}] 得分: {response.json().get('score')} {papersInfo.get('id')}")
# print("{}/{} [{}] 得分: {}".format(len(papersInfo), index + 1, papersInfo[id], response.json().get('score')))
time.sleep(1)
class AutoCompleteOnlineCourse:
def __init__(self) -> None:
if cookie_xsid == '':
input("请添加xsid")
exit(0)
# HEADERS
session = requests.Session()
session.headers['Cookie'] = 'player=1; xsid=' + cookie_xsid
# session.headers['Connection'] = 'close'
session.headers[
'User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
session.headers['Host'] = 'www.cqooc.com'
session.keep_alive = False
self.Session = session
self.CompleteCourse = None
self.courseId = None
self.courseDes = None
self.username = None
self.ownerId = None
self.title = None
self.parentId = None
@staticmethod
def sleep_print(sec):
for i in range(sec):
print(f"\r等待 {sec - i} 秒后继续", end='')
time.sleep(1)
def get(self, url, headers=None):
while True:
try:
return self.Session.get(url, headers=headers)
except:
print("\r请求异常,重试中...")
continue
def post(self, url, post_json=None, headers=None):
while True:
try:
return self.Session.post(url, json=post_json, headers=headers)
except:
print("\r请求异常,重试中...")
continue
def main(self) -> None:
info = self.getInfomation()
try:
print('Login ID:', info['username'])
except:
input("xsid有误,请检查!")
return
self.ownerId = info['id']
self.username = info['username']
courseData = []
for index, i in enumerate(self.getCourseInfo()['data']):
print("{}、{}".format(index + 1, i['title']))
courseData.append({
"title": i['title'],
"parentId": i['id'],
"courseId": i['courseId']
})
while True:
try:
select_id = input('请选择课程(序号):')
self.title = courseData[int(select_id) - 1]['title']
break
except:
select_id = None
print("输入有误,请重新输入!")
continue
self.parentId = courseData[int(select_id) - 1]['parentId']
self.courseId = courseData[int(select_id) - 1]['courseId']
print("\n已选择 {}".format(self.title))
while True:
print("\n1、模拟观看网课\n2、题目作答(过期题目作答)\n3、题目作答(拷贝答案作答)\n4、导出题目与答案\n")
select = input("请选择模式(序号): ")
autoCompletPapers = AutoCompletPapers(self.Session, self.courseId, self.title)
if select == '1':
self.CompleteCourse = self.getCompleteCourse()
self.getCourseDes()
self.startLearnCourse()
elif select == '2':
autoCompletPapers.sendAnswers(mode='due')
elif select == '3':
autoCompletPapers.sendAnswers()
elif select == '4':
print("\n1、从过期题目中导出\n2、从已作答的题目中导出\n")
n = input("请选择模式(序号): ")
papersList = self.get(
f'https://www.cqooc.com/json/exam/papers?limit=200&start=1&courseId={self.courseId}&select=id,title&ts={getTs()}')
papersIds = {}
for i in papersList.json()['data']:
papersIds[i['id']] = i['title']
if n == '1':
autoCompletPapers.saveAnswersFromDue(papersIds)
elif n == '2':
autoCompletPapers.saveAnswersFromUser(papersIds)
else:
print("输入有误,请重新输入!")
elif select == '5':
pass
else:
print("输入有误,请重新输入!")
def getCourseDes(self):
# 课程章节名
self.Session.headers['Referer'] = f'https://www.cqooc.com/my/learn/mooc/structure?id={self.courseId}'
courseDes = {}
res = self.get(
f'https://www.cqooc.com/json/chapters?limit=200&start=1&sortby=selfId&status=1&courseId={self.courseId}&select=id,title,level,selfId,parentId&ts={getTs()}')
for i in res.json()['data']:
courseDes[i['id']] = i['title']
self.courseDes = courseDes
def getInfomation(self) -> json:
"""
获取基本信息
:return:
"""
return self.get('https://www.cqooc.com/user/session?xsid=' + cookie_xsid).json()
def getCourseInfo(self) -> json:
"""
获取课程信息
:return:
"""
self.Session.headers['Referer'] = 'https://www.cqooc.com/my/learn'
return self.get(
'https://www.cqooc.com/json/mcs?sortby=id&reverse=true&del=2&courseType=2&ownerId={}&limit=30'.format(
self.ownerId)).json()
def getCompleteCourse(self) -> list:
"""
获取已完成小节列表
:return:
"""
# 防止死循环,最大能获取到的课程小节数为750(150*5),若实际课程小节数大于750,请将此值改大!
maximumCycles = 5
limit, start = 150, 1
CourseIdList = []
while True:
maximumCycles -= 1
self.Session.headers['Referer'] = 'https://www.cqooc.com/learn/mooc/progress?id=' + self.courseId
data = self.get(
f'https://www.cqooc.com/json/learnLogs?limit={limit}&start={start}&courseId={self.courseId}&select=sectionId&username={self.username}&ts={getTs()}',
headers={
"referer": f'https://www.cqooc.com/learn/mooc/structure?id={self.courseId}'
})
learnLogs = data.json()['meta']
for i in data.json()['data']:
CourseIdList.append(i['sectionId'])
# 课程未获取完全
if len(CourseIdList) < int(learnLogs['total']):
start += limit
else:
return CourseIdList
if not maximumCycles:
print("超出最大循环次数,若已完成小节未获取完全,请将 492 行值改大!")
return CourseIdList
def startLearn(self) -> json:
self.Session.headers['Referer'] = 'https://www.cqooc.com/learn/mooc/structure?id=' + self.courseId
return self.post(url='https://www.cqooc.com/account/session/api/login/time', post_json={
"username": self.username
}).json()
def getLog(self, sectionId) -> json:
self.Session.headers['Referer'] = 'https://www.cqooc.com/learn/mooc/structure?id=' + self.courseId
return self.get(
'https://www.cqooc.com/json/learnLogs?sectionId=' + sectionId + '&username=' + self.username).json()
def checkProgress(self, courseId, sectionId, chapterId) -> None:
count = 0
while True:
self.Session.headers['Referer'] = 'https://www.cqooc.com/learn/mooc/structure?id=' + courseId
self.startLearn()
self.getLog(sectionId)
self.sleep_print(35)
self.startLearn()
time.sleep(1)
Log = self.post('https://www.cqooc.com/learnLog/api/add', post_json={
"action": 0,
"category": 2,
"chapterId": str(chapterId),
"courseId": str(courseId),
"ownerId": self.ownerId,
"parentId": str(self.parentId),
"sectionId": int(sectionId),
"username": self.username
})
if count <= 2:
date = 40
else:
date = 150
try:
if Log.json()['msg'] == '已经添加记录' or Log.json()['msg'] == 'No error':
return
else:
self.sleep_print(date)
count += 1
continue
except:
continue
def startLearnCourse(self):
sectionList = \
self.get('https://www.cqooc.com/json/chapter/lessons?courseId=' + self.courseId).json()['data'][0]['body']
index_t = 0
# CompleteCourse = self.getCompleteCourse()
print("已完成小节数: {} ".format(len(self.CompleteCourse)))
for chapterId, sectionIds in sectionList.items():
print('\r章节进度: {}/{}({:.2f}%) \t当前: {}'.format(index_t + 1, len(sectionList.items()),
((float(
(index_t + 1) / len(sectionList.items()))) * 100),
self.courseDes.get(chapterId)))
index_t += 1
for index, sectionId in enumerate(sectionIds):
print('\r\t小节进度: %d/%d(%.2f%%)' % (
index + 1, len(sectionIds), (float((index + 1) / len(sectionIds)) * 100)), end='')
if sectionId in self.CompleteCourse:
print('\t已完成,跳过!')
continue
print('\t成功!')
self.checkProgress(self.courseId, sectionId, chapterId)
if __name__ == '__main__':
AutoCompleteOnlineCourse().main()