Hacker News 数据源
数据官方

Hacker News 数据源

Use when the user asks for Hacker News data — top stories, hot posts, Ask HN, Show HN, jobs (keywords:Hacker News、HN、热榜、热门、Tech news、最新文章、Ask HN、Show HN). Do NOT use for general news or non-HN sources.

hustcc
hustcc
浏览3.9万
使用8

Skill 文件

SKILL.md
name
hackernews-datasource
title
Hacker News 数据源
description
Use when the user asks for Hacker News data — top stories, hot posts, Ask HN, Show HN, jobs (keywords:Hacker News、HN、热榜、热门、Tech news、最新文章、Ask HN、Show HN). Do NOT use for general news or non-HN sources.
tools
curl

Hacker News 数据源

通过 curl tool 调用 Hacker News API 获取 HN 上的技术新闻、热门讨论、Ask/Show/Jobs 等内容。

数据特点说明

  • HN API 采用特殊的分层设计:先获取ID列表,再逐个获取详情(不像GraphQL可以一次性展开)
  • 列表接口(如 topstories)只返回item ID数组,需要逐个调 /item/{id} 获取完整内容
  • 这给API调用带来限制:获取10条热门文章需要11次curl调用
  • 数据有近实时更新,来自Firebase

鉴权

Hacker News API 完全免费且无需鉴权,直接发请求即可,不要传任何 token 或 API key。

通用约定

  • Base URLhttps://hacker-news.firebaseio.com/v0/(必须使用 HTTPS,HTTP 会被系统拒绝)
  • 响应格式:JSON
  • 时间格式:Unix Timestamp(整数秒)
  • 数据范围:约30个最新条目(高频更新)
  • 分页:不分页,固定数量返回
  • 速率限制:暂无明确限制,但请遵循文明开发原则

数据调用依赖(关键)

两步调用模式

  1. 第1步:调列表接口(如 /v0/topstories.json),返回ID数组如 [9129911, 9129199, 9127761, ...]
  2. 第2步:对每个ID调 /v0/item/{id}.json 获取详情

建议策略

  • 查询热门文章时,建议取 ID 列表的前 5-10 个,避免调用过多
  • 如需获取更多故事,提醒用户API特性导致响应较慢
  • 串行请求,避免同时发起多个curl请求

常用接口

1. 获取热门故事ID列表

获取当前最热门的故事ID列表(最多500个,返回ID数组)。

GET https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

返回示例

json
[9129911, 9129199, 9127761, 9128141, 9128264, ...]

关键返回字段

JSON Path类型说明
[N]integer第N个故事的唯一ID

2. 获取单项详情

获取任意 Item(故事、评论、job等)的完整详情。Item 类型包括:story(故事)、comment(评论)、job(招聘)、poll(投票)、pollopt(投票选项)。

GET https://hacker-news.firebaseio.com/v0/item/{item_id}.json?print=pretty

参数

参数名位置必填说明
item_idpathItem ID,如从 topstories 获取的ID
printquerypretty可美化输出,可选

关键返回字段

JSON Path类型说明
.idinteger唯一ID
.typestring类型:"story"/"comment"/"job"/"poll"/"pollopt"
.bystring作者用户名
.timeinteger创建时间(Unix戳)
.titlestring标题(story/job/poll)
.urlstring外链URL(story/job)
.textstring正文内容(HTML,comment/poll/ask)
.scoreinteger点赞数
.descendantsinteger评论总数
.kidsinteger[]子评论(按排序)的ID列表
.parentinteger父评论/父故事的ID
.deletedboolean是否已删除
.deadboolean是否被标记为dead

3. 获取最新故事ID列表

获取最新提交的故事ID列表(最多500个)。

GET https://hacker-news.firebaseio.com/v0/newstories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

返回示例

json
[34200422, 34200354, 34200345, ...]

4. 获取最佳故事(高讨论质量)

获取权重最高的故事ID列表(最多500个,基于讨论质量)。

GET https://hacker-news.firebaseio.com/v0/beststories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

5. 获取 Ask HN 故事

获取 Ask HN 分类的故事ID列表(最多200个)。

GET https://hacker-news.firebaseio.com/v0/askstories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

6. 获取 Show HN 故事

获取 Show HN 分类的故事ID列表(最多200个)。

GET https://hacker-news.firebaseio.com/v0/showstories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

7. 获取招聘信息

获取 Job 分类的职位信息ID列表(最多200个)。

GET https://hacker-news.firebaseio.com/v0/jobstories.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

8. 获取最新最大Item ID

获取当前最大的 item ID,用于发现最新内容或遍历。

GET https://hacker-news.firebaseio.com/v0/maxitem.json

参数:无

返回示例

json
34200891

9. 获取用户信息

获取指定用户的详细资料。

GET https://hacker-news.firebaseio.com/v0/user/{username}.json?print=pretty

参数

参数名位置必填说明
usernamepath用户名(大小写敏感)
printquerypretty可美化输出,可选

关键返回字段

JSON Path类型说明
.idstring用户名
.createdinteger注册时间(Unix戳)
.karmaintegerKarma积分
.aboutstring个人简介(HTML)
.submittedinteger[]用户发布的story/comment ID列表

10. 获取最近变更

获取最近发生变化的 item 和 user 列表。

GET https://hacker-news.firebaseio.com/v0/updates.json?print=pretty

参数

参数名位置必填说明
printquerypretty可美化输出,可选

关键返回字段

JSON Path类型说明
.itemsinteger[]发生变更的item ID数组
.profilesstring[]发生变更的用户名数组

调用示例

场景:"查看 Hacker News 当前热门新闻前5条"

  1. 获取热门故事ID列表:
curl(url = "https://hacker-news.firebaseio.com/v0/topstories.json")
  1. 从返回中提取前5个ID,假设为:[36155330, 36157787, 36156634, 36156984, 36157696]

  2. 逐个获取故事详情(串行调用):

curl(url = "https://hacker-news.firebaseio.com/v0/item/36155330.json")curl(url = "https://hacker-news.firebaseio.com/v0/item/36157787.json")curl(url = "https://hacker-news.firebaseio.com/v0/item/36156634.json")curl(url = "https://hacker-news.firebaseio.com/v0/item/36156984.json")curl(url = "https://hacker-news.firebaseio.com/v0/item/36157696.json")
  1. 从每个返回中提取 .title.url.score.by.time.descendants 整理为列表展示。

场景:"显示 Hacker News 上的最新 Ask HN 讨论"

  1. 获取 Ask HN 故事ID列表:
curl(url = "https://hacker-news.firebaseio.com/v0/askstories.json")
  1. 取前5个ID,逐个获取详情,从返回中提取 .title.text(问题正文)、.by.descendants(评论数)、.score

  2. 如果时间戳需要,将 Unix timestamp 转换为可读时间格式。


场景:"查找用户 'pg' 在 Hacker News 的 karma"

  1. 获取用户信息:
curl(url = "https://hacker-news.firebaseio.com/v0/user/pg.json")
  1. 从返回中提取 .karma 字段的值。

错误处理

HTTP 状态码含义应对方式
200成功正常处理
404资源不存在Item ID 不存在或已被删除;User 不存在或无公开活动
403拒绝访问检查 HTTP 协议,确保使用 HTTPS
5xx服务器错误重试1-2次,若持续失败告知用户服务端暂时不可用

数据缺失说明

  • deleted: true - Item 已被删除
  • dead: true - Item 被标记为dead(可能违反社区规则)
  • 某些字段可能为 null 或缺失(如 comment 的 .url 字段)- 使用前检查存在性
  • value 为 null 时 - 用户注册后未设置该字段(如 about 为空)

数据边界与限制

  • 地域限制:无,全球可访问
  • 时间滞后:近实时(Firebase 同步)
  • 用户数据限制:只有有公开活动(评论或提交)的用户才可通过 API 访问
  • 用户名字段:区分大小写
  • HTML内容texttitle 字段可能包含 HTML 标签,渲染时需做 XSS 防护
  • 列表长度:topstories/newstories/beststories 最多500条;ask/show/job stories 最多200条
Ln 1, Col 1MarkdownSpaces: 2
No errors