From 9fd5c42951983918e488ff364d7e07e1bd64fe5d Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Sun, 9 Jun 2024 02:50:52 +0800 Subject: [PATCH] feat(route/my-formosa): Create route (#15838) * feat(route/my-formosa): Create route * . * Update index.ts * Update index.ts * Update index.ts * . * . * . * Update lib/routes/my-formosa/index.ts --------- --- lib/routes/my-formosa/index.ts | 78 ++++++++++++++++++++++++++++++ lib/routes/my-formosa/namespace.ts | 6 +++ 2 files changed, 84 insertions(+) create mode 100644 lib/routes/my-formosa/index.ts create mode 100644 lib/routes/my-formosa/namespace.ts diff --git a/lib/routes/my-formosa/index.ts b/lib/routes/my-formosa/index.ts new file mode 100644 index 000000000..231f414fb --- /dev/null +++ b/lib/routes/my-formosa/index.ts @@ -0,0 +1,78 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; +import timezone from '@/utils/timezone'; + +export const route: Route = { + path: '/', + categories: ['new-media'], + example: '/my-formosa', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['my-formosa.com/'], + }, + ], + name: '首页', + maintainers: ['dzx-dzx'], + handler, + url: 'my-formosa.com', +}; + +function fetch(url) { + return ofetch(url, { responseType: 'arrayBuffer' }).then((raw) => { + const decoder = new TextDecoder('big5'); + return decoder.decode(raw); + }); +} + +async function handler() { + const rootUrl = 'http://www.my-formosa.com/'; + + const res = await fetch(rootUrl); + + const $ = load(res); + + const items = await Promise.all( + $('#featured-news h3 a') + .toArray() + .map((item) => { + item = $(item); + + const title = item.text(); + const link = new URL(item.attr('href'), rootUrl).href; + + return cache.tryGet(link, async () => { + const res = await fetch(link); + const $ = load(res); + + const isTV = /^\/TV/.test(new URL(link).pathname); + + return { + title, + link, + author: $('.page-header~#featured-news h4').text(), + category: $("meta[name='keywords']").attr('content').split(',').filter(Boolean), + pubDate: timezone(parseDate((isTV ? $('.icon-calendar')[0].next.data : $('.date').text()).trim()), +8), + description: (isTV ? $('.post-item').html() : $('.body').html()).replaceAll(/\/News.*?\.jpg/g, (match) => `http://my-formosa.com${match}`), + }; + }); + }) + ); + + return { + title: $('title').text(), + link: rootUrl, + item: items, + }; +} diff --git a/lib/routes/my-formosa/namespace.ts b/lib/routes/my-formosa/namespace.ts new file mode 100644 index 000000000..d1db40b2d --- /dev/null +++ b/lib/routes/my-formosa/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '美麗島電子報', + url: 'my-formosa.com', +};