From a5dbe72fe9f3ff5320c7253ab0d55bb5693e7e8e Mon Sep 17 00:00:00 2001 From: glowzz <24627181@qq.com> Date: Mon, 24 Mar 2025 14:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0Markdown=E6=96=87=E4=BB=B6=E4=B8=AD=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialogue_download_change.py | 90 ++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/dialogue_download_change.py b/dialogue_download_change.py index e50270d..ea7373f 100644 --- a/dialogue_download_change.py +++ b/dialogue_download_change.py @@ -11,7 +11,8 @@ import shutil # 配置信息 local_image_folder = 'downloaded_images' -lsky_pro_url = 'http://192.168.107.248:18089/api/v1' +#lsky_pro_url = 'http://192.168.107.248:18089/api/v1' +lsky_pro_url = 'https://image.lqsjy.cn/api/v1' upload_endpoint = '/upload' token = '1|QJP2YEr9GIN52VBgmm5hCqV5DwBSvJLUKjnwcKB8' @@ -144,6 +145,55 @@ def upload_image_to_lsky_pro(image_path): pass return None + + +def process_image_file(markdown_file_path): + with open(markdown_file_path, 'r', encoding='utf-8') as file: + markdown_content = file.read() + + # 提取所有图片URL + image_urls = re.findall(r'!\[.*?\]\((.*?)\)', markdown_content) + url_mapping = {} # 存储原始URL到新URL的映射 + + for url in image_urls: + try: + if not url.startswith(('http://', 'https://')): + if 'image.lqsjy.cn' not in url: + + + + # 上传到图床 + new_url = upload_image_to_lsky_pro(url) + if new_url: + url_mapping[url] = new_url + print(f"处理成功: {url} -> {new_url}") + else: + print(f"上传失败: {url}") + + except Exception as e: + print(f"处理出错 {url}: {str(e)}") + continue + + # 更新Markdown内容 + if url_mapping: + new_content = markdown_content + for old_url, new_url in url_mapping.items(): + new_content = new_content.replace(old_url, new_url) + + # 构造新的文件名 + file_name, file_ext = os.path.splitext(markdown_file_path) + new_file_path = f"{file_name}_lsky{file_ext}" + + # 保存为新文件 + with open(new_file_path, 'w', encoding='utf-8') as file: + file.write(new_content) + + messagebox.showinfo("完成", f"成功处理 {len(url_mapping)} 张图片") + else: + messagebox.showwarning("警告", "没有图片被处理") + + + def upload_images(): markdown_file_path = input_file_entry.get() if not markdown_file_path or not os.path.isfile(markdown_file_path): @@ -262,6 +312,37 @@ def download_and_upload_images(): +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() + # 添加你的图片上传逻辑... + +def select_files(): + root = tk.Tk() + root.withdraw() # 隐藏主窗口 + + file_paths = filedialog.askopenfilenames( + title='选择Markdown文件', + filetypes=[('Markdown Files', '*.md')] + ) + + if file_paths: + upload_images_batch_files(file_paths) + else: + print("未选择文件") + + # 创建主窗口 root = tk.Tk() root.title("Markdown图片处理工具") @@ -280,9 +361,14 @@ download_button.grid(row=1, column=0, columnspan=3, pady=5) upload_button = tk.Button(root, text="上传图片", command=upload_images) upload_button.grid(row=2, column=0, columnspan=3, pady=5) +# 上传图片按钮_batch_files +# upload_button = tk.Button(root, text="上传图片_批量", command=upload_images_batch_files) +# upload_button.grid(row=3, column=0, columnspan=3, pady=5) +tk.Button(root, text="上传图片_批量", command=select_files).grid(row=3, column=1, padx=10, pady=10) + # 下载并上传图片按钮 download_upload_button = tk.Button(root, text="下载并上传图片", command=download_and_upload_images) -download_upload_button.grid(row=3, column=0, columnspan=3, pady=5) +download_upload_button.grid(row=4, column=0, columnspan=3, pady=5) # 运行主循环 root.mainloop() \ No newline at end of file