feat: 飞书知识库读写工具:增删查改节点、写入页面、创建早报、记录踩坑,支持 Markdown 表格原生渲染。

This commit is contained in:
NightStar
2026-07-30 15:50:17 +08:00
commit ceb3b6c9a8
13 changed files with 1029 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
#!/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)