[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));

输出成功

在这里插入图片描述
在这里插入图片描述

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值