From 117c0784978440e4f5537d237d588d081e2ce7c9 Mon Sep 17 00:00:00 2001 From: Qiuyelin <85266337+pooneyy@users.noreply.github.com> Date: Wed, 7 Sep 2022 14:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化:提高多账户场景下任务执行效率 优化:使显示内容更简洁 --- .gitignore | 1 + README.md | 54 ++++++++++++++++++++++++++++------------------- Utils.py | 34 ++++++++++++++++++++++++++---- main.py | 62 ++++++++++++++++++++++++++++++++++-------------------- 4 files changed, 102 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 02b9bee..7dc9450 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build dist +__pycache__ *.spec diff --git a/README.md b/README.md index c266e28..67fd62d 100644 --- a/README.md +++ b/README.md @@ -6,36 +6,46 @@ # 使用方法 -以`UTF-8`的编码方式创建`config.json`文件。其内容格式如下: - -> `config.json`: -> -> ```json -> [ -> {"token":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","userId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","tenantCode":"00000001","userProjectId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}, -> {"token":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","userId":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","tenantCode":"00000002","userProjectId":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"}, -> {"token":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","userId":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","tenantCode":"00000003","userProjectId":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"}, -> {#第4个账号信息#}, -> {#第5个账号信息#}, -> ... -> {#第n个账号信息#} -> ] -> ``` - -### 说明 - 1. 登录[安全微伴 (mycourse.cn)](http://weiban.mycourse.cn/#/login),在登录后的页面上运行刚才添加进收藏夹的脚本 2. 在浏览器地址栏运行 - ``` - javascript:(function(){data=JSON.parse(localStorage.user);prompt('',JSON.stringify({token:data['token'],userId:data['userId'], tenantCode:data['tenantCode'], userProjectId: data['preUserProjectId']}));})(); + ```javascript + javascript:(function(){data=JSON.parse(localStorage.user);prompt('',JSON.stringify({token:data['token'],userId:data['userId'], tenantCode:data['tenantCode'], userProjectId: data['preUserProjectId'], realName: data['realName']}));})(); ``` - 浏览器地址栏如果吞掉了“`javascript:`”,请手动加上,或者你可以将上述脚本添加到收藏夹。 + 浏览器地址栏如果吞掉了“`javascript:`”,请手动加上,或者你可以将上述脚本[添加到收藏夹](https://www.qiuyelin.com/getWei-banToken.html)。 3. 复制弹窗内的内容,**按照格式**添加到`config.json`。(格式不对会报错) [![1662441411827.png](http://png.eot.ooo/i/2022/09/06/6316d7c7f3567.png)](http://png.eot.ooo/i/2022/09/06/6316d7c7f3567.png) -4. 运行`main.py` 或者 [main.exe](https://github.com/pooneyy/weiban-tool/releases)。 \ No newline at end of file +4. 以`UTF-8`的编码方式创建`config.json`文件。其内容格式如下: + + > `config.json`: + > + > ```json + > [ + > {"token":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","userId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","tenantCode":"00000001","userProjectId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}, + > {"token":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","userId":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","tenantCode":"00000002","userProjectId":"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"}, + > {"token":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","userId":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","tenantCode":"00000003","userProjectId":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"}, + > {#第4个账号信息#}, + > {#第5个账号信息#}, + > ... + > {#第n个账号信息#} + > ] + > ``` + +5. 运行`main.py` 或者 [main.exe](https://github.com/pooneyy/weiban-tool/releases)。. + +### 更新日志 + +```text +版本 1.1 at 2022-09-06 15:08:08 + 优化:增加对多账户的支持。 + +版本 1.2 at 2022-09-07 14:02:39 + 优化:使用异步函数,提高多账户场景下任务执行效率,避免由于多个账户排队时任务流程过长,Token过期,导致后面的账户任务失败。 + 优化:使显示内容更简洁。 +``` + diff --git a/Utils.py b/Utils.py index b123e2c..f655012 100644 --- a/Utils.py +++ b/Utils.py @@ -1,6 +1,7 @@ import time import requests import json +import asyncio class main: tenantCode = 0 @@ -20,6 +21,31 @@ def __init__(self, code, id, token,projectId): def init(self): self.headers['x-token'] = self.x_token + def getRealName(self): + url = f"https://weiban.mycourse.cn/pharos/my/getInfo.do?timestamp={int(time.time())}" + data = { + 'tenantCode': self.tenantCode, + 'userId': self.userId + } + response = requests.post(url, data=data, headers=self.headers) + text = response.text + data = json.loads(text) + return data['data']['realName'] + + def getTaskName(self): + url = f"https://weiban.mycourse.cn/pharos/index/listStudyTask.do?timestamp={int(time.time())}" + data = { + 'tenantCode': self.tenantCode, + 'userId': self.userId, + 'limit': 2 + } + response = requests.post(url, data=data, headers=self.headers) + text = response.text + data = json.loads(text) + for i in data['data']: + if self.userProjectId in i['userProjectId']:taskName = i['projectName'] + return taskName + def getProgress(self): url = "https://weiban.mycourse.cn/pharos/project/showProgress.do" data = { @@ -91,7 +117,7 @@ def getFinishIdList(self): return result - def start(self,courseId): + async def start(self,courseId): data = { 'userProjectId': self.userProjectId, 'tenantCode': self.tenantCode, @@ -103,9 +129,9 @@ def start(self,courseId): } res = requests.post("https://weiban.mycourse.cn/pharos/usercourse/study.do",data=data,headers=headers) while json.loads(res.text)['code'] == -1: - time.sleep(5) + await asyncio.sleep(5) res = requests.post("https://weiban.mycourse.cn/pharos/usercourse/study.do",data=data,headers=headers) - print("start:"+courseId) + print(f"start:{courseId}\r",end='') def finish(self,finishId): params = { @@ -115,4 +141,4 @@ def finish(self,finishId): } url = "https://weiban.mycourse.cn/pharos/usercourse/finish.do" requests.get(url=url,params=params) - print("finish:"+finishId) + print(f"finish:{finishId}\r",end='') diff --git a/main.py b/main.py index 8b95495..700c698 100644 --- a/main.py +++ b/main.py @@ -1,32 +1,48 @@ # @Author : Qiuyelin # @repo : https://github.com/pooneyy/weiban-tool -import os +import os, sys import Utils -import time import json +import asyncio -# tenantCode UserId x-token userProjectId -try: - with open("config.json", "r+", encoding='utf8') as file: - usersConfig = json.load(file) - for user in usersConfig: - tenantCode = user['tenantCode'] - userId = user['userId'] - x_token = user['token'] - userProjectId = user['userProjectId'] - main = Utils.main(tenantCode, userId, x_token, userProjectId) - main.init() - try: - finishIdList = main.getFinishIdList() - except json.decoder.JSONDecodeError: - print('账户信息错误或已经过期,请重新获取。详见:https://github.com/pooneyy/weiban-tool') - break +async def weibanTask(user): + # tenantCode UserId x-token userProjectId + tenantCode = user.get('tenantCode') + userId = user.get('userId') + x_token = user.get('token') + userProjectId = user.get('userProjectId') + realName = user.get('realName',userId) + taskName = '未知的任务名' + main = Utils.main(tenantCode, userId, x_token, userProjectId) + main.init() + try: + realName = main.getRealName() + taskName = main.getTaskName() + print(f"开始进行 {realName} 的任务:{taskName}") + finishIdList = main.getFinishIdList() for i in main.getCourse(): - main.start(i) - time.sleep(20) + await main.start(i) + await asyncio.sleep(20) main.finish(finishIdList[i]) - os.system("pause") -except FileNotFoundError: - print('未找到 config.json!详见:https://github.com/pooneyy/weiban-tool') + print(f"{realName} 的任务已完成") + except json.decoder.JSONDecodeError:print(f'{realName} 的账户信息错误或已经过期,请重新获取。详见:https://github.com/pooneyy/weiban-tool') + except KeyboardInterrupt:print(f'{realName} 的任务被手动终止') + +async def main(): + usersConfig = {} + try: + with open("config.json", "r+", encoding='utf8') as file: + try:usersConfig = json.load(file) + except json.decoder.JSONDecodeError:print('配置文件格式错误,请仔细检查 config.json 。详见:https://github.com/pooneyy/weiban-tool') + tasks=[] + for user in usersConfig: + tasks.append(weibanTask(user)) + try:await asyncio.gather(*tasks) + except asyncio.CancelledError:pass + except FileNotFoundError:print('未找到 config.json!详见:https://github.com/pooneyy/weiban-tool') + +if __name__ =='__main__': + try:asyncio.run(main()) + except KeyboardInterrupt:print(f'\n任务被手动终止') os.system("pause") \ No newline at end of file