From 16192e00a2eb04dead9ac11435a5fe9b48d200d1 Mon Sep 17 00:00:00 2001 From: glowzz <24627181@qq.com> Date: Mon, 24 Mar 2025 16:17:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=AF=95=E6=9C=BA=E5=88=B6=E5=92=8C=E8=B6=85=E6=97=B6=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E6=94=AF=E6=8C=81=E6=89=B9=E9=87=8F=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=B9=B6=E5=8F=91=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialogue_download_change.py | 67 ++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/dialogue_download_change.py b/dialogue_download_change.py index ea7373f..b643e93 100644 --- a/dialogue_download_change.py +++ b/dialogue_download_change.py @@ -127,23 +127,28 @@ def download_images(): else: messagebox.showwarning("警告", "没有图片被下载") -def upload_image_to_lsky_pro(image_path): - """上传图片到Lsky Pro图床""" - with open(image_path, 'rb') as img_file: - files = {'file': img_file} - headers = { - 'Authorization': f'Bearer {token}', - 'Accept': 'application/json' - } - response = requests.post(f'{lsky_pro_url}{upload_endpoint}', files=files, headers=headers) - if response.status_code == 200: - try: - data = response.json() - if data['status']: - return data['data']['links']['url'] - except: - pass - return None +def upload_image_to_lsky_pro(image_path, retries=3, timeout=30): + """上传图片到Lsky Pro图床,增加重试机制和超时设置""" + for attempt in range(retries): + try: + with open(image_path, 'rb') as img_file: + files = {'file': img_file} + headers = { + 'Authorization': f'Bearer {token}', + 'Accept': 'application/json' + } + response = requests.post(f'{lsky_pro_url}{upload_endpoint}', files=files, headers=headers, timeout=timeout) + if response.status_code == 200: + try: + data = response.json() + if data['status']: + return data['data']['links']['url'] + except: + pass + except requests.exceptions.RequestException as e: + print(f"上传失败,重试 {attempt + 1}/{retries}: {str(e)}") + time.sleep(3) # 重试前等待3秒 + return None @@ -309,24 +314,18 @@ def download_and_upload_images(): except Exception as e: messagebox.showerror("错误", f"处理过程中出错: {str(e)}") +def upload_images_batch_files(file_paths, max_concurrent_uploads=5): + """批量上传图片,限制并发上传数量""" + from concurrent.futures import ThreadPoolExecutor, as_completed - - -def upload_images_batch_files(file_paths): - # """ 你的图片上传逻辑(需自行实现) """ - for file_path in file_paths: - # 这里添加处理每个文件的逻辑 - if not file_path or not os.path.isfile(file_path): - messagebox.showwarning("警告", "请选择有效的Markdown文件。") - return - - print(f"Processing: {file_path}") - process_image_file(file_path) - - # 示例:读取markdown文件内容 - # with open(file_path, 'r') as f: - # content = f.read() - # 添加你的图片上传逻辑... + with ThreadPoolExecutor(max_workers=max_concurrent_uploads) as executor: + futures = {executor.submit(process_image_file, file_path): file_path for file_path in file_paths} + for future in as_completed(futures): + file_path = futures[future] + try: + future.result() + except Exception as e: + print(f"处理文件 {file_path} 时出错: {str(e)}") def select_files(): root = tk.Tk()