#!/usr/bin/env python3 import json, datetime, logging, sys, re from pathlib import Path from typing import Dict, List, Optional, Tuple from dataclasses import dataclass, asdict PROJECT_ROOT = Path('/root/openclaw-workspace/projects/yu-zhi-ran') sys.path.insert(0, str(PROJECT_ROOT)) sys.path.insert(0, str(PROJECT_ROOT / "platform" / "backend")) from scripts.compliance_checker import check_article try: from app.core.nvidia_client import call_llm HAVE_LLM = True except ImportError: HAVE_LLM = False from db_helper import get_topic_by_id, update_topic_status, get_active_llm_config, get_articles_by_topic, save_article DATA_DIR = PROJECT_ROOT / "automation" / "data" DRAFTS_DIR = DATA_DIR / "drafts" 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", "可持续", "生活方式"] } _llm_config_cache = None def get_llm_config(): global _llm_config_cache if _llm_config_cache is None: _llm_config_cache = get_active_llm_config() return _llm_config_cache @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(): from db_helper import export_topics_to_json topics = export_topics_to_json() return {t['id']: t for t in topics} def get_articles_from_db(topic_ids: Optional[List[str]] = None) -> List[Tuple[str, str, str]]: """从 articles 表读取 HTML 内容 Returns: [(html_content, platform, topic_id), ...] """ from db_helper import get_articles_by_topic results = [] seen_topics = set() if topic_ids: for tid in topic_ids: articles = get_articles_by_topic(tid) for a in articles: if a.get("html_content"): results.append((a["html_content"], a["platform"], a["topic_id"])) seen_topics.add(a["topic_id"]) else: from app.database import SessionLocal from app.models import Article db = SessionLocal() try: all_articles = db.query(Article).filter(Article.html_content.isnot(None)).all() for a in all_articles: results.append((a.html_content, a.platform, a.topic_id)) finally: db.close() return results def fix_wechat_title(html: str, title: str) -> str: suffix = f" - {TODAY} - 微信公众号" max_base_len = 32 - len(suffix) title_tag = re.search(r'