一些优化

优化:提高多账户场景下任务执行效率
优化:使显示内容更简洁
This commit is contained in:
Qiuyelin 2022-09-07 14:07:04 +08:00
parent b217939063
commit 117c078497
4 changed files with 102 additions and 49 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
build build
dist dist
__pycache__
*.spec *.spec

View File

@ -6,7 +6,21 @@
# 使用方法 # 使用方法
以`UTF-8`的编码方式创建`config.json`文件。其内容格式如下: 1. 登录[安全微伴 (mycourse.cn)](http://weiban.mycourse.cn/#/login),在登录后的页面上运行刚才添加进收藏夹的脚本
2. 在浏览器地址栏运行
```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:`”,请手动加上,或者你可以将上述脚本[添加到收藏夹](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. 以`UTF-8`的编码方式创建`config.json`文件。其内容格式如下:
> `config.json` > `config.json`
> >
@ -22,20 +36,16 @@
> ] > ]
> ``` > ```
### 说明 5. 运行`main.py` 或者 [main.exe](https://github.com/pooneyy/weiban-tool/releases)。.
1. 登录[安全微伴 (mycourse.cn)](http://weiban.mycourse.cn/#/login),在登录后的页面上运行刚才添加进收藏夹的脚本 ### 更新日志
2. 在浏览器地址栏运行 ```text
版本 1.1 at 2022-09-06 15:08:08
优化:增加对多账户的支持。
``` 版本 1.2 at 2022-09-07 14:02:39
javascript:(function(){data=JSON.parse(localStorage.user);prompt('',JSON.stringify({token:data['token'],userId:data['userId'], tenantCode:data['tenantCode'], userProjectId: data['preUserProjectId']}));})(); 优化使用异步函数提高多账户场景下任务执行效率避免由于多个账户排队时任务流程过长Token过期导致后面的账户任务失败。
优化:使显示内容更简洁。
``` ```
浏览器地址栏如果吞掉了“`javascript:`”,请手动加上,或者你可以将上述脚本添加到收藏夹。
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)。

View File

@ -1,6 +1,7 @@
import time import time
import requests import requests
import json import json
import asyncio
class main: class main:
tenantCode = 0 tenantCode = 0
@ -20,6 +21,31 @@ class main:
def init(self): def init(self):
self.headers['x-token'] = self.x_token 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): def getProgress(self):
url = "https://weiban.mycourse.cn/pharos/project/showProgress.do" url = "https://weiban.mycourse.cn/pharos/project/showProgress.do"
data = { data = {
@ -91,7 +117,7 @@ class main:
return result return result
def start(self,courseId): async def start(self,courseId):
data = { data = {
'userProjectId': self.userProjectId, 'userProjectId': self.userProjectId,
'tenantCode': self.tenantCode, 'tenantCode': self.tenantCode,
@ -103,9 +129,9 @@ class main:
} }
res = requests.post("https://weiban.mycourse.cn/pharos/usercourse/study.do",data=data,headers=headers) res = requests.post("https://weiban.mycourse.cn/pharos/usercourse/study.do",data=data,headers=headers)
while json.loads(res.text)['code'] == -1: 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) 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): def finish(self,finishId):
params = { params = {
@ -115,4 +141,4 @@ class main:
} }
url = "https://weiban.mycourse.cn/pharos/usercourse/finish.do" url = "https://weiban.mycourse.cn/pharos/usercourse/finish.do"
requests.get(url=url,params=params) requests.get(url=url,params=params)
print("finish:"+finishId) print(f"finish:{finishId}\r",end='')

52
main.py
View File

@ -1,32 +1,48 @@
# @Author : Qiuyelin # @Author : Qiuyelin
# @repo : https://github.com/pooneyy/weiban-tool # @repo : https://github.com/pooneyy/weiban-tool
import os import os, sys
import Utils import Utils
import time
import json import json
import asyncio
async def weibanTask(user):
# tenantCode UserId x-token userProjectId # tenantCode UserId x-token userProjectId
try: tenantCode = user.get('tenantCode')
with open("config.json", "r+", encoding='utf8') as file: userId = user.get('userId')
usersConfig = json.load(file) x_token = user.get('token')
for user in usersConfig: userProjectId = user.get('userProjectId')
tenantCode = user['tenantCode'] realName = user.get('realName',userId)
userId = user['userId'] taskName = '未知的任务名'
x_token = user['token']
userProjectId = user['userProjectId']
main = Utils.main(tenantCode, userId, x_token, userProjectId) main = Utils.main(tenantCode, userId, x_token, userProjectId)
main.init() main.init()
try: try:
realName = main.getRealName()
taskName = main.getTaskName()
print(f"开始进行 {realName} 的任务:{taskName}")
finishIdList = main.getFinishIdList() finishIdList = main.getFinishIdList()
except json.decoder.JSONDecodeError:
print('账户信息错误或已经过期请重新获取。详见https://github.com/pooneyy/weiban-tool')
break
for i in main.getCourse(): for i in main.getCourse():
main.start(i) await main.start(i)
time.sleep(20) await asyncio.sleep(20)
main.finish(finishIdList[i]) main.finish(finishIdList[i])
os.system("pause") print(f"{realName} 的任务已完成")
except FileNotFoundError: except json.decoder.JSONDecodeError:print(f'{realName} 的账户信息错误或已经过期请重新获取。详见https://github.com/pooneyy/weiban-tool')
print('未找到 config.json详见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") os.system("pause")