From fee059355ba6d91ff2b523377d45d6f701266243 Mon Sep 17 00:00:00 2001 From: yuban10703 <2846021566@qq.com> Date: Tue, 21 Sep 2021 22:30:09 +0800 Subject: [PATCH] fix bug --- main.py | 113 +++++++++++++++++++++++++++++-------------------- models/APIs.py | 28 ++++++++++-- 2 files changed, 91 insertions(+), 50 deletions(-) diff --git a/main.py b/main.py index bd290e3..4ca56fa 100644 --- a/main.py +++ b/main.py @@ -65,6 +65,32 @@ def knowledge_sort(self, all_knowledge): keys_sorted = natsorted(dict_all_knowledge.keys()) return [dict_all_knowledge[k] for k in keys_sorted] + def pass_video(self, video_duration, cpi, dtoken, otherInfo, clazzid, jobid, objectid): + sec = 58 + for playingTime in range(video_duration + 120): + if sec >= 58: + sec = 0 + res = self.chaoxing.pass_video( + cpi, + dtoken, + otherInfo, + playingTime, + clazzid, + video_duration, + jobid, + objectid + ) + # print(res) + if res.get('isPassed'): + self.print_progress_bar(video_duration, video_duration) + break + elif res.get('error'): + raise Exception('请联系作者') + continue + self.print_progress_bar(playingTime, video_duration) + sec += 1 + time.sleep(1) + def main(self): if not self.chaoxing.login(): print('登录失败') @@ -86,56 +112,49 @@ def main(self): for knowledge_id_data in self.knowledge_sort( course_data['data'][0]['course']['data'][0]['knowledge']['data']): # 遍历排序过的任务点 # print(knowledge_id_data) - knowledge_card_web_text = self.chaoxing.get_knowledge_card( - course_all_id_data[int(index_id)]['clazzid'], - course_all_id_data[int(index_id)]['courseid'], - knowledge_id_data['id']) - # print(knowledge_card_web_text) - # print(knowledge_id_data['id']) - attachments: dict = self.get_attachments(knowledge_card_web_text) - if not attachments: - continue - if not attachments.get('attachments'): - continue - print(f'当前章节:{knowledge_id_data["label"]}:{knowledge_id_data["name"]}') - for attachment in attachments['attachments']: # 遍历任务点 - # print(attachment) - if attachment.get('type') != 'video': # 只刷视频 + knowledge_json = self.chaoxing.get_knowledge_json( + knowledge_id_data['id'], + course_all_id_data[int(index_id)]['clazzid'] + ) + # print(knowledge_json) + tabs = len(knowledge_json['data'][0]['card']['data']) + # print(tabs) + for tab_index in range(tabs): + knowledge_card_web_text = self.chaoxing.get_knowledge_card( + course_all_id_data[int(index_id)]['clazzid'], + course_all_id_data[int(index_id)]['courseid'], + knowledge_id_data['id'], + tab_index + ) + # print(knowledge_card_web_text) + # print(knowledge_id_data['id']) + attachments: dict = self.get_attachments(knowledge_card_web_text) + if not attachments: continue - print(f"当前视频:{attachment['property']['name']}") - if attachment.get('isPassed'): # 跳过已完成的 - self.print_progress_bar(1, 1) + if not attachments.get('attachments'): continue - video_info = self.chaoxing.get_d_token( - attachment['objectId'], - attachments['defaults']['fid'] - ) - # print(video_info) - # playingTime = 0 - sec = 58 - for playingTime in range(video_info['duration'] + 1): - if sec >= 58 or playingTime == video_info['duration']: - sec = 0 - res = self.chaoxing.pass_video( - attachments['defaults']['cpi'], - video_info['dtoken'], - attachments['attachments'][0]['otherInfo'], - playingTime, - course_all_id_data[int(index_id)]['clazzid'], - video_info['duration'], - attachments['attachments'][0]['jobid'], - video_info['objectid'] - ) - # print(res) - if res.get('isPassed'): - self.print_progress_bar(video_info['duration'], video_info['duration']) - break - elif res.get('error'): - raise Exception('请联系作者') + print(f'当前章节:{knowledge_id_data["label"]}:{knowledge_id_data["name"]}') + for attachment in attachments['attachments']: # 遍历任务点 + # print(attachment) + if attachment.get('type') != 'video': # 只刷视频 + continue + print(f"当前视频:{attachment['property']['name']}") + if attachment.get('isPassed'): # 跳过已完成的 + self.print_progress_bar(1, 1) continue - self.print_progress_bar(playingTime, video_info['duration']) - sec += 1 - time.sleep(1) + video_info = self.chaoxing.get_d_token( + attachment['objectId'], + attachments['defaults']['fid'] + ) + self.pass_video( + video_info['duration'], + attachments['defaults']['cpi'], + video_info['dtoken'], + attachments['attachments'][0]['otherInfo'], + course_all_id_data[int(index_id)]['clazzid'], + attachments['attachments'][0]['jobid'], + video_info['objectid'] + ) time.sleep(random.randint(1, 3)) diff --git a/models/APIs.py b/models/APIs.py index e6793a2..ab23d05 100644 --- a/models/APIs.py +++ b/models/APIs.py @@ -97,13 +97,28 @@ def get_jobs_nodes(self, courseid, clazzid, nodes): return self.client.post(url, data=data).json() @retry(stop_max_attempt_number=3) - def get_knowledge_card(self, clazzid, courseid, knowledgeid): + def get_knowledge_json(self, id, courseid): + url = 'https://mooc1-api.chaoxing.com/gas/knowledge' + enc = self.get_infEnc_and_time() + params = { + 'id': id, + 'courseid': courseid, + 'fields': 'id,parentnodeid,indexorder,label,layer,name,begintime,createtime,lastmodifytime,status,jobUnfinishedCount,clickcount,openlock,card.fields(id,knowledgeid,title,knowledgeTitile,description,cardorder).contentcard(all)', + 'view': 'json', + 'token': "4faa8662c59590c6f43ae9fe5b002b42", + '_time': enc[0], + 'inf_enc': enc[1] + } + return self.client.get(url, params=params).json() + + @retry(stop_max_attempt_number=3) + def get_knowledge_card(self, clazzid, courseid, knowledgeid, num): url = 'https://mooc1-api.chaoxing.com/knowledge/cards' params = { 'clazzid': clazzid, 'courseid': courseid, 'knowledgeid': knowledgeid, - 'num': 0, + 'num': num, 'isPhone': 1, 'control': True, 'cpi': self.personid @@ -120,7 +135,7 @@ def get_d_token(self, objectid, fid): } return self.client.get(url, params=params).json() - @retry(stop_max_attempt_number=3) + @retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=2000) def pass_video(self, personid, dtoken, otherInfo, playingTime, clazzId, duration, jobid, objectId): url = 'https://mooc1-api.chaoxing.com/multimedia/log/a/{}/{}'.format(personid, dtoken) # print(url) @@ -142,6 +157,13 @@ def pass_video(self, personid, dtoken, otherInfo, playingTime, clazzId, duration } return self.client.get(url, params=params).json() + def get_infEnc_and_time(self): + m_time = str(int(time.time() * 1000)) + m_token = '4faa8662c59590c6f43ae9fe5b002b42' + m_encrypt_str = 'token=' + m_token + '&_time=' + m_time + '&DESKey=Z(AfY@XS' + m_inf_enc = md5(m_encrypt_str.encode('utf-8')).hexdigest() + return m_time, m_inf_enc + def get_enc(self, clazzId, jobid, objectId, playingTime, duration): # https://github.com/ZhyMC/chaoxing-xuexitong-autoflush/blob/445c8d8a8cc63472dd90cdf2a6ab28542c56d93b/logger.js return md5(