feat: 资讯条目加20字灰色简介
- 每条标题下加20字灰色简介(RSS description/GitHub描述) - 去HTML+LLM翻译压缩22字+截断20字 - 翻译分块+4并发(title18/desc8), 总时长6.7min→2.5min - 丢URL类简介; HN/股票不显示简介 - 列表高度240→330px仍显5条(含简介)框内滚动 验证: 简介灰色rgba(0,0,0,0.45)/5条可见/框内滚动 ✓ CHANGELOG记录
This commit is contained in:
1 parent
4d7c398510
commit
3d45765a75
3 files changed
+100
-27
No files matched your search
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. See [standa
|
||||
|
||||
## Version 2026.10.12-it-tools-pro
|
||||
|
||||
### 📝 资讯条目加 20 字灰色简介
|
||||
- 每条资讯标题下新增一行 20 字简介(灰色小字,标题深色对比)
|
||||
- 简介来源:RSS description / GitHub 仓库描述;去 HTML、LLM 翻译并压缩到 22 字、截断 20 字
|
||||
- 翻译改分块 + 4 并发(title 18/批、desc 8/批),总时长从 6.7 分钟降到约 2.5 分钟,避免超时
|
||||
- 丢弃 URL 类/无用简介;HN(无简介)、股票(本就中文)不显示简介
|
||||
- 资讯列表高度 240→330px,仍固定显示 5 条(含简介),超出框内滚动
|
||||
|
||||
|
||||
### 📰 每日资讯改版:置顶 + 每列 10 条 + 框内滚动
|
||||
- 「📰 每日资讯」移到首页最顶部(在精选推荐之上)
|
||||
- 每列抓取数量 5 → 10 条(GitHub/国际新闻/AI资讯/IT技术/股票)
|
||||
|
||||
@@ -2564,8 +2564,14 @@ def _http_get(opener, url, timeout=20):
|
||||
return resp.read()
|
||||
|
||||
|
||||
def _strip_html(s, cap=100):
|
||||
s = re.sub(r"<[^>]+>", " ", s or "")
|
||||
s = re.sub(r"\s+", " ", s).strip()
|
||||
return s[:cap]
|
||||
|
||||
|
||||
def _parse_rss(data, limit=10):
|
||||
"""RSS 2.0 / Atom -> [{title,url,time}]"""
|
||||
"""RSS 2.0 / Atom -> [{title,url,time,desc}]"""
|
||||
items = []
|
||||
try:
|
||||
root = _ET.fromstring(data)
|
||||
@@ -2575,8 +2581,9 @@ def _parse_rss(data, limit=10):
|
||||
t = (it.findtext("title") or "").strip()
|
||||
link = (it.findtext("link") or "").strip()
|
||||
pub = (it.findtext("pubDate") or "").strip()
|
||||
desc = _strip_html(it.findtext("description") or "")
|
||||
if t:
|
||||
items.append({"title": t, "url": link, "time": _norm_time(pub)})
|
||||
items.append({"title": t, "url": link, "time": _norm_time(pub), "desc": desc})
|
||||
if len(items) >= limit:
|
||||
break
|
||||
if items:
|
||||
@@ -2589,8 +2596,9 @@ def _parse_rss(data, limit=10):
|
||||
if l is not None:
|
||||
link = l.get("href", "")
|
||||
pub = (it.findtext(f"{ns}published") or it.findtext(f"{ns}updated") or "").strip()
|
||||
desc = _strip_html(it.findtext(f"{ns}summary") or it.findtext(f"{ns}content") or "")
|
||||
if t:
|
||||
items.append({"title": t, "url": link, "time": _norm_time(pub)})
|
||||
items.append({"title": t, "url": link, "time": _norm_time(pub), "desc": desc})
|
||||
if len(items) >= limit:
|
||||
break
|
||||
return items
|
||||
@@ -2606,6 +2614,7 @@ def _fetch_github():
|
||||
"title": f"{r.get('full_name','')} \u2b50{r.get('stargazers_count',0)}",
|
||||
"url": r.get("html_url", ""),
|
||||
"time": _norm_time(r.get("created_at") or r.get("pushed_at")),
|
||||
"desc": _strip_html(r.get("description") or ""),
|
||||
})
|
||||
return out
|
||||
|
||||
@@ -2638,34 +2647,61 @@ 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=120)
|
||||
resp = request.urlopen(req, timeout=180)
|
||||
data = json.loads(resp.read())
|
||||
if "error" in data:
|
||||
raise RuntimeError(data["error"].get("message", "upstream error"))
|
||||
return data["choices"][0]["message"]["content"]
|
||||
|
||||
|
||||
def _translate_titles(titles: list) -> list:
|
||||
"""批量把英文新闻标题翻译成中文;失败或数量不符则保留英文原文"""
|
||||
if not titles:
|
||||
return titles
|
||||
def _is_urlish(s: str) -> bool:
|
||||
s = (s or "").strip().lower()
|
||||
return s.startswith("http") or "article url" in s
|
||||
|
||||
|
||||
def _translate_chunk(chunk: list, condense: bool = False) -> list:
|
||||
"""翻译一批(≤18 条);失败/数量不符返回原文"""
|
||||
if not chunk:
|
||||
return chunk
|
||||
try:
|
||||
prompt = ("把下面 JSON 数组中的每条英文新闻标题翻译成简体中文,人名/机构/产品名保留英文(可跟中文意译)。"
|
||||
"输出同样长度的 JSON 字符串数组,顺序完全一致,只输出 JSON(不要代码块标记、不要解释)。"
|
||||
"已是中文的条目原样保留。\n" + json.dumps(list(titles), ensure_ascii=False))
|
||||
if condense:
|
||||
instr = ("把下面 JSON 数组里每条英文翻译成简体中文,并压缩成不超过 22 字的简介(保留关键信息)。"
|
||||
"输出同样长度的 JSON 字符串数组,顺序一致,只输出 JSON。已是中文的原样保留。")
|
||||
else:
|
||||
instr = ("把下面 JSON 数组里每条英文新闻标题翻译成简体中文,人名/机构/产品名保留英文(可跟中文意译)。"
|
||||
"输出同样长度的 JSON 字符串数组,顺序完全一致,只输出 JSON(不要代码块标记、不要解释)。"
|
||||
"已是中文的条目原样保留。")
|
||||
prompt = instr + "\n" + json.dumps(list(chunk), ensure_ascii=False)
|
||||
out = _llm_chat_nm([{"role": "user", "content": prompt}])
|
||||
s = out.strip()
|
||||
if s.startswith("```"):
|
||||
s = re.sub(r"^```[a-zA-Z]*\s*", "", s)
|
||||
s = re.sub(r"\s*```$", "", s).strip()
|
||||
arr = json.loads(s)
|
||||
if isinstance(arr, list) and len(arr) == len(titles):
|
||||
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(titles)}", flush=True)
|
||||
return titles
|
||||
print(f"[news] translate mismatch got={len(arr) if isinstance(arr, list) else 'non-list'} want={len(chunk)}", flush=True)
|
||||
return chunk
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"[news] translate failed: {e}", flush=True)
|
||||
return titles
|
||||
print(f"[news] translate chunk failed: {e}", flush=True)
|
||||
return chunk
|
||||
|
||||
|
||||
def _translate_chunked(texts: list, condense: bool = False) -> list:
|
||||
"""分批 + 并行翻译:分块避免单次过大,块间并行缩短总时长"""
|
||||
CH = 8 if condense else 18
|
||||
chunks = [texts[i:i + CH] for i in range(0, len(texts), CH)]
|
||||
if not chunks:
|
||||
return []
|
||||
if len(chunks) == 1:
|
||||
return _translate_chunk(chunks[0], condense)
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
with ThreadPoolExecutor(max_workers=min(4, len(chunks))) as ex:
|
||||
results = list(ex.map(lambda c: _translate_chunk(c, condense), chunks))
|
||||
out = []
|
||||
for r in results:
|
||||
out.extend(r)
|
||||
return out
|
||||
|
||||
|
||||
def _fetch_news():
|
||||
@@ -2688,18 +2724,34 @@ def _fetch_news():
|
||||
lambda: _parse_rss(_http_get(OPENER_PROXY, _HN_RSS)))
|
||||
_col("stock", "\u5f53\u5929\u6700\u70ed\u80a1\u7968", "\U0001f4c8", _fetch_stock)
|
||||
|
||||
# 英文栏目标题翻译成中文(GitHub 仓库名/股票中文名不翻,仅 world/ai/it)
|
||||
# 翻译成中文:world/ai/it 标题;github/world/ai 简介(HN 无简介、股票本就中文)
|
||||
en_keys = {"world", "ai", "it"}
|
||||
titles, idx = [], []
|
||||
desc_keys = {"github", "world", "ai"}
|
||||
title_texts, title_refs = [], []
|
||||
desc_texts, desc_refs = [], []
|
||||
for ci, c in enumerate(cols):
|
||||
if c["key"] in en_keys:
|
||||
for ii, it in enumerate(c["items"]):
|
||||
titles.append(it["title"])
|
||||
idx.append((ci, ii))
|
||||
if titles:
|
||||
zh = _translate_titles(titles)
|
||||
for (ci, ii), t in zip(idx, zh):
|
||||
for ii, it in enumerate(c["items"]):
|
||||
if c["key"] in en_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)
|
||||
desc_refs.append((ci, ii))
|
||||
elif d and (c["key"] not in desc_keys or _is_urlish(d)):
|
||||
cols[ci]["items"][ii]["desc"] = "" # 丢弃无用/URL 类简介
|
||||
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 字
|
||||
for c in cols:
|
||||
for it in c["items"]:
|
||||
d = it.get("desc") or ""
|
||||
if len(d) > 20:
|
||||
it["desc"] = d[:20] + "…"
|
||||
return cols
|
||||
|
||||
|
||||
|
||||
+15
-2
@@ -109,6 +109,7 @@ function onUpdateFavoriteTools() {
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<span class="news-title">{{ it.title }}</span>
|
||||
<span v-if="it.desc" class="news-desc">{{ it.desc }}</span>
|
||||
<span class="news-time">{{ it.time }}</span>
|
||||
</a>
|
||||
<div v-if="!col.items.length" class="news-empty">暂无内容</div>
|
||||
@@ -385,8 +386,8 @@ function onUpdateFavoriteTools() {
|
||||
.news-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
// 固定显示约 5 条的高度(每条约 48px),超出在框内滚动
|
||||
height: 240px;
|
||||
// 固定显示 5 条的高度(含简介约 66px/条),超出在框内滚动
|
||||
height: 330px;
|
||||
overflow-y: auto;
|
||||
overscroll-behavior: contain;
|
||||
scrollbar-width: thin;
|
||||
@@ -428,6 +429,18 @@ function onUpdateFavoriteTools() {
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.news-desc {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
font-size: 11px;
|
||||
line-height: 1.45;
|
||||
color: rgba(0, 0, 0, 0.45);
|
||||
margin-top: 2px;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.news-time {
|
||||
display: block;
|
||||
font-size: 10.5px;
|
||||
|
||||
Reference in new issue
Block a user