Skip to content

Commit

Permalink
版本 1.2
Browse files Browse the repository at this point in the history
优化:提高多账户场景下任务执行效率
优化:使显示内容更简洁
  • Loading branch information
pooneyy committed Sep 7, 2022
1 parent b217939 commit aebdb79
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

build
dist
__pycache__
*.spec
54 changes: 32 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)。
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过期,导致后面的账户任务失败。
优化:使显示内容更简洁。
```
34 changes: 30 additions & 4 deletions Utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import requests
import json
import asyncio

class main:
tenantCode = 0
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand All @@ -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='')
62 changes: 39 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit aebdb79

Please sign in to comment.