#!/usr/bin/env bun /** * Fetches top stories from Hacker News API and prints title + URL + score. * Usage: bun run scripts/hn_top.ts [limit] */ const limit = Number(Bun.argv[2] ?? 10); interface HNItem { id: number; title?: string; url?: string; score?: number; by?: string; type?: string; } const BASE = "https://hacker-news.firebaseio.com/v0"; async function fetchJSON(url: string): Promise { const res = await fetch(url); if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`); return res.json() as Promise; } const ids = await fetchJSON(`${BASE}/topstories.json`); const top = ids.slice(0, limit); const items = await Promise.all( top.map((id) => fetchJSON(`${BASE}/item/${id}.json`)), ); for (const item of items) { if (item.type !== "story" || !item.title) continue; const score = item.score ?? 0; const url = item.url ?? "(no url)"; console.log(`[${score}↑] ${item.title}`); console.log(` ${url}`); console.log(); }