Skip to content

Commit

Permalink
修复token刷新失败的问题(#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
aoaostar committed Oct 12, 2021
1 parent 85553e9 commit b9d39ec
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
22 changes: 14 additions & 8 deletions AliyunDrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# +-------------------------------------------------------------------
# | 阿里云盘上传类
# +-------------------------------------------------------------------
# | Author: Pluto <i@abcyun.cc>
# | Author: Pluto <i@aoaostar.com>
# +-------------------------------------------------------------------

import json
Expand Down Expand Up @@ -97,10 +97,11 @@ def load_file(self, filepath, realpath):

def token_refresh(self):
LOCK_TOKEN_REFRESH.acquire()

try:
data = {"refresh_token": DATA['config']['REFRESH_TOKEN'], 'Grant_Type': 'refresh_token'}
post = requests.post(
'https://auth.aliyundrive.com/v2/account/token',
'https://api.aliyundrive.com/token/refresh',
data=json.dumps(data),
headers={
'content-type': 'application/json;charset=UTF-8'
Expand Down Expand Up @@ -204,12 +205,17 @@ def upload(self):
upload_url = self.part_upload_url_list[self.part_number]['upload_url']
total_size = min(self.chunk_size, self.filesize)
fs.seek(self.part_number * total_size)
res = requests.put(
url=upload_url,
data=common.read_in_chunks(fs, 16 * 1024, total_size),
verify=False,
timeout=None
)
try:
res = requests.put(
url=upload_url,
data=common.read_in_chunks(fs, 16 * 1024, total_size),
verify=False,
timeout=None)
except Exception as e:
self.print(e, 'error')
self.part_upload_url_list = self.get_upload_url()
return self.upload()

if 400 <= res.status_code < 600:
common_get_xml_value = common.get_xml_tag_value(res.text, 'Message')
if common_get_xml_value == 'Request has expired.':
Expand Down
8 changes: 5 additions & 3 deletions Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# +-------------------------------------------------------------------
# | Client.py
# +-------------------------------------------------------------------
# | Author: Pluto <i@abcyun.cc>
# | Author: Pluto <i@aoaostar.com>
# +-------------------------------------------------------------------

# 配置信息
Expand Down Expand Up @@ -56,7 +56,7 @@ def init_config(self):
"RETRY": 0,
"RESIDENT": False,
"ALLOW_REPEAT": False,
"VERSIONS": "v2021.0904.1900"
"VERSIONS": "v2021.1011.1700"
}
if not os.path.exists(get_config_file_path()):
self.print('请配置好config.json后重试', 'error')
Expand Down Expand Up @@ -217,7 +217,9 @@ def save_task(self, task):

def print_config_info(self):
s = ''
for k in DATA['config'].keys():
config__keys = DATA['config'].keys()
for k in config__keys:
if k in ['REFRESH_TOKEN', 'DRIVE_ID']: continue
s += "\n\t\t%s:%s" % (k, DATA['config'][k])

content = '''=================================================
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ default_drive_id => ${data.default_drive_id}
"RETRY": 0,
"RESIDENT": false,
"ALLOW_REPEAT": true,
"VERSIONS":"v2021.0729.1800"
"VERSIONS":"v2021.0919.2000"
}
```
| 参数 | 注释 ||
Expand Down
7 changes: 2 additions & 5 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# +-------------------------------------------------------------------
# | 公共函数类
# +-------------------------------------------------------------------
# | Author: Pluto <i@abcyun.cc>
# | Author: Pluto <i@aoaostar.com>
# +-------------------------------------------------------------------
import base64
import hashlib
Expand All @@ -16,7 +16,6 @@
from urllib import parse
from xml.dom.minidom import parseString


from sqlite import sqlite

LOCK = threading.Lock()
Expand All @@ -42,7 +41,6 @@
}
}


def suicide(code=0):
os._exit(code)

Expand Down Expand Up @@ -260,8 +258,7 @@ def get_buff_hash_proof(access_token: string, realpath: string) -> dict:
with open(realpath, 'rb') as buff:
filesize = os.path.getsize(realpath)
if filesize == 0: return {'sha1': 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709', 'proof_code': ''};

hash = get_hash(realpath).upper()
hash = get_hash(realpath).upper()
m = html.unescape(parse.quote(access_token))

buffa = m
Expand Down
2 changes: 1 addition & 1 deletion example.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"RETRY": 0,
"RESIDENT": false,
"ALLOW_REPEAT": false,
"VERSIONS": "v2021.0917.1400"
"VERSIONS": "v2021.0924.1700"
}
13 changes: 9 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# +-------------------------------------------------------------------
# | main.py
# +-------------------------------------------------------------------
# | Author: Pluto <i@abcyun.cc>
# | Author: Pluto <i@aoaostar.com>
# +-------------------------------------------------------------------

import os
Expand All @@ -13,12 +13,12 @@

from AliyunDrive import AliyunDrive
from Client import Client
from common import DATA, print_error, get_db, get_timestamp, print_info, load_task, create_task, suicide, ctrl_c
from common import DATA, print_error, get_db, get_timestamp, print_info, load_task, create_task, suicide, ctrl_c, \
LOCK_TOKEN_REFRESH

if __name__ != '__main__':
suicide(0)


signal.signal(signal.SIGINT, ctrl_c)
signal.signal(signal.SIGTERM, ctrl_c)

Expand All @@ -32,6 +32,7 @@
# 输出配置信息
client.print_config_info()
db = get_db()

# 是否常驻运行
if not DATA['config']['RESIDENT']:
for v in client.tasks:
Expand All @@ -53,6 +54,8 @@


def thread(task):
LOCK_TOKEN_REFRESH.acquire()
LOCK_TOKEN_REFRESH.release()
drive = client.upload_file(task)
drive.finish_time = get_timestamp()
drive.spend_time = drive.finish_time - drive.start_time
Expand All @@ -67,6 +70,7 @@ def distribute_thread(tasks):
thread(task)
else:
with ThreadPoolExecutor(max_workers=int(DATA['config']['MAX_WORKERS'])) as executor:

for task in tasks:
# 提交线程
executor.submit(thread, task)
Expand All @@ -80,6 +84,7 @@ def crontab_tasks():
DATA['config']['CHUNK_SIZE'])).token_refresh()

time_period = DATA['time_period']
# 首次启动先执行一次
crontab_tasks()
while True:
if time_period <= 0:
Expand All @@ -94,7 +99,7 @@ def crontab_tasks():
time.sleep(1)


(ThreadPoolExecutor()).submit(crontab)
(ThreadPoolExecutor(1)).submit(crontab)

is_RESIDENT = DATA['config']['RESIDENT']
while True:
Expand Down

0 comments on commit b9d39ec

Please sign in to comment.