WIP: 优化上传图片到Lsky Pro的逻辑,增加重试机制和超时设置;调整Markdown文件保存路径处理 #2
@ -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("没有图片被处理")
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user