Glanceway Glanceway
所有資料來源

Hacker News

新聞 v1.0.0

Top stories from Hacker News

@codytseng #news #tech #programming

設定項目

名稱 類型 必填 預設值 描述
Story Type STORY_TYPE select top Type of stories to display
topnewbestaskshowjob

原始碼

version: 1.0.0
name: Hacker News
description: Top stories from Hacker News
author: codytseng
category: News
tags:
  - news
  - tech
  - programming

config:
  - key: STORY_TYPE
    name: Story Type
    type: select
    required: false
    default: top
    description: Type of stories to display
    options:
      - top
      - new
      - best
      - ask
      - show
      - job
module.exports = async (api) => {
  const storyType = api.config.get("STORY_TYPE") || "top";

  async function fetchData() {
    const endpoint = `https://hacker-news.firebaseio.com/v0/${storyType}stories.json`;
    const idsResponse = await api.fetch(endpoint);

    if (!idsResponse.ok || !idsResponse.json) {
      throw new Error(`Failed to fetch Hacker News story list (HTTP ${idsResponse.status})`);
    }

    const topIds = idsResponse.json.slice(0, 100);
    const items = [];

    await Promise.allSettled(
      topIds.map(async (id) => {
        const res = await api.fetch(
          `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
        );
        if (res.json) {
          items.push(res.json);
          api.emit(
            items.map((item) => ({
              id: item.id.toString(),
              title: item.title,
              subtitle: `${item.score} points · ${item.descendants ?? 0} comments · by ${item.by}`,
              url: `https://news.ycombinator.com/item?id=${item.id}`,
              timestamp: item.time,
            })),
          );
        }
      }),
    );
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};