|
|
@@ -8,6 +8,7 @@ import sys
|
|
|
from datetime import datetime
|
|
|
from pathlib import Path
|
|
|
from typing import Optional
|
|
|
+from zoneinfo import ZoneInfo
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
from sqlalchemy import desc, or_
|
|
|
@@ -134,7 +135,12 @@ def _safe_truncate(s: object, max_len: int) -> str:
|
|
|
|
|
|
|
|
|
def _load_name_score_map(execution_id: int) -> dict:
|
|
|
- """读取 data/{execution_id} 下所有 JSON 的 name->score(同名取最高分)。"""
|
|
|
+ """读取 data/{execution_id} 下所有 JSON 的「名字->score」(同名取最高分)。
|
|
|
+
|
|
|
+ 兼容两类数据结构:
|
|
|
+ - `*_元素.json`:字段 `name` 表示名字
|
|
|
+ - `*_分类.json`:字段 `category` 表示名字
|
|
|
+ """
|
|
|
data_dir = Path(__file__).parent / "data" / str(execution_id)
|
|
|
if not data_dir.exists():
|
|
|
return {}
|
|
|
@@ -153,7 +159,10 @@ def _load_name_score_map(execution_id: int) -> dict:
|
|
|
for item in payload:
|
|
|
if not isinstance(item, dict):
|
|
|
continue
|
|
|
+ # 元素数据以 name 为主;分类数据以 category 为主。
|
|
|
name = item.get("name")
|
|
|
+ if not isinstance(name, str) or not name:
|
|
|
+ name = item.get("category")
|
|
|
score = item.get("score")
|
|
|
if isinstance(name, str) and isinstance(score, (int, float)):
|
|
|
prev = score_map.get(name)
|
|
|
@@ -259,7 +268,7 @@ def write_demand_items_to_mysql(execution_id: int, merge_level2: str) -> int:
|
|
|
log(f"[mysql] 需求 JSON 非数组,跳过写入:type={type(items)}")
|
|
|
return 0
|
|
|
|
|
|
- dt_value = datetime.now().strftime("%Y%m%d")
|
|
|
+ dt_value = datetime.now(ZoneInfo("Asia/Shanghai")).strftime("%Y%m%d")
|
|
|
score_map = _load_name_score_map(execution_id)
|
|
|
rows: list[dict] = []
|
|
|
for di in items:
|
|
|
@@ -269,10 +278,7 @@ def write_demand_items_to_mysql(execution_id: int, merge_level2: str) -> int:
|
|
|
name = _join_element_names_to_name(di.get("element_names"))
|
|
|
if not name:
|
|
|
continue
|
|
|
-
|
|
|
- score = di.get("score")
|
|
|
- if score is None or score == 0.0:
|
|
|
- score = _avg_score_for_joined_name(name, score_map)
|
|
|
+ score = _avg_score_for_joined_name(name, score_map)
|
|
|
reason = di.get("reason")
|
|
|
desc_value = di.get("desc")
|
|
|
suggestion = desc_value
|
|
|
@@ -428,5 +434,6 @@ async def main(
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- piaoquan_prepare('历史名人')
|
|
|
- # asyncio.run(main('小阳看天下', 'changwen'))
|
|
|
+ # asyncio.run(run_once(8, '贪污腐败'))
|
|
|
+ write_demand_items_to_mysql(execution_id=8, merge_level2='贪污腐败')
|
|
|
+
|