Glanceway Glanceway
Tüm kaynaklar

Hacker News

Haberler v1.0.0

Top stories from Hacker News

@codytseng #news #tech #programming

Yapılandırma

Ad Anahtar Tür Zorunlu Varsayılan Açıklama
Story Type STORY_TYPE select Hayır top Type of stories to display
topnewbestaskshowjob

Kaynak Kodu

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
import type { GlancewayAPI, SourceMethods } from "../../types";

export default (api: GlancewayAPI): SourceMethods => {
  return {
    async refresh() {
      const storyType = api.config.get("STORY_TYPE") || "top";
      const endpoint = `https://hacker-news.firebaseio.com/v0/${storyType}stories.json`;
      const idsResponse = await api.fetch<number[]>(endpoint);

      if (!idsResponse.ok || !idsResponse.json) {
        return;
      }

      const topIds = idsResponse.json.slice(0, 50);

      type Story = {
        id: number;
        title: string;
        url?: string;
        time: number;
        score: number;
        descendants?: number;
        by: string;
      };

      const items: Story[] = [];

      await Promise.all(
        topIds.map(async (id) => {
          const res = await api.fetch<Story>(
            `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: item.url ?? `https://news.ycombinator.com/item?id=${item.id}`,
                timestamp: item.time,
              })),
            );
          }
        }),
      );
    },
  };
};