diff --git a/CHANGELOG.md b/CHANGELOG.md index 7da9e9b..912fc10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.12-it-tools-pro +### 💻 IT最新技术换权威源(IT之家) +- 「IT最新技术」数据源 Hacker News(社区内容、无简介)→ **IT之家**(权威中文 IT 资讯,60 条带中文简介) +- 中文源免翻译免代理,标题+简介原生中文;简介自动去「IT之家X月X日消息」套话再截 20 字 +- 翻译可靠性:超时/数量不符均拆半重试(深层 60s 短超时),英文简介残留降到 0 +- 验证:IT栏显示诺基亚魔改/华为/小米/索尼等权威 IT 新闻 + 中文简介,全站简介英文数 0 ✓ + + ### 📝 资讯条目加 20 字灰色简介 - 每条资讯标题下新增一行 20 字简介(灰色小字,标题深色对比) - 简介来源:RSS description / GitHub 仓库描述;去 HTML、LLM 翻译并压缩到 22 字、截断 20 字 diff --git a/docker/it-tools-relay/server.py b/docker/it-tools-relay/server.py index 255b236..b1ad26a 100644 --- a/docker/it-tools-relay/server.py +++ b/docker/it-tools-relay/server.py @@ -2531,7 +2531,7 @@ NEWS_FILE = os.environ.get("NEWS_FILE", "/app/data/news.json") _CST = _tz(_td(hours=8)) _BBC_RSS = "https://feeds.bbci.co.uk/news/world/rss.xml" _TC_AI_RSS = "https://techcrunch.com/category/artificial-intelligence/feed/" -_HN_RSS = "https://hnrss.org/frontpage" +_ITHOME_RSS = "https://www.ithome.com/rss/" # IT之家:权威中文 IT 资讯 _STOCK_URL = ("https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10&po=1&np=1" "&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&fields=f12,f14,f3") @@ -2638,7 +2638,7 @@ def _fetch_stock(): return out -def _llm_chat_nm(messages: list, model: str = "") -> str: +def _llm_chat_nm(messages: list, model: str = "", timeout: int = 120) -> str: """模块级 LLM 调用(新闻标题翻译用,NewAPI 本地)""" key = os.environ.get("NEWAPI_KEY", "") upstream = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions") @@ -2647,7 +2647,7 @@ def _llm_chat_nm(messages: list, model: str = "") -> str: raise RuntimeError("AI 服务未配置") body = json.dumps({"model": model, "messages": messages, "temperature": 0.3, "max_tokens": 4096}).encode() req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key}) - resp = request.urlopen(req, timeout=180) + resp = request.urlopen(req, timeout=timeout) data = json.loads(resp.read()) if "error" in data: raise RuntimeError(data["error"].get("message", "upstream error")) @@ -2659,8 +2659,8 @@ def _is_urlish(s: str) -> bool: return s.startswith("http") or "article url" in s -def _translate_chunk(chunk: list, condense: bool = False) -> list: - """翻译一批(≤18 条);失败/数量不符返回原文""" +def _translate_chunk(chunk: list, condense: bool = False, depth: int = 0) -> list: + """翻译一批(≤18 条);数量不符时拆半重试,超时/异常返回原文""" if not chunk: return chunk try: @@ -2672,7 +2672,7 @@ def _translate_chunk(chunk: list, condense: bool = False) -> list: "输出同样长度的 JSON 字符串数组,顺序完全一致,只输出 JSON(不要代码块标记、不要解释)。" "已是中文的条目原样保留。") prompt = instr + "\n" + json.dumps(list(chunk), ensure_ascii=False) - out = _llm_chat_nm([{"role": "user", "content": prompt}]) + out = _llm_chat_nm([{"role": "user", "content": prompt}], timeout=(60 if depth else 120)) s = out.strip() if s.startswith("```"): s = re.sub(r"^```[a-zA-Z]*\s*", "", s) @@ -2680,10 +2680,14 @@ def _translate_chunk(chunk: list, condense: bool = False) -> list: arr = json.loads(s) if isinstance(arr, list) and len(arr) == len(chunk): return [str(x) for x in arr] - print(f"[news] translate mismatch got={len(arr) if isinstance(arr, list) else 'non-list'} want={len(chunk)}", flush=True) - return chunk + raise RuntimeError(f"count {len(arr) if isinstance(arr, list) else 'non-list'} != {len(chunk)}") except Exception as e: # noqa: BLE001 - print(f"[news] translate chunk failed: {e}", flush=True) + # 失败(数量不符/超时):拆半重试(更小更快更可靠),最多 2 层 + if len(chunk) > 1 and depth < 2: + mid = len(chunk) // 2 + return (_translate_chunk(chunk[:mid], condense, depth + 1) + + _translate_chunk(chunk[mid:], condense, depth + 1)) + print(f"[news] translate give up ({len(chunk)}): {e}", flush=True) return chunk @@ -2721,37 +2725,40 @@ def _fetch_news(): _col("ai", "AI\u8d44\u8baf", "\U0001f916", lambda: _parse_rss(_http_get(OPENER_PROXY, _TC_AI_RSS))) _col("it", "IT\u6700\u65b0\u6280\u672f", "\U0001f4bb", - lambda: _parse_rss(_http_get(OPENER_PROXY, _HN_RSS))) + lambda: _parse_rss(_http_get(OPENER_DIRECT, _ITHOME_RSS))) _col("stock", "\u5f53\u5929\u6700\u70ed\u80a1\u7968", "\U0001f4c8", _fetch_stock) - # 翻译成中文:world/ai/it 标题;github/world/ai 简介(HN 无简介、股票本就中文) - en_keys = {"world", "ai", "it"} - desc_keys = {"github", "world", "ai"} + # 翻译:world/ai 英文标题与简介、github 英文简介(it=IT之家中文、stock 中文均免翻) + en_title_keys = {"world", "ai"} + en_desc_keys = {"github", "world", "ai"} title_texts, title_refs = [], [] desc_texts, desc_refs = [], [] for ci, c in enumerate(cols): for ii, it in enumerate(c["items"]): - if c["key"] in en_keys: + if c["key"] in en_title_keys: title_texts.append(it["title"]) title_refs.append((ci, ii)) d = it.get("desc") or "" - if d and not _is_urlish(d) and c["key"] in desc_keys: - desc_texts.append(d) + if _is_urlish(d): + cols[ci]["items"][ii]["desc"] = "" # 丢弃 URL 类 + elif d and c["key"] in en_desc_keys: + desc_texts.append(d) # 英文简介:翻译压缩 desc_refs.append((ci, ii)) - elif d and (c["key"] not in desc_keys or _is_urlish(d)): - cols[ci]["items"][ii]["desc"] = "" # 丢弃无用/URL 类简介 + # it/stock 中文简介:保留,稍后去套话+截断 if title_texts: for (ci, ii), t in zip(title_refs, _translate_chunked(title_texts, condense=False)): cols[ci]["items"][ii]["title"] = t if desc_texts: for (ci, ii), t in zip(desc_refs, _translate_chunked(desc_texts, condense=True)): cols[ci]["items"][ii]["desc"] = t - # 简介统一截断到 20 字 + # 简介:去来源+日期套话(如"IT之家10月13日消息,")+ 统一截断 20 字 for c in cols: for it in c["items"]: d = it.get("desc") or "" + d = re.sub(r"^(?:IT之家|快科技|cnBeta)[^,,]{0,25}消息[,,、\s]*", "", d).strip() if len(d) > 20: - it["desc"] = d[:20] + "…" + d = d[:20] + "…" + it["desc"] = d return cols