動かざることバグの如し

近づきたいよ 君の理想に

HackerNews APIで遊ぶ

Hacker Newsは知るぞ知る投稿型ニュースサイト。

https://news.ycombinator.com/news.ycombinator.com

英語読めないマンなのでこのサイト自体は活用できてないのだが、サイトが公式でAPIを出しており、無料かつユーザー登録不要ということで、新しい言語やフレームワークの練習によく使われる(要出典

が、そもそもそのAPIの仕様を知らないと書けないのでまとめた

一覧取得

一発で投稿一覧取得はできない。 大まかな流れとしては、投稿ID一覧取得→IDを指定して各投稿の詳細を取得となる。

jsonだが返ってくるのは配列である

[15728194,15728191,15728174,(略),15728173,15722110,15722108]

投稿取得

ID.jsonで取得できる。

以下のようなJSONが返ってくる。

{
  "by": "dmmalam",
  "descendants": 164,
  "id": 15723926,
  "kids": [
    15725222,
    15725497,
    15724381,
    15725098,
    15725522,
    15724380,
    15727049,
    15725813,
    15725354,
    15725170,
    15727631,
    15727112,
    15724376,
    15724479,
    15724432,
    15724919,
    15724328,
    15724448,
    15724396,
    15725571,
    15724863,
    15727718
  ],
  "score": 551,
  "time": 1510939468,
  "title": "Microsoft and GitHub team up to take Git virtual file system to macOS, Linux",
  "type": "story",
  "url": "https://arstechnica.com/gadgets/2017/11/microsoft-and-github-team-up-to-take-git-virtual-file-system-to-macos-linux/"
}

各項目は以下。あとで翻訳する。

Field Description
id The item's unique id.
deleted true if the item is deleted.
type The type of item. One of "job", "story", "comment", "poll", or "pollopt".
by The username of the item's author.
time Creation date of the item, in Unix Time.
text The comment, story or poll text. HTML.
dead true if the item is dead.
parent The comment's parent: either another comment or the relevant story.
poll The pollopt's associated poll.
kids The ids of the item's comments, in ranked display order.
url The URL of the story.
score The story's score, or the votes for a pollopt.
title The title of the story, poll or job.
parts A list of related pollopts, in display order.
descendants In the case of stories or polls, the total comment count.