forked from CCSU-Fan/zhijiaoyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmooc.py
742 lines (554 loc) · 23.4 KB
/
mooc.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import requests
import urllib3
import random
import json
import time
import random
# 解决警告
urllib3.disable_warnings()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
session = requests.Session()
session.headers = headers
class Mooc:
def __init__(self):
self.prepareWork()
self.getCookie()
self.download_code()
self.login()
self.courseNeedInfo()
def prepareWork(self):
self.userName = '' #输入你的账号
self.passWord = '' #输入你的密码
# 1 评论 0 不评论
self.whetherNeedPinlun = 1
def getCookie(self):
url = 'https://mooc.icve.com.cn/'
session.get(url, verify=False)
# 下载验证码到本件夹下
def download_code(self):
r = random.random()
url = "https://mooc.icve.com.cn/portal/LoginMooc/getVerifyCode?ts=" + str(r)
res = session.get(url, verify=False)
img = res.content
with open('verifycode.png', 'wb') as f:
f.write(img)
def login(self):
url = 'https://mooc.icve.com.cn/portal/LoginMooc/loginSystem'
verifyCode = input("请输入验证码:")
data = {
'userName': self.userName,
'password': self.passWord,
'verifycode': verifyCode
}
res = session.post(url, data=data, verify=False).json()
if(res['code'] != 1):
print('登录失败, 请检查账号 密码 验证码.')
exit()
else:
self.token = res['token']
# 姓名
self._name = res['displayName']
print('登录成功! 欢迎你: ' + self._name)
def courseNeedInfo(self):
url = 'https://mooc.icve.com.cn/portal/Course/getMyCourse?isFinished=0&page=1&pageSize={}'.format(20)
res = json.loads(session.get(url, verify=False).text)
if res['code'] != 1:
print('课程获取失败!')
exit()
courseList = res['list']
for i in range(len(courseList)):
courseName = courseList[i]['courseName']
print(str(i+1) + ': ' + courseName)
i = int(input('你所学课程如上, 请选择: ')) - 1
courseInfo = courseList[i]
self.Id = courseList[i]['Id']
self.courseOpenId = courseList[i]['courseOpenId']
self.stuId = courseList[i]['stuId']
time.sleep(3)
self.getProcessList()
def getProcessList(self):
url = 'https://mooc.icve.com.cn/study/learn/getProcessList'
data = {
'courseOpenId': self.courseOpenId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('章节信息获取失败!')
exit()
moduleList = res['proces']['moduleList']
for module in moduleList:
Mlist = []
moduleId = module['id']
moduleName = module['name']
moduleResId = module['ResId']
modulePercent = module['percent']
if moduleResId != "":
time.sleep(1)
continue
Mdic = {
'moduleId': moduleId,
'moduleName': moduleName
}
Mlist.append(Mdic)
if modulePercent == 100:
print('目录: ' + moduleName +', 进度100%(自动跳过)')
time.sleep(1)
continue
print('目录: ' + moduleName)
self.getTopicId(Mlist)
time.sleep(3)
def getTopicId(self, Mlist):
url = 'https://mooc.icve.com.cn/study/learn/getTopicByModuleId'
for moduleInfo in Mlist:
moduleId = moduleInfo['moduleId']
data = {
'courseOpenId': self.courseOpenId,
'moduleId': moduleId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('TopicId获取出错!')
exit()
topicList = res['topicList']
for topic in topicList:
Tlist = []
topicId = topic['id']
topicName = topic['name']
studyStatus = topic['studyStatus']
if studyStatus == 1:
print(' 子目录: ' + topicName[:10] + '..' + ', 进度100%(自动跳过)')
time.sleep(3)
continue
Tdic = {
'topicId': topicId,
'moduleId': moduleId
}
Tlist.append(Tdic)
print(' 子目录: ' + topicName)
self.getCellId(Tlist)
time.sleep(3)
time.sleep(5)
def getCellId(self, Tlist):
url = 'https://mooc.icve.com.cn/study/learn/getCellByTopicId'
for topicInfo in Tlist:
topicId = topicInfo['topicId']
moduleId = topicInfo['moduleId']
data = {
'courseOpenId': self.courseOpenId,
'topicId': topicId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('CellId获取失败!')
exit()
cellList = res['cellList']
for cell in cellList:
Clist = []
cellId = cell['Id']
categoryName = cell['categoryName']
cellName = cell['cellName']
isStudyFinish = cell['isStudyFinish']
resId = cell['resId']
childNodeList = cell['childNodeList']
Cdic = {
'cellId': cellId,
'categoryName': categoryName,
'cellName': cellName,
'moduleId': moduleId,
'topicId': topicId,
'isStudyFinish': isStudyFinish,
'resId': resId,
'childNodeList': childNodeList
}
Clist.append(Cdic)
self.doIt(Clist)
time.sleep(3)
time.sleep(5)
def doIt(self, Clist):
for cell in Clist:
cellId = cell['cellId']
categoryName = cell['categoryName']
cellName = cell['cellName'][:10] + '..'
moduleId = cell['moduleId']
topicId = cell['topicId']
isStudyFinish = cell['isStudyFinish']
resId = cell['resId']
childNodeList = cell['childNodeList']
if isStudyFinish:
print(' 文件: ' + cellName + ', 进度100%(自动跳过)')
continue
if categoryName == '视频' or categoryName == '音频':
print(' 文件: ' + cellName + ', 开始刷课!!')
print(' 请等待..')
shuake = Shuake(self.courseOpenId, cellId, moduleId)
shuake.VideoInfo()
shuake.video()
if self.whetherNeedPinlun:
shuake.pinglun()
elif categoryName =='ppt' or categoryName == 'ppt文档' or categoryName == 'office文档' or categoryName == 'office' or categoryName == '文档' or categoryName == '图片':
print(' 文件: ' + cellName + ', 开始刷课!!')
print(' 请等待..')
shuake = Shuake(self.courseOpenId, cellId, moduleId)
shuake.pptInfo()
shuake.wendang()
if categoryName == '文档':
shuake.wendangAddTime()
if self.whetherNeedPinlun:
shuake.pinglun()
elif categoryName == '讨论':
print(' 文件: ' + cellName + ', 开始刷课!!')
print(' 请等待..')
shuake = Shuake(self.courseOpenId, cellId, moduleId)
shuake.taolun(resId)
elif categoryName == '子节点':
for chiledCell in childNodeList:
childClist = []
cellIdC = chiledCell['Id']
categoryNameC = chiledCell['categoryName']
cellNameC = chiledCell['cellName']
resIdC = chiledCell['resId']
isStudyFinishC= chiledCell['isStudyFinish']
childNodeListC = []
childCdic = {
'cellId': cellIdC,
'categoryName': categoryNameC,
'cellName': cellNameC,
'moduleId': moduleId,
'topicId': topicId,
'resId': resIdC,
'isStudyFinish': isStudyFinishC,
'childNodeList': childNodeListC
}
childClist.append(childCdic)
self.doIt(childClist)
time.sleep(3)
elif categoryName == '作业' or categoryName == '测验':
print(' 文件: ' + cellName + ', 开始刷课!!')
print(' 请等待..')
print(' notice: 作业/测验会刷两次')
stuWorkCount, ReplyCount, agreeWorkExam, paperType = self.getWorkExamData(resId)
if agreeWorkExam != 'agree':
print(' 作业/测试时间已过期, 跳过')
else:
if ReplyCount == -1 or ReplyCount >=2 or paperType != 1:
time.sleep(1)
studentWorkId = self.workDetail(resId)
if studentWorkId == "":
time.sleep(3)
uniqueId, bigQuestions, questions = self.workExamPerview(resId)
for bigQuestion in bigQuestions:
if bigQuestion['Title'] == '简答题':
print(' 有简答题, 请手动实现')
else:
examList = []
for question in questions:
DAdic = {
'Answer': '1',
'questionId': question['questionId'],
'questionType': question['questionType']
}
examList.append(DAdic)
time.sleep(1)
shuake = Shuake(self.courseOpenId, cellId, moduleId)
shuake.zuoye(examList, uniqueId)
shuake.zuoyeSubmit(uniqueId, resId)
ZYDic = {
'cellId': cellId,
'categoryName': categoryName,
'cellName': cellName,
'moduleId': moduleId,
'topicId': topicId,
'isStudyFinish': False,
'resId': resId,
'childNodeList': []
}
ZYList = [ZYDic]
time.sleep(3)
self.doIt(ZYList)
else:
# 作业/测试做第二次执行
time.sleep(10)
examList = self.ExamHistory(resId, studentWorkId, categoryName)
if examList == []:
print(' 有简答题, 请手动实现')
else:
time.sleep(3)
uniqueId, bigQuestions, questions = self.workExamPerview(resId)
time.sleep(1)
shuake = Shuake(self.courseOpenId, cellId, moduleId)
shuake.zuoye(examList, uniqueId)
shuake.zuoyeSubmit(uniqueId, resId)
else:
print(' 作业/测试只能做一次或需要审批, 请手动实现')
time.sleep(2)
else:
print(' 该程序处理不了!')
time.sleep(2)
def getWorkExamData(self, resId):
url = 'https://mooc.icve.com.cn/study/workExam/getWorkExamData'
data = {
'courseOpenId': self.courseOpenId,
'workExamId': resId,
'workExamType': 0
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('获取做作业数据失败!')
exit()
stuWorkCount = res['workExam']['stuWorkCount']
ReplyCount = res['workExam']['ReplyCount']
agreeWorkExam = res['workExam']['agreeWorkExam']
paperType = res['workExam']['paperType']
return stuWorkCount, ReplyCount, agreeWorkExam, paperType
def workDetail(self, resId):
url = 'https://mooc.icve.com.cn/study/workExam/detail'
data = {
'courseOpenId': self.courseOpenId,
'workExamId': resId,
'workExamType': 0
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('获取做作业Id失败!')
exit()
resList = res['list']
if resList == []:
return ""
studentWorkId = resList[0]['Id']
return studentWorkId
def ExamHistory(self, resId, studentWorkId, categoryName):
url = 'https://mooc.icve.com.cn/study/workExam/history'
if categoryName == '测验':
workExamType = 1
else:
workExamType = 0
data = {
'courseOpenId': self.courseOpenId,
'workExamId': resId,
'studentWorkId': studentWorkId,
'workExamType': workExamType
}
res = json.loads(session.post(url, headers=headers, data=data, verify=False).text)
if res['code'] != 1:
print('获取历史作业答案失败')
exit()
workExamData = res['workExamData']
bigQuestions = json.loads(workExamData)['bigQuestions']
for i in bigQuestions:
id = i['Id']
Title = i ['Title']
if Title == "简答题":
return []
questions = json.loads(workExamData)['questions']
_list = []
for i in range(len(questions)):
questionId = questions[i]['questionId']
Answer = questions[i]['Answer']
questionType = questions[i]['questionType']
DAdic = {
'Answer': Answer,
'questionId': questionId,
'questionType': questionType
}
_list.append(DAdic)
return _list
def workExamPerview(self, resId):
url = 'https://mooc.icve.com.cn/study/workExam/workExamPreview'
data = {
'courseOpenId': self.courseOpenId,
'workExamId': resId,
'agreeHomeWork': 'agree',
'workExamType': 0
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('获取作业题目失败!')
exit()
uniqueId = res['uniqueId']
workExamData = res['workExamData']
if workExamData == "":
bigQuestions = res['paperData']['bigQuestions']
questions = res['paperData']['questions']
else:
bigQuestions = json.loads(workExamData)['bigQuestions']
questions = json.loads(workExamData)['questions']
return uniqueId, bigQuestions, questions
class Shuake:
def __init__(self, courseOpenId, cellId, moduleId):
self.courseOpenId = courseOpenId
self.cellId = cellId
self.moduleId = moduleId
def VideoInfo(self):
url = 'https://mooc.icve.com.cn/study/learn/viewDirectory'
data = {
'courseOpenId': self.courseOpenId,
'cellId': self.cellId,
'fromType': 'stu',
'moduleId': self.moduleId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('获取视频信息出错!')
exit()
self.videoPercent = res['VideoPercent']
courseCell = res['courseCell']
self.CategoryName = courseCell['CategoryName']
self.VideoTimeLong = courseCell['VideoTimeLong']
self.currentTime = res['currentTime']
def pptInfo(self):
url = 'https://mooc.icve.com.cn/study/learn/viewDirectory'
data = {
'courseOpenId': self.courseOpenId,
'cellId': self.cellId,
'fromType': 'stu',
'moduleId': self.moduleId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('获取视频信息出错!')
exit()
self.videoPercent = res['VideoPercent']
courseCell = res['courseCell']
self.CategoryName = courseCell['CategoryName']
self.PageCount = courseCell['PageCount']
def video(self):
url = 'https://mooc.icve.com.cn/study/learn/statStuProcessCellLogAndTimeLong'
otherTime = self.VideoTimeLong - self.currentTime
forNum = int(otherTime / 60) + 1
for i in range(forNum):
time.sleep(60)
auvideoLength = self.currentTime + 60 * (i+1)
if auvideoLength > self.VideoTimeLong:
auvideoLength = self.VideoTimeLong
data = {
'courseId': '',
'courseOpenId': self.courseOpenId,
'moduleId': self.moduleId,
'cellId': self.cellId,
'auvideoLength': auvideoLength,
'videoTimeTotalLong': self.VideoTimeLong,
'sourceForm': '993'
}
res = json.loads(session.post(url, data=data, verify=False).text)
# print('视频返回信息: ' + str(res))
if res['code'] != 1:
print('刷视频出错!')
exit()
def wendang(self):
url = 'https://mooc.icve.com.cn/study/learn/statStuProcessCellLogAndTimeLong'
data = {
'courseId': '',
'courseOpenId': self.courseOpenId,
'moduleId': self.moduleId,
'cellId': self.cellId,
'videoTimeTotalLong': 0,
'sourceForm': 1030
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('刷文档出错!')
exit()
def taolun(self, resId):
url = 'https://mooc.icve.com.cn/study/discussion/addStuViewTopicRemember'
data = {
'courseOpenId': self.courseOpenId,
'topicId': resId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['msg'] not in ["浏览成功", "已浏览主题"]:
print('讨论类文件出错!')
exit()
def pinglun(self):
for i in range(1, 5):
url = 'https://mooc.icve.com.cn/study/learn/saveAllReply'
if i == 2:
star = 5
else:
star = 0
textDic = {
"ResId": "",
"replyToUserId": "",
"replyToDisplayName": "",
"Content": "<p>{}</p>".format(random.choice(["好", "无", "内容丰富", "。", "通俗易懂", "课件内容丰富详细", "没有问题", "很详细"])),
"CourseOpenId": self.courseOpenId,
"CategoryId": "bbszhtlq-{}".format(self.courseOpenId),
"cellId": self.cellId,
"star": str(star),
"SignType": i
}
data = {
'replyData': str(textDic),
'urlList': '[]'
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('评论出错!')
exit()
time.sleep(4)
print(' 评论完成!!')
def wendangAddTime(self):
time.sleep(3)
url = 'https://mooc.icve.com.cn/study/learn/computatlearningTimeLong'
data = {
'courseId': '',
'courseOpenId': self.courseOpenId,
'moduleId': self.moduleId,
'cellId': self.cellId,
'auvideoLength': 10,
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('刷时间出错!')
exit()
def zuoye(self, examList, uniqueId):
for exam in examList:
# 答案
Answer = exam['Answer']
questionId = exam['questionId']
questionType = exam['questionType']
url = 'https://mooc.icve.com.cn/study/workExam/onlineHomeworkAnswer'
data = {
'studentWorkId': '',
'questionId': questionId,
'workExamType': 0,
'paperStuQuestionId':'',
'online': 1,
'answer': Answer,
'userId': '',
'questionType': questionType,
'uniqueId': uniqueId
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('回答作业失败')
exit()
# print("回答题目Info:" + str(res))
time.sleep(5)
def getDaAn(self, resId, studentWorkId):
url = 'https://mooc.icve.com.cn/study/workExam/homeWork/history.html'
params = {
'courseOpenId': self.courseOpenId,
'workExamId': resId,
'studentWorkId': studentWorkId,
'workExamType': 0
}
res = session.get(url, params=params, verify=False).text
def zuoyeSubmit(self, uniqueId, workExamId):
url = 'https://mooc.icve.com.cn/study/workExam/workExamSave'
data = {
'uniqueId': uniqueId,
'workExamId': workExamId,
'workExamType': 1,
'courseOpenId': self.courseOpenId,
'paperStructUnique': '',
'useTime': random.randint(120, 180)
}
res = json.loads(session.post(url, data=data, verify=False).text)
if res['code'] != 1:
print('作业提交失败!')
exit()
if __name__ == "__main__":
Mooc()
print('谢谢使用!')