From 672a8e3bb2b901b4c8285dc604ab80543cfd75ce Mon Sep 17 00:00:00 2001 From: glowzz <24627181@qq.com> Date: Mon, 24 Mar 2025 17:15:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8A=E4=BC=A0=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=88=B0Lsky=20Pro=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=E5=92=8C=E8=B6=85=E6=97=B6=E8=AE=BE=E7=BD=AE=EF=BC=9B=E8=B0=83?= =?UTF-8?q?=E6=95=B4Markdown=E6=96=87=E4=BB=B6=E4=BF=9D=E5=AD=98=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialogue_download_change.py | 70 ++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/dialogue_download_change.py b/dialogue_download_change.py index ea7373f..f1f9948 100644 --- a/dialogue_download_change.py +++ b/dialogue_download_change.py @@ -127,23 +127,51 @@ 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): +# """上传图片到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 + + + + + @@ -182,15 +210,17 @@ def process_image_file(markdown_file_path): # 构造新的文件名 file_name, file_ext = os.path.splitext(markdown_file_path) - new_file_path = f"{file_name}_lsky{file_ext}" + new_file_path = f"{file_name}{file_ext}" # 保存为新文件 with open(new_file_path, 'w', encoding='utf-8') as file: file.write(new_content) - messagebox.showinfo("完成", f"成功处理 {len(url_mapping)} 张图片") + #messagebox.showinfo("完成", f"成功处理 {len(url_mapping)} 张图片") + print(f"{file_name}:成功处理 {len(url_mapping)} 张图片") else: - messagebox.showwarning("警告", "没有图片被处理") + #messagebox.showwarning("警告", "没有图片被处理") + print("没有图片被处理")