优化图片文件名处理逻辑,支持直接使用URL扩展名,并更新Markdown文件保存路径
This commit is contained in:
parent
5737b297ad
commit
4aee514ea5
@ -34,12 +34,18 @@ def choose_input_file():
|
||||
|
||||
def clean_filename(url):
|
||||
try:
|
||||
# 提取fdId参数
|
||||
# 首先检查URL是否有常见的图片扩展名
|
||||
image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')
|
||||
if url.lower().endswith(image_extensions):
|
||||
# 如果有扩展名,直接使用URL的最后部分作为文件名
|
||||
return os.path.basename(url)
|
||||
|
||||
# 如果没有常见扩展名,则检查fdId
|
||||
fd_id = re.search(r'fdId=([0-9a-f]+)', url)
|
||||
if fd_id:
|
||||
return f"{fd_id.group(1)}.jpg"
|
||||
|
||||
# 如果没有fdId,使用时间戳
|
||||
# 如果既没有扩展名也没有fdId,使用时间戳
|
||||
return f"image_{int(time.time())}.jpg"
|
||||
except:
|
||||
return f"image_{int(time.time())}.jpg"
|
||||
@ -100,9 +106,11 @@ def download_images():
|
||||
new_content = markdown_content
|
||||
for old_url, new_path in url_mapping.items():
|
||||
new_content = new_content.replace(old_url, new_path)
|
||||
|
||||
# 构造新的文件名
|
||||
file_name, file_ext = os.path.splitext(markdown_file_path)
|
||||
new_file_path = f"{file_name}_local{file_ext}"
|
||||
# 保存更新后的Markdown文件
|
||||
with open(markdown_file_path, 'w', encoding='utf-8') as file:
|
||||
with open(new_file_path, 'w', encoding='utf-8') as file:
|
||||
file.write(new_content)
|
||||
|
||||
messagebox.showinfo("完成", f"成功下载 {success_count} 张图片并更新Markdown文件")
|
||||
|
Loading…
x
Reference in New Issue
Block a user