Glanceway Glanceway
Toutes les sources

V2EX

Développement v1.0.0

V2EX 热门话题

@codytseng #v2ex #chinese #tech

Configuration

Nom Clé Type Requis Défaut Description
节点名称 NODE_NAME string Non 按节点筛选,多个用逗号分隔(如 python,apple,create)。留空则显示热门话题。

Code source

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: string
    required: false
    description: 按节点筛选,多个用逗号分隔(如 python,apple,create)。留空则显示热门话题。
import type { GlancewayAPI, SourceMethods } from "../../types";

export default (api: GlancewayAPI): SourceMethods => {
  return {
    async refresh() {
      type Topic = {
        id: number;
        title: string;
        content: string;
        url: string;
        replies: number;
        node: { title: string };
        member: { username: string };
        created: number;
      };

      const nodeName = api.config.get("NODE_NAME");

      const toItems = (topics: Topic[]) =>
        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 (nodeName) {
        const nodes = nodeName.split(",").map((n) => n.trim()).filter(Boolean);
        await Promise.all(
          nodes.map(async (node) => {
            const res = await api.fetch<Topic[]>(
              `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<Topic[]>(
          "https://www.v2ex.com/api/topics/hot.json",
        );
        if (res.ok && res.json) {
          api.emit(toItems(res.json));
        }
      }
    },
  };
};