diff --git a/README.md b/README.md index 374140e..f5fe431 100644 --- a/README.md +++ b/README.md @@ -47,4 +47,7 @@ - 2023.09.17 - 更新`importData.py`获取题库的方式,登录相关的代码来自[Coaixy/weiban-tool](https://github.com/Coaixy/weiban-tool) - 题库开始记录选项id +- 2023.09.18 + - 支持从已结束的学习项目导入作答记录 + diff --git a/Utils.py b/Utils.py index c6296c4..69cb29a 100644 --- a/Utils.py +++ b/Utils.py @@ -15,30 +15,26 @@ class Parse: headers = {} - headers['x-token'] = "", headers["User-agent"] = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Mobile Safari/537.36 Edg/103.0.1264.77" - userProjectId = "" - examPlanIdList = [] - userExamIdList = [] def __init__(self, login_State): self.tenantCode = login_State.get('tenantCode') self.userId = login_State.get('userId') self.headers['x-token'] = login_State.get('token') + self.userProjectIds = [] + self.examPlanIdList = [] + self.userExamIdList = [] def get_Project_Info(self): url = f'https://weiban.mycourse.cn/pharos/index/listMyProject.do?timestamp={time.time()}' - data = { - 'tenantCode': self.tenantCode, - 'userId': self.userId, - 'ended': 2 - } - response = requests.post(url, data=data, headers=self.headers) - data = json.loads(response.text)['data'] - if len(data) <= 0:self.userProjectId = '' - else: - self.userProjectId = data[0]["userProjectId"] - self.taskName = data[0]["projectName"] + payload = {} + payload['tenantCode'] = self.tenantCode + payload['userId'] = self.userId + for ended in [1,2]: + payload['ended'] = ended + response = requests.post(url, data=payload, headers=self.headers) + data = json.loads(response.text)['data'] + self.userProjectIds.extend([i.get("userProjectId") for i in data]) def getStudentNumber(self): url = f"https://weiban.mycourse.cn/pharos/my/getInfo.do?timestamp={int(time.time())}" @@ -54,47 +50,44 @@ def getStudentNumber(self): def getExamPlanId(self): '''返回一个列表,包含课程全部考试计划的 id''' url = f'https://weiban.mycourse.cn/pharos/exam/listPlan.do?timestamp={int(time.time())}' - data = { - 'userProjectId': self.userProjectId, - 'tenantCode': self.tenantCode, - 'userId': self.userId - } - response = requests.post(url, data=data, headers=self.headers) - text = response.text - data = json.loads(text) - self.examPlanIdList = [i['examPlanId'] for i in data['data']] + payload = {} + payload['tenantCode'] = self.tenantCode, + payload['userId'] = self.userId + for userProjectId in self.userProjectIds: + payload['userProjectId'] = userProjectId + response = requests.post(url, data=payload, headers=self.headers) + data = json.loads(response.text)['data'] + self.examPlanIdList.extend([i['examPlanId'] for i in data]) def getUserExamId(self): '''返回一个列表,包含某一次考试全部试卷(重做试卷)的ID''' url = f'https://weiban.mycourse.cn/pharos/exam/listHistory.do?timestamp={int(time.time())}' for examPlanId in self.examPlanIdList: - data = { + payload = { 'examPlanId': examPlanId, 'tenantCode': self.tenantCode, 'userId': self.userId, 'isRetake': 2 } - response = requests.post(url, data=data, headers=self.headers) - text = response.text - data = json.loads(text) + response = requests.post(url, data=payload, headers=self.headers) + data = json.loads(response.text) for i in data['data']: self.userExamIdList.append(i['id']) def getPaperDetails(self): - '''返回一个列表,包含某一张试卷的详情''' + '''返回一个字典,包含某一张试卷的详情''' userQuestionsBank = {} url = f'https://weiban.mycourse.cn/pharos/exam/reviewPaper.do?timestamp={int(time.time())}' for userExamId in self.userExamIdList: - data = { + payload = { 'userExamId': userExamId, 'isRetake': 2, 'tenantCode': self.tenantCode, 'userId': self.userId } - response = requests.post(url, data=data, headers=self.headers) - text = response.text - data = json.loads(text) - for i in data['data']['questions']: + response = requests.post(url, data=payload, headers=self.headers) + data = json.loads(response.text)['data'] + for i in data['questions']: userQuestionsBank[i['id']] = {} userQuestionsBank[i['id']]['question'] = i['title'] userQuestionsBank[i['id']]['answer'] = [] @@ -194,27 +187,35 @@ def get_Login_State(account : dict) -> dict: realName = response.get('data').get('realName') print(f"用户 {user_id} {realName} 登录成功") return {"token":x_token,"userId":userId,"tenantCode":tenantCode,"realName":realName,"raw_id":user_id} - elif "账号与密码不匹配" in response["msg"] or "账号已被锁定" in response["msg"]: + elif "账号与密码不匹配" in response["msg"] or "账号已被锁定" in response["msg"] or "权限错误" in response["msg"]: print(f'用户 {user_id} 登录失败,错误码 {response["code"]} 原因为 {response["msg"]}') return {"is_locked":True,"raw_id":user_id} - else: + elif "验证码有误" in response["msg"]: print(f'用户 {user_id} 登录失败,错误码 {response["code"]} 原因为 {response["msg"]}') return get_Login_State(account) + else: + print(f'用户 {user_id} 意料外的登录失败,错误码 {response["code"]} 原因为 {response["msg"]}') + return {"is_locked":True,"raw_id":user_id} def getUserQuestionsBank(): global DEFAULT_SCHOOL_NAME account = {} print('输入学校名、帐号、密码,结束输入请按 Ctrl + C') - account['schoolName'] = input(f'输入学校名称(当前默认学校为 {DEFAULT_SCHOOL_NAME}):') + account['schoolName'] = input(f'输入学校名称 (当前默认学校为 {DEFAULT_SCHOOL_NAME} ): ') if account['schoolName'] == '':account['schoolName'] = DEFAULT_SCHOOL_NAME else:DEFAULT_SCHOOL_NAME = account['schoolName'] - account['id'] = input('输入学号:') - account['password'] = input('输入密码:') - questionsBankParse = Parse(get_Login_State(account)) - questionsBankParse.get_Project_Info() - questionsBankParse.getExamPlanId() - try:questionsBankParse.getUserExamId() - except KeyError:print("\033[0;31;40m输入的账户未找到作答记录\033[0m") - userQuestionsBank = questionsBankParse.getPaperDetails() - print(f'{questionsBankParse.getStudentNumber()} 的答题记录导入完毕。请接着',end='') + account['id'] = input('输入学号: ') + account['password'] = input('输入密码: ') + login_State = get_Login_State(account) + if login_State.get("is_locked") is True: + print(f"\033[0;31;40m账号 {account['id']} 已被锁定,请重新输入\033[0m") + userQuestionsBank = {} + else: + questionsBankParse = Parse(login_State) + questionsBankParse.get_Project_Info() + questionsBankParse.getExamPlanId() + try:questionsBankParse.getUserExamId() + except KeyError:print("\033[0;31;40m输入的账户未找到作答记录\033[0m") + userQuestionsBank = questionsBankParse.getPaperDetails() + print(f"从{account['id']} 的账户中导入 {len(userQuestionsBank)} 条答题记录。请接着",end='') return userQuestionsBank