From 0dcfda0c803f8848e71f67cdcc52f7a1dd8db7f9 Mon Sep 17 00:00:00 2001 From: Yuzhiran Dev Date: Thu, 21 May 2026 10:09:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=96=B0=E9=B2=9C=E5=BA=A6?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C:=20=E6=89=80=E6=9C=89=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=97=A5=E6=9C=9F/=E6=97=B6=E6=95=88?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. search_cache.json → 添加 _metadata.updated_at 时间戳 web_search.search_from_cache() 跳过超过36h的旧缓存 防止某查询失败时残留旧数据 2. metrics_feedback.json → collector 检查mtime,超过24h不采用 3. trends.json → 已有 date==TODAY 校验(load_trends) 4. collector_ai_advice → DB每日覆盖,时序安全 creator→topic状态位避免重复生成 optimizer→文章状态位避免重复审查 --- scripts/collector.py | 19 ++++++++++++------- scripts/opencode_search.py | 7 +++++-- scripts/web_search.py | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/scripts/collector.py b/scripts/collector.py index 2d41353..9c5e12e 100644 --- a/scripts/collector.py +++ b/scripts/collector.py @@ -398,13 +398,18 @@ class SustainabilityCollector: metrics_file = DATA_DIR / "metrics_feedback.json" if metrics_file.exists(): try: - feedback = json.loads(metrics_file.read_text(encoding='utf-8')) - top_domains = feedback.get("top_domains", []) - if top_domains: - lines = ["## 历史表现反馈(高互动领域优先", ""] - for d, s in top_domains[:3]: - lines.append(f"- {d}:平均分 {s}") - parts.append("\n".join(lines)) + mtime = datetime.datetime.fromtimestamp(metrics_file.stat().st_mtime) + age = (datetime.datetime.now() - mtime).total_seconds() + if age > 86400: # 超过24h的数据不采用 + logger.debug("metrics_feedback 过时(%.0fh),跳过", age / 3600) + else: + feedback = json.loads(metrics_file.read_text(encoding='utf-8')) + top_domains = feedback.get("top_domains", []) + if top_domains: + lines = ["## 历史表现反馈(高互动领域优先", ""] + for d, s in top_domains[:3]: + lines.append(f"- {d}:平均分 {s}") + parts.append("\n".join(lines)) except Exception: pass diff --git a/scripts/opencode_search.py b/scripts/opencode_search.py index fb6002d..95d2310 100644 --- a/scripts/opencode_search.py +++ b/scripts/opencode_search.py @@ -108,10 +108,13 @@ def refresh_cache(): "AI工具 人工智能 效率提升 2026", ] - cache = {} + cache = {"_metadata": {"updated_at": datetime.datetime.now().isoformat()}} if SEARCH_CACHE_FILE.exists(): try: - cache = json.loads(SEARCH_CACHE_FILE.read_text(encoding="utf-8")) + old = json.loads(SEARCH_CACHE_FILE.read_text(encoding="utf-8")) + for k, v in old.items(): + if not k.startswith("_"): + cache.setdefault(k, v) except Exception: pass diff --git a/scripts/web_search.py b/scripts/web_search.py index ccb442a..b173d36 100644 --- a/scripts/web_search.py +++ b/scripts/web_search.py @@ -7,7 +7,7 @@ 2. Bing Web Search API(设 BING_API_KEY) 3. Bing 网页抓取(服务器环境常反爬拦截) """ -import json, logging, os, re +import json, logging, os, re, datetime from pathlib import Path from typing import List, Dict, Optional from urllib.parse import quote_plus @@ -92,11 +92,21 @@ def search_scrape(query: str, max_results: int = 5) -> List[Dict]: def search_from_cache(query: str, max_results: int = 5) -> List[Dict]: - """从 opencode webfetch 预填充的缓存中读取""" + """从 opencode webfetch 预填充的缓存中读取(跳过超过36小时的缓存)""" if not SEARCH_CACHE_FILE.exists(): return [] try: cache = json.loads(SEARCH_CACHE_FILE.read_text(encoding="utf-8")) + meta = cache.get("_metadata", {}) + updated = meta.get("updated_at", "") + if updated: + try: + age = (datetime.datetime.now() - datetime.datetime.fromisoformat(updated)).total_seconds() + if age > 129600: # 36h + logger.warning("搜索缓存过时(%dh),跳过", int(age // 3600)) + return [] + except Exception: + pass results = cache.get(query, []) return results[:max_results] except Exception: