Glanceway Glanceway
所有資料來源

Wikipedia On This Day

其他 v1.0.0

Historical events that happened on today's date from Wikipedia

@codytseng #wikipedia #history #today

設定項目

名稱 類型 必填 預設值 描述
Language LANGUAGE select en Wikipedia language edition
enzhjadefres

原始碼

version: 1.0.0
name: Wikipedia On This Day
description: Historical events that happened on today's date from Wikipedia
author: codytseng
author_url: https://github.com/codytseng
category: Other
tags:
  - wikipedia
  - history
  - today

config:
  - key: LANGUAGE
    name: Language
    type: select
    required: false
    default: en
    description: Wikipedia language edition
    options:
      - en
      - zh
      - ja
      - de
      - fr
      - es
module.exports = async (api) => {
  const language = api.config.get("LANGUAGE") || "en";

  async function fetchData() {
    const now = new Date();
    const mm = String(now.getMonth() + 1).padStart(2, "0");
    const dd = String(now.getDate()).padStart(2, "0");

    const url = `https://api.wikimedia.org/feed/v1/wikipedia/${language}/onthisday/all/${mm}/${dd}`;

    const response = await api.fetch(url);

    if (!response.ok || !response.json) {
      throw new Error(
        `Failed to fetch Wikipedia On This Day (HTTP ${response.status})`,
      );
    }

    const events = [
      ...(response.json.selected || []),
      ...(response.json.events || []),
    ];

    const seen = new Set();
    const items = [];

    for (const item of events) {
      const page = item.pages?.[0];
      const id = `${item.year}-${page?.title || item.text.slice(0, 50)}`;

      if (seen.has(id)) continue;
      seen.add(id);

      items.push({
        id,
        title: `${item.year} \u2014 ${item.text}`,
        subtitle: page?.extract,
        url: page?.content_urls?.desktop?.page,
      });

      if (items.length >= 200) break;
    }

    api.emit(items);
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};