Skip to content

Commit

Permalink
feat(route): add icable route (#17105)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
quiniapiezoelectricity authored Oct 17, 2024
1 parent f8d7e71 commit 3d34710
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/routes/i-cable/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '有線新聞',
url: 'i-cable.com',
};
77 changes: 77 additions & 0 deletions lib/routes/i-cable/news.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
8 changes: 8 additions & 0 deletions lib/routes/i-cable/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ if media.length > 0 }}
{{ each media }}
<figure><img src="{{ $value.source_url }}" /></figure>
{{ /each }}
{{ /if }}
{{ if content }}
{{@ content }}
{{ /if }}

0 comments on commit 3d34710

Please sign in to comment.