From 1a3720ab4d079cdb6f422b09af8f15d4bab24631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A2=B3=E9=BB=91=E6=A9=99=E5=AD=90?= <96968141+HChenZi@users.noreply.github.com> Date: Fri, 19 Apr 2024 20:54:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20Add=20=E4=B8=8A=E6=B5=B7?= =?UTF-8?q?=E4=B8=9A=E4=BD=99=E6=97=A0=E7=BA=BF=E7=94=B5=E5=8D=8F=E4=BC=9A?= =?UTF-8?q?=20(#15290)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): Add 上海业余无线电协会 * fix(route): fix type error --- lib/routes/sara/index.ts | 66 ++++++++++++++++++++++++++++++++++++ lib/routes/sara/namespace.ts | 6 ++++ 2 files changed, 72 insertions(+) create mode 100644 lib/routes/sara/index.ts create mode 100644 lib/routes/sara/namespace.ts diff --git a/lib/routes/sara/index.ts b/lib/routes/sara/index.ts new file mode 100644 index 00000000000000..1b85c6371580ea --- /dev/null +++ b/lib/routes/sara/index.ts @@ -0,0 +1,66 @@ +import { Route, DataItem } from '@/types'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import { ofetch } from 'ofetch'; + +const typeMap = { + dynamic: '协会动态', + announcement: '通知公告', + industry: '行业动态', +}; + +export const route: Route = { + path: '/:type', + categories: ['government'], + example: '/sara/announcement', + parameters: { type: 'dynamic | announcement | industry' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + description: `| 协会动态 | 通知公告 |行业动态 | + | -------- | ------------ | -------- | + | dynamic | announcement | industry |`, + + name: '新闻资讯', + maintainers: ['HChenZi'], + handler: async (ctx) => { + const baseUrl = 'http://www.sara.org.cn'; + const type = ctx.req.param('type'); + + const url = `${baseUrl}/news/${type}.htm`; + const response = await ofetch(url); + const $ = load(response); + const list = $('.newsItem_total > dd') + .toArray() + .map((item) => { + const a = $(item).find('a').first(); + return { + link: `${baseUrl}${a.attr('href')}`, + title: a.attr('title'), + }; + }); + const items = (await Promise.all(list.map(getFeedItem))) as DataItem[]; + return { + title: typeMap[type], + link: url, + item: items, + }; + }, +}; + +async function getFeedItem(item) { + return await cache.tryGet(item.link, async () => { + const response = await ofetch(item.link); + const $ = load(response); + return { + description: $('.text').html(), + language: 'zh-cn', + ...item, + }; + }); +} diff --git a/lib/routes/sara/namespace.ts b/lib/routes/sara/namespace.ts new file mode 100644 index 00000000000000..71ef8ba5f0bb25 --- /dev/null +++ b/lib/routes/sara/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '上海业余无线电协会', + url: 'www.sara.org.cn', +};