实战范例

新闻聚合器

用两次 batch 调用从多个新闻源聚合标题

两段式设计:先 distill 每个新闻源的首页发现文章 URL,再从这些 URL 提取(Extract)结构化标题记录。

流程

  1. Pass 1 —— 对每个新闻源首页跑 /batch/distill,带上 include: ["links"]
  2. 按模式过滤发现的链接(例如今天的日期、文章路径)
  3. Pass 2 —— 对过滤后的 URL 跑 /batch/extract,使用标题 schema
  4. 两段都完成时 Webhook 触发;写入你的存储

Schema

{
  "type": "object",
  "properties": {
    "title":       { "type": "string", "description": "The article headline" },
    "url":         { "type": "string", "description": "Canonical article URL" },
    "publishedAt": { "type": "string", "description": "ISO-8601 publish timestamp" },
    "summary":     { "type": "string", "description": "1-2 sentence summary" }
  },
  "required": ["title", "url"]
}

实现示意

import httpx

API = "https://openapi.thunderbit.com/openapi/v1"
H = {"Authorization": "Bearer YOUR_API_KEY"}

# Pass 1: distill homepages, collect article URLs
sources = ["https://news.example.com", "https://blog.example.org"]
home_job = httpx.post(f"{API}/batch/distill",
                      headers=H,
                      json={"urls": sources, "include": ["links"]}).json()
# (poll until COMPLETED — see Batch Job Lifecycle guide)

article_urls = []
for r in home_job["data"]["results"]:
    if r["status"] == "SUCCEEDED":
        article_urls += [u for u in r["links"] if "/article/" in u]

# Pass 2: extract structured headlines
extract_job = httpx.post(f"{API}/batch/extract",
                         headers=H,
                         json={"urls": article_urls, "schema": SCHEMA}).json()

小贴士

  • 给首页结果加缓存(5-10 分钟 TTL),避免在同一批标题上重复消耗额度
  • 如果新闻源会按 IP 本地化首页,设置 countryCode
  • 按规范化 URL 或内容哈希去重 —— 同一条标题经常出现在多个新闻源上

相关

这份 recipe 正在补充完整的两段式编排器示例,敬请期待。