import requests import json import sys # Halo API 配置 BASE_URL = "http://192.168.5.8:8090" # 读取Bearer Token(需要用户手动提供或从配置中读取) token = input("请输入 Halo Bearer Token: ").strip() # 读取 Markdown 文件 md_file = input("请输入 Markdown 文件路径: ").strip() # 读取文件内容 try: with open(md_file, 'r', encoding='utf-8') as f: content = f.read() except FileNotFoundError: print(f"文件不存在: {md_file}") sys.exit(1) # 从文件第一行提取标题 lines = content.split('\n') title = lines[0].lstrip('#').strip() if not title: title = input("无法从文件提取标题,请输入文章标题: ").strip() # 创建文章 headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } data = { "title": title, "content": content, "rawType": "markdown", "visible": "PUBLIC", "publish": True } response = requests.post(f"{BASE_URL}/apis/api.console.halo.run/v1alpha1/posts", headers=headers, json=data) if response.status_code == 200 or response.status_code == 201: result = response.json() print(f"文章创建成功!") print(f"标题: {result.get('spec', {}).get('title', 'N/A')}") print(f"ID: {result.get('metadata', {}).get('name', 'N/A')}") else: print(f"创建失败: {response.status_code}") print(response.text)