[Eagle API]使用python保存所有文件夹层级信息

api : https://www.yuque.com/augus-gsjgn/eagle-api/pq0y2y

var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

fetch("http://localhost:41595/api/folder/list?token=YOUR_API_TOKEN", requestOptions)
  .then(response => response.json())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

输出成功

在这里插入图片描述
[Eagle API]使用python保存所有文件夹层级信息_第1张图片

python代码

import requests
import json
import os

url = "http://localhost:41595/api/folder/list?token=YOUR_API_TOKEN"
output_folder = "Eagle_json"

def traverse_folder_hierarchy(folder, indent_level=0):
    indent = "    " * indent_level
    folder_info = indent + folder["name"] + "\n"
    
    if "children" in folder:
        for child in folder["children"]:
            folder_info += traverse_folder_hierarchy(child, indent_level + 1)
    
    return folder_info

response = requests.get(url)
data = response.json()

if response.status_code == 200 and data["status"] == "success":
    folders = data["data"]
    
    # 构建完整的 JSON
    output_json_full = json.dumps(data, indent=4)
    
    # 创建保存 JSON 的文件夹(如果不存在)
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 保存完整的 JSON 到文件中
    output_file_full = os.path.join(output_folder, "folder_hierarchy_full.json")
    with open(output_file_full, "w") as file:
        file.write(output_json_full)
    
    print("Folder hierarchy saved to", output_file_full)
    
    # 构建缩略信息的 JSON 和文本内容
    folder_hierarchy_data = {
        "folder_names": []
    }
    folder_hierarchy_text = ""
    
    for folder in folders:
        folder_hierarchy_data["folder_names"].append(folder["name"])
        folder_hierarchy_text += folder["name"] + "\n"
        
        if "children" in folder:
            for child in folder["children"]:
                folder_hierarchy_data["folder_names"].append("    " + child["name"])
                folder_hierarchy_text += "    " + child["name"] + "\n"
    
    # 保存缩略信息的 JSON 到文件中
    output_json_thumbnail = json.dumps(folder_hierarchy_data, indent=4)
    output_file_thumbnail = os.path.join(output_folder, "folder_hierarchy_thumbnail.json")
    with open(output_file_thumbnail, "w") as file:
        file.write(output_json_thumbnail)
    
    print("Folder hierarchy thumbnail saved to", output_file_thumbnail)
    
    # 保存缩略信息的文本文件
    output_file_text = os.path.join(output_folder, "folder_hierarchy_thumbnail.txt")
    with open(output_file_text, "w") as file:
        file.write(folder_hierarchy_text)
    
    print("Folder hierarchy thumbnail saved to", output_file_text)
else:
    print("Error occurred:", data)

你可能感兴趣的:(python,数学建模,开发语言)