Glanceway Glanceway
所有信息源

V2EX

开发者 v1.0.0

V2EX 热门话题

@codytseng #v2ex #chinese #tech

配置项

名称 类型 必填 默认值 描述
节点名称 NODE_NAME list 按节点筛选(如 python, apple, create)。留空则显示热门话题。

源代码

version: 1.0.0
name: V2EX
description: V2EX 热门话题
author: codytseng
author_url: https://github.com/codytseng
category: Developer
tags:
  - v2ex
  - chinese
  - tech

config:
  - key: NODE_NAME
    name: 节点名称
    type: list
    required: false
    description: 按节点筛选(如 python, apple, create)。留空则显示热门话题。
module.exports = async (api) => {
  const nodes = api.config.get("NODE_NAME") ?? [];

  async function fetchData() {
    const toItems = (topics) =>
      topics.map((topic) => ({
        id: topic.id.toString(),
        title: topic.title,
        subtitle: `${topic.node.title} · ${topic.replies} 回复 · ${topic.member.username}`,
        url: topic.url,
        timestamp: topic.created,
      }));

    if (nodes.length > 0) {
      await Promise.allSettled(
        nodes.map(async (node) => {
          const res = await api.fetch(
            `https://www.v2ex.com/api/topics/show.json?node_name=${encodeURIComponent(node)}`,
          );
          if (res.ok && res.json) {
            api.emit(toItems(res.json));
          }
        }),
      );
    } else {
      const res = await api.fetch(
        "https://www.v2ex.com/api/topics/hot.json",
      );
      if (!res.ok || !res.json) {
        throw new Error(`Failed to fetch V2EX hot topics (HTTP ${res.status})`);
      }
      api.emit(toItems(res.json));
    }
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};