From b9d39ec5bb53e0e9b920d1e097e04418f91756d0 Mon Sep 17 00:00:00 2001 From: aoaostar Date: Tue, 12 Oct 2021 17:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtoken=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AliyunDrive.py | 22 ++++++++++++++-------- Client.py | 8 +++++--- README.md | 2 +- common.py | 7 ++----- example.config.json | 2 +- main.py | 13 +++++++++---- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/AliyunDrive.py b/AliyunDrive.py index 7d895f1..e125344 100644 --- a/AliyunDrive.py +++ b/AliyunDrive.py @@ -2,7 +2,7 @@ # +------------------------------------------------------------------- # | 阿里云盘上传类 # +------------------------------------------------------------------- -# | Author: Pluto +# | Author: Pluto # +------------------------------------------------------------------- import json @@ -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' @@ -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.': diff --git a/Client.py b/Client.py index 8b2ba6d..4a815d7 100644 --- a/Client.py +++ b/Client.py @@ -2,7 +2,7 @@ # +------------------------------------------------------------------- # | Client.py # +------------------------------------------------------------------- -# | Author: Pluto +# | Author: Pluto # +------------------------------------------------------------------- # 配置信息 @@ -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') @@ -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 = '''================================================= diff --git a/README.md b/README.md index e6f7446..c62741d 100644 --- a/README.md +++ b/README.md @@ -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" } ``` | 参数 | 注释 | 值 | diff --git a/common.py b/common.py index 0da7f2e..0296f38 100644 --- a/common.py +++ b/common.py @@ -2,7 +2,7 @@ # +------------------------------------------------------------------- # | 公共函数类 # +------------------------------------------------------------------- -# | Author: Pluto +# | Author: Pluto # +------------------------------------------------------------------- import base64 import hashlib @@ -16,7 +16,6 @@ from urllib import parse from xml.dom.minidom import parseString - from sqlite import sqlite LOCK = threading.Lock() @@ -42,7 +41,6 @@ } } - def suicide(code=0): os._exit(code) @@ -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 diff --git a/example.config.json b/example.config.json index b0d608d..7a9e8af 100644 --- a/example.config.json +++ b/example.config.json @@ -11,5 +11,5 @@ "RETRY": 0, "RESIDENT": false, "ALLOW_REPEAT": false, - "VERSIONS": "v2021.0917.1400" + "VERSIONS": "v2021.0924.1700" } \ No newline at end of file diff --git a/main.py b/main.py index ee2264d..c06efa6 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ # +------------------------------------------------------------------- # | main.py # +------------------------------------------------------------------- -# | Author: Pluto +# | Author: Pluto # +------------------------------------------------------------------- import os @@ -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) @@ -32,6 +32,7 @@ # 输出配置信息 client.print_config_info() db = get_db() + # 是否常驻运行 if not DATA['config']['RESIDENT']: for v in client.tasks: @@ -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 @@ -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) @@ -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: @@ -94,7 +99,7 @@ def crontab_tasks(): time.sleep(1) -(ThreadPoolExecutor()).submit(crontab) +(ThreadPoolExecutor(1)).submit(crontab) is_RESIDENT = DATA['config']['RESIDENT'] while True: