#!/usr/bin/env python3 """列出知识库节点,可选指定父节点""" import json, sys, os import subprocess SPACE_ID = os.environ.get('FEISHU_SPACE_ID', '7664817589230570761') def get_token(): r = subprocess.run(['python3', os.path.join(os.path.dirname(__file__), 'init_token.py')], capture_output=True, text=True) td = json.loads(r.stdout) return td['token'] def list_nodes(parent_token=None): import requests token = get_token() h = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'} url = f'https://open.feishu.cn/open-apis/wiki/v2/spaces/{SPACE_ID}/nodes' params = {} if parent_token: params['parent_node_token'] = parent_token r = requests.get(url, params=params, headers=h) data = r.json() if data.get('code') != 0: raise Exception(f"API error: {data.get('msg')}") return data['data']['items'] if __name__ == '__main__': parent = sys.argv[1] if len(sys.argv) > 1 else None try: nodes = list_nodes(parent) print(json.dumps(nodes, indent=2, ensure_ascii=False)) except Exception as e: print(f'Error: {e}', file=sys.stderr) sys.exit(1)