tgo-article-extraction-workflow.md 3.91 KB

TGO 群文章预摘要手动批量工作流

2026-07-25 实测记录。用于用户一次性要求「提取文章 + 批量生成摘要」的场景。

三步调用

API 基地址:http://127.0.0.1:5001

1. 提取文章

curl -s -X POST http://127.0.0.1:5001/api/articles/extract \
  -H "Content-Type: application/json" \
  -d '{}'

返回示例:

{"ok": true, "data": {"new_articles": 0, "new_shares": 0, "total": 985}}

2. 获取未摘要文章

curl -s http://127.0.0.1:5001/api/articles/unsummarized

返回示例(节选):

{"ok": true, "data": [
  {"id": 463, "chatroom_id": "34416781008@chatroom", "title": "一夜之间,Claude Code 删掉了 80% 系统提示词", "url": "https://mp.weixin.qq.com/s?..."},
  ...
]}

3. 批量生成摘要

关键字段:POST /api/articles/summarize 必须同时传 id 和 url,否则返回 {"ok": false, "error": "需要 url"}。

单篇调用示例:

curl -s -X POST http://127.0.0.1:5001/api/articles/summarize \
  -H "Content-Type: application/json" \
  -d '{"id": 463, "url": "https://mp.weixin.qq.com/s?..."}'

Python 批量脚本($HOME/summarize_all.py):

import json
import sys
import subprocess
import time

articles = json.load(sys.stdin)['data']
for a in articles:
    payload = {"id": a["id"], "url": a["url"]}
    cmd = [
        "curl", "-s", "-X", "POST",
        "http://127.0.0.1:5001/api/articles/summarize",
        "-H", "Content-Type: application/json",
        "-d", json.dumps(payload)
    ]
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
    try:
        resp = json.loads(result.stdout)
        ok = resp.get('ok', False)
    except Exception:
        ok = False
        resp = {"error": "parse_failed", "raw": result.stdout}
    print(f"{'OK' if ok else 'FAIL'} {a['id']}: {resp}")
    time.sleep(0.5)

执行:

curl -s http://127.0.0.1:5001/api/articles/unsummarized | python3 $HOME/summarize_all.py

实测结果(2026-07-25)

  • 提取:0 新文章 / 0 新分享,累计 985 篇
  • 未摘要:20 篇
  • 只传 id 时:20 篇全部失败,错误 需要 url
  • 同时传 id + url 后:20 篇全部成功

响应类型与验证

异步提交(started)

新文章第一次提交时,返回:

{"ok": true, "data": {"status": "started", "task_id": "a9dc6cf8349d"}}

这表示 Hermes 子进程已启动,正在后台抓取+摘要。接口不会等待完成。

缓存命中(cached)

如果文章已经有缓存摘要(或本次提交前已被其他任务完成),接口会立即返回完整内容:

{"ok": true, "data": {"status": "cached", "content": "# 标题 摘要\n\n...", "summary": "...", "report_id": 423, "created_at": "2026-07-21 12:05:37"}}

注意字段名是 status 而不是 task_id 时已完成;report_id 表示已写入 daily_reports。

验证陷阱:无法从外部确认后台完成

  • 服务端没有暴露 /api/articles/task/<task_id> 状态查询接口(访问返回 404)
  • GET /api/articles/unsummarized 在提交后不会立即减少,它只反映数据库中尚未缓存摘要的文章,而异步任务完成后才写入
  • 因此提交大量 started 任务后,无法通过轮询 unsummarized 列表来确认进度

推荐做法:

  • 小批量手动任务:接受 started 响应,报告"已提交 X 篇,后台处理中"
  • 需要确认完成时:直接查询服务端日志、数据库 daily_reports 表,或让后端增加 job 状态接口
  • 不要对 unsummarized 列表做忙等待轮询

与 cron 预摘要的差异

  • cron 每轮最多提交 3 篇(Semaphore(3) 并发限制),剩余留给下一轮
  • 手动批量无此限制,可一次性提交全部,但建议每次请求间隔 0.5s 避免触发微信反爬
  • 异步任务在后台完成,接口返回成功仅代表已提交,不代表摘要已生成完毕