#!/usr/bin/env python3 """ 合规审查与优化任务 每天 05:45 运行,处理当天所有 draft 文章: 1. 执行合规检查(compliance_checker) 2. 自动修复已知问题(标题、标签) 3. 重写合规版本 4. 更新选题状态为「审查通过待发布」 5. 生成优化报告通知 """ import json, datetime, logging, sys, re from pathlib import Path from typing import Dict, List from dataclasses import dataclass, asdict PROJECT_ROOT = Path('/root/openclaw-workspace/projects/yu-zhi-ran') sys.path.insert(0, str(PROJECT_ROOT)) from scripts.compliance_checker import check_article # 导入 LLM 客户端(合规优化使用 NVIDIA) sys.path.insert(0, str(PROJECT_ROOT / "platform" / "backend")) try: from app.core.modelscope_client import call_llm HAVE_LLM = True except ImportError: HAVE_LLM = False DATA_DIR = PROJECT_ROOT / "automation" / "data" RELEASES_DIR = DATA_DIR / "releases" DRAFTS_DIR = DATA_DIR / "drafts" TOPICS_FILE = DATA_DIR / "sustainability_topics.json" LOGS_DIR = PROJECT_ROOT / "automation" / "logs" TODAY = datetime.datetime.now().strftime("%Y-%m-%d") logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[logging.FileHandler(LOGS_DIR / f"optimizer_{TODAY}.log"), logging.StreamHandler()]) logger = logging.getLogger(__name__) # 平台白名单标签 PLATFORM_TAGS = { "zhihu": ["科技", "职场"], "xiaohongshu": ["AI", "可持续", "生活方式"] } @dataclass class OptimizationResult: file: str platform: str topic_id: str title: str original_issues: int fixed_issues: int final_score: int status: str def load_topic_map(): with open(TOPICS_FILE, 'r', encoding='utf-8') as f: topics = json.load(f) return {t['id']: t for t in topics} def update_topic_status(topic_id: str, status: str): """更新选题状态(JSON + 数据库)""" # 更新 JSON with open(TOPICS_FILE, 'r', encoding='utf-8') as f: topics = json.load(f) updated = False for t in topics: if t.get('id') == topic_id: t['status'] = status updated = True break if updated: with open(TOPICS_FILE, 'w', encoding='utf-8') as f: json.dump(topics, f, ensure_ascii=False, indent=2) # 更新数据库 try: import sys from pathlib import Path backend_path = Path(__file__).resolve().parents[2] / 'platform' / 'backend' if str(backend_path) not in sys.path: sys.path.insert(0, str(backend_path)) from app.database import SessionLocal from app.models import Topic db = SessionLocal() topic_db = db.query(Topic).filter(Topic.id == topic_id).first() if topic_db: topic_db.status = status db.commit() db.close() except Exception as e: logger.error(f"更新数据库失败: {e}") def fix_wechat_title(html: str, title: str) -> str: """微信标题优化: