hackernews

hackernews

Hacker News API,用于获取故事和评论。当用户提到“Hacker News”、“HN”、“Y Combinator”或询问科技新闻时使用。

74Star
17Fork
更新于 2026/7/24
SKILL.md
readonly只读
name
hackernews
description

Hacker News API,用于获取故事和评论。当用户提到“Hacker News”、“HN”、“Y Combinator”或询问科技新闻时使用。

使用方法

1. 获取热门故事

获取当前排名前500的故事ID:

curl -s "https://hacker-news.firebaseio.com/v0/topstories.json" | jq '.[:10]'

2. 获取最佳故事

获取最佳故事(按时间累计最高投票):

curl -s "https://hacker-news.firebaseio.com/v0/beststories.json" | jq '.[:10]'

3. 获取最新故事

获取最新故事:

curl -s "https://hacker-news.firebaseio.com/v0/newstories.json" | jq '.[:10]'

4. 获取Ask HN故事

获取“Ask HN”帖子:

curl -s "https://hacker-news.firebaseio.com/v0/askstories.json" | jq '.[:10]'

5. 获取Show HN故事

获取“Show HN”帖子:

curl -s "https://hacker-news.firebaseio.com/v0/showstories.json" | jq '.[:10]'

6. 获取招聘故事

获取招聘帖子:

curl -s "https://hacker-news.firebaseio.com/v0/jobstories.json" | jq '.[:10]'

条目详情

7. 获取故事/评论/招聘详情

通过ID获取任何条目的完整详情。将<item-id>替换为实际条目ID:

curl -s "https://hacker-news.firebaseio.com/v0/item/<item-id>.json"

响应字段:

字段 描述
id 唯一条目ID
type storycommentjobpollpollopt
by 作者用户名
time Unix时间戳
title 故事标题(仅故事)
url 故事URL(如有外部链接)
text 内容文本(Ask HN、评论)
score 点赞数
descendants 总评论数
kids 子评论ID数组

8. 获取多个故事及其详情

获取前5个故事及其完整详情。将<item-id>替换为实际条目ID:

curl -s "https://hacker-news.firebaseio.com/v0/topstories.json" | jq '.[:5][]' | while read id; do
  curl -s "https://hacker-news.firebaseio.com/v0/item/${id}.json" | jq '{id, title, score, url, by}'
done

9. 获取故事及其评论

获取一个故事及其顶级评论。将<story-id>替换为实际故事ID:

curl -s "https://hacker-news.firebaseio.com/v0/item/<story-id>.json" | jq '{title, score, descendants, kids}'

然后对于kids数组中的每个评论ID,将<comment-id>替换为实际评论ID:

curl -s "https://hacker-news.firebaseio.com/v0/item/<comment-id>.json" | jq '{by, text, score}'

用户数据

10. 获取用户资料

获取用户详情。将<username>替换为实际用户名:

curl -s "https://hacker-news.firebaseio.com/v0/user/<username>.json"

响应字段:

字段 描述
id 用户名
created 账户创建时间戳
karma 用户Karma值
about 用户简介(HTML)
submitted 提交的条目ID数组

11. 获取用户最近提交

获取用户最近的提交。将<username>替换为实际用户名:

curl -s "https://hacker-news.firebaseio.com/v0/user/<username>.json" | jq '.submitted[:5]'

实时更新

12. 获取最大条目ID

获取当前最大条目ID(用于轮询新条目):

curl -s "https://hacker-news.firebaseio.com/v0/maxitem.json"

13. 获取变更的条目和资料

获取最近变更的条目和资料(用于实时更新):

curl -s "https://hacker-news.firebaseio.com/v0/updates.json"

实用示例

获取今日前10名及其分数

curl -s "https://hacker-news.firebaseio.com/v0/topstories.json" | jq '.[:10][]' | while read id; do
  curl -s "https://hacker-news.firebaseio.com/v0/item/${id}.json" | jq -r '"\(.score) points | \(.title) | \(.url // "Ask HN")"'
done

查找高分故事(100分以上)

curl -s "https://hacker-news.firebaseio.com/v0/topstories.json" | jq '.[:30][]' | while read id; do
  curl -s "https://hacker-news.firebaseio.com/v0/item/${id}.json" | jq -r 'select(.score >= 100) | "\(.score) | \(.title)"'
done

获取最新AI/ML相关故事

curl -s "https://hacker-news.firebaseio.com/v0/topstories.json" | jq '.[:50][]' | while read id; do
  curl -s "https://hacker-news.firebaseio.com/v0/item/${id}.json" | jq -r 'select(.title | test("AI|GPT|LLM|Machine Learning|Neural"; "i")) | "\(.score) | \(.title)"'
done

API端点汇总

端点 描述
/v0/topstories.json 前500个故事
/v0/beststories.json 最佳故事
/v0/newstories.json 最新500个故事
/v0/askstories.json Ask HN故事
/v0/showstories.json Show HN故事
/v0/jobstories.json 招聘帖子
/v0/item/{id}.json 条目详情
/v0/user/{id}.json 用户资料
/v0/maxitem.json 当前最大条目ID
/v0/updates.json 变更的条目/资料

使用指南

  1. 无速率限制文档:但请保持礼貌,批量获取时添加延迟
  2. 使用jq进行过滤:过滤JSON响应以提取所需数据
  3. 缓存结果:故事不会频繁变化,尽可能缓存
  4. 谨慎批量请求:每个条目需要单独的API调用
  5. 处理空值:某些字段可能为null或缺失(例如Ask HN的url
  6. Unix时间戳:所有时间均为Unix时间戳,按需转换