From 3d34710b6eee0907f0f7658f4bfbd6f79e890669 Mon Sep 17 00:00:00 2001 From: quiniapiezoelectricity <73748843+quiniapiezoelectricity@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:42:25 +0100 Subject: [PATCH] feat(route): add icable route (#17105) * feat(route) add icable route * fix Disallow specified syntax * fix Move function definitions to the highest possible scope. * fix Disallow unnecessary escape characters * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --- lib/routes/i-cable/namespace.ts | 6 ++ lib/routes/i-cable/news.ts | 77 ++++++++++++++++++++ lib/routes/i-cable/templates/description.art | 8 ++ 3 files changed, 91 insertions(+) create mode 100644 lib/routes/i-cable/namespace.ts create mode 100644 lib/routes/i-cable/news.ts create mode 100644 lib/routes/i-cable/templates/description.art diff --git a/lib/routes/i-cable/namespace.ts b/lib/routes/i-cable/namespace.ts new file mode 100644 index 00000000000000..9a0945cbdde5cb --- /dev/null +++ b/lib/routes/i-cable/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '有線新聞', + url: 'i-cable.com', +}; diff --git a/lib/routes/i-cable/news.ts b/lib/routes/i-cable/news.ts new file mode 100644 index 00000000000000..d8819db08be361 --- /dev/null +++ b/lib/routes/i-cable/news.ts @@ -0,0 +1,77 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { getCurrentPath } from '@/utils/helpers'; +import path from 'node:path'; +import { art } from '@/utils/render'; +import { config } from '@/config'; +import InvalidParameterError from '@/errors/types/invalid-parameter'; + +const __dirname = getCurrentPath(import.meta.url); + +export const route: Route = { + path: '/news/:category?', + categories: ['traditional-media'], + example: '/i-cable/news', + parameters: { category: '分類,默認為新聞資訊' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.i-cable.com'], + target: '/news', + }, + { + source: ['www.i-cable.com/category/:category'], + target: '/news/:category', + }, + ], + name: '新聞', + maintainers: ['quiniapiezoelectricity'], + handler, + url: 'www.i-cable.com/', + description: ` +:::tip +分類只可用分類名稱,如:新聞資訊/港聞 +:::`, +}; + +async function handler(ctx) { + const category = ctx.req.param('category') ?? '新聞資訊'; + const limit = ctx.req.query('limit') ?? 20; + const root = 'https://www.i-cable.com/wp-json/wp/v2'; + + const response = await cache.tryGet(`${root}/categories?slug=${category}`, async () => await got(`${root}/categories?slug=${category}`), config.cache.routeExpire, false); + if (response.data.length < 1) { + throw new InvalidParameterError(`Invalid Category: ${category}`); + } + const metadata = response.data[0]; + + const list = await got(`${root}/posts?_embed=1&categories=${metadata.id}&per_page=${limit}`); + const items = list.data.map((item) => { + const description = art(path.join(__dirname, 'templates/description.art'), { + media: item._embedded['wp:featuredmedia'] ?? [], + content: item.content.rendered, + }); + return { + title: item.title.rendered, + link: item.link, + pubDate: item.date_gmt, + description, + category: item._embedded['wp:term'][0].map((term) => term.name) ?? [], + }; + }); + + return { + title: `有線新聞 - ${metadata.name}`, + description: metadata.description, + link: metadata.link, + item: items, + }; +} diff --git a/lib/routes/i-cable/templates/description.art b/lib/routes/i-cable/templates/description.art new file mode 100644 index 00000000000000..1748ab4221bd89 --- /dev/null +++ b/lib/routes/i-cable/templates/description.art @@ -0,0 +1,8 @@ +{{ if media.length > 0 }} + {{ each media }} +
+ {{ /each }} +{{ /if }} +{{ if content }} + {{@ content }} +{{ /if }} \ No newline at end of file