#!/usr/bin/env python3 """ 调整选题优先级(数据库 + JSON 备份) """ import sys from pathlib import Path PROJECT_ROOT = Path(__file__).parent.parent sys.path.insert(0, str(PROJECT_ROOT)) try: from db_helper import get_topic_by_id, update_topic_status from app.database import SessionLocal from app.models import Topic as DBTopic HAVE_DB = True except ImportError: HAVE_DB = False print("Warning: db_helper not available, will only update JSON") DATA_DIR = PROJECT_ROOT / "automation" / "data" TOPICS_FILE = DATA_DIR / "sustainability_topics.json" def adjust_json_priority(adjustments): try: with open(TOPICS_FILE, 'r', encoding='utf-8') as f: topics = json.load(f) except: topics = [] updated = [] for t in topics: if t['id'] in adjustments: old = t.get('priority_score', 0) t['priority_score'] = adjustments[t['id']] updated.append(f"{t['id']}: {old} -> {adjustments[t['id']]}") with open(TOPICS_FILE, 'w', encoding='utf-8') as f: json.dump(topics, f, ensure_ascii=False, indent=2) return updated def adjust_db_priority(adjustments): if not HAVE_DB: return [] updated = [] db = SessionLocal() try: for tid, new_score in adjustments.items(): topic = db.query(DBTopic).filter(DBTopic.id == tid).first() if topic: topic.priority_score = new_score topic.updated_at = datetime.now() updated.append(f"{tid}: {topic.priority_score} -> {new_score}") db.commit() finally: db.close() return updated def main(): # 定义需要调整的优先级:ID -> 新分数 adjustments = { 'D01': 11, 'B05': 10 } print("调整优先级...") db_updated = adjust_db_priority(adjustments) if HAVE_DB else [] if db_updated: print("[DB] updated:", ', '.join(db_updated)) json_updated = adjust_json_priority(adjustments) print("[JSON] updated:", ', '.join(json_updated)) print("完成") if __name__ == "__main__": import json, datetime main()