from fastapi import APIRouter, HTTPException, Query from pathlib import Path import os from datetime import datetime, date router = APIRouter(prefix="/api/articles", tags=["articles"]) PROJECT_ROOT = Path('/root/openclaw-workspace/projects/yu-zhi-ran') @router.get("/drafts") def list_drafts(publish_date: str = None): """列出指定日期的草稿文件(三平台)""" if not publish_date: publish_date = date.today().isoformat() base_dir = PROJECT_ROOT / "automation" / "data" / "releases" / publish_date if not base_dir.exists(): raise HTTPException(status_code=404, detail="No releases for this date") platforms = ["zhihu", "wechat", "xiaohongshu"] result = {} for p in platforms: path = base_dir / p if path.exists(): files = sorted([f.name for f in path.glob("*.html") if f.is_file()]) result[p] = files else: result[p] = [] return {"date": publish_date, "files": result} @router.get("/{topic_id}/preview") def preview_article(topic_id: str, platform: str = "zhihu", publish_date: str = None): """预览某选题的HTML内容""" if not publish_date: publish_date = date.today().isoformat() filename = f"{platform}_{topic_id}_{platform}.html" file_path = PROJECT_ROOT / "automation" / "data" / "releases" / publish_date / platform / filename # DEBUG print(f"[DEBUG] file_path={file_path}, exists={file_path.exists()}") if not file_path.exists(): raise HTTPException(status_code=404, detail=f"Article not found: {file_path}") content = file_path.read_text(encoding='utf-8') return {"topic_id": topic_id, "platform": platform, "html": content} @router.get("/optimization-report") def get_optimization_report(publish_date: str = None): """获取合规优化报告""" if not publish_date: publish_date = date.today().isoformat() report_path = PROJECT_ROOT / "automation" / "data" / "drafts" / publish_date / "optimization_report.json" if not report_path.exists(): raise HTTPException(status_code=404, detail="No optimization report for this date") report = report_path.read_text(encoding='utf-8') import json return json.loads(report)