添加批量上传Markdown文件中图片的功能,并更新图片上传逻辑
This commit is contained in:
		@@ -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()
 | 
			
		||||
		Reference in New Issue
	
	Block a user