-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
f8d7e71
commit 3d34710
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |