Glanceway Glanceway
Todas las fuentes

金十数据

Finanzas v1.0.0

金十数据快讯

@codytseng #finance #news #chinese #market

Código fuente

version: 1.0.0
name: 金十数据
description: 金十数据快讯
author: codytseng
author_url: https://github.com/codytseng
category: Finance
tags:
  - finance
  - news
  - chinese
  - market
import type { GlancewayAPI, InfoItem, SourceMethods } from "../../types";

interface Jin10FlashItem {
  id: string;
  time: string;
  type: number;
  data: {
    content?: string;
    title?: string;
    link?: string;
  };
}

interface Jin10Response {
  data: Jin10FlashItem[];
}

function stripHtml(html: string): string {
  return html.replace(/<[^>]*>/g, "").trim();
}

export default (api: GlancewayAPI): SourceMethods => {
  return {
    async refresh() {
      const response = await api.fetch<Jin10Response>(
        "https://flash-api.jin10.com/get_flash_list?channel=-8200&vip=1",
        {
          headers: {
            "x-app-id": "bVBF4FyRTn5NJF5n",
            "x-version": "1.0.0",
          },
        },
      );

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

      const items: InfoItem[] = [];

      for (const item of response.json.data) {
        if (item.type === 1) {
          continue;
        }

        let title: string;
        let url: string | undefined;

        if (item.type === 2) {
          title = item.data.title ?? "";
          url = item.data.link;
        } else {
          const content = stripHtml(item.data.content ?? "");
          const match = content.match(/【(.+?)】/);
          title = match ? match[1] : content;
          url = "https://www.jin10.com/flash";
        }

        if (!title) {
          continue;
        }

        items.push({
          id: item.id,
          title,
          subtitle: stripHtml(item.data.content ?? ""),
          url,
          timestamp: item.time,
        });
      }

      api.emit(items);
    },
  };
};