#!/usr/bin/env python3 """创建踩坑记录页面,自动挂到踩坑记录合集下""" import json, sys, os, argparse, subprocess SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__)) SPACE_ID = os.environ.get('FEISHU_SPACE_ID', '7664817589230570761') def get_token(): r = subprocess.run(['python3', os.path.join(SCRIPTS_DIR, 'init_token.py')], capture_output=True, text=True) return json.loads(r.stdout)['token'] def find_or_create_parent(name): """Find or create a parent node by name under root""" import requests token = get_token() h = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'} ROOT = os.environ.get('FEISHU_ROOT_NODE', 'LY5wwNWpniOoFwkHHG4cmDvGnSh') r = requests.get( f'https://open.feishu.cn/open-apis/wiki/v2/spaces/{SPACE_ID}/nodes', params={'parent_node_token': ROOT}, headers=h ) items = r.json()['data']['items'] for n in items: if name in n['title']: return n['node_token'] r = requests.post( f'https://open.feishu.cn/open-apis/wiki/v2/spaces/{SPACE_ID}/nodes', headers=h, json={'obj_type': 'docx', 'node_type': 'origin', 'parent_node_token': ROOT, 'title': '📝 踩坑记录合集'} ) return r.json()['data']['node']['node_token'] def create_pitfall(title, content_file=None): import requests token = get_token() h = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'} parent_token = find_or_create_parent('踩坑记录合集') r = requests.post( f'https://open.feishu.cn/open-apis/wiki/v2/spaces/{SPACE_ID}/nodes', headers=h, json={'obj_type': 'docx', 'node_type': 'origin', 'parent_node_token': parent_token, 'title': title} ) data = r.json() if data.get('code') != 0: raise Exception(f"Create pitfall failed: {data.get('msg')}") node = data['data']['node'] print(json.dumps({'node_token': node['node_token'], 'obj_token': node['obj_token']})) if content_file: subprocess.run( ['python3', os.path.join(SCRIPTS_DIR, 'wiki_write.py'), '--node', node['node_token'], '--file', content_file], check=True ) return node if __name__ == '__main__': p = argparse.ArgumentParser() p.add_argument('--title', required=True) p.add_argument('--content', default=None) args = p.parse_args() try: create_pitfall(args.title, args.content) except Exception as e: print(f'Error: {e}', file=sys.stderr) sys.exit(1)