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
This commit is contained in:
quiniapiezoelectricity
2024-10-17 23:42:25 +08:00
committed by GitHub
parent f8d7e71f2b
commit 3d34710b6e
3 changed files with 91 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '有線新聞',
url: 'i-cable.com',
};
+77
View File
@@ -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,
};
}
@@ -0,0 +1,8 @@
{{ if media.length > 0 }}
{{ each media }}
<figure><img src="{{ $value.source_url }}" /></figure>
{{ /each }}
{{ /if }}
{{ if content }}
{{@ content }}
{{ /if }}