Files
RSSHub/lib/routes/pts/index.ts
T
DIYgod cf4e772de8 refactor: replace art template with jsx (#20777)
* refactor: replace art template with jsx

* fix: tsx files

* fix: renderToString import

* fix: hono html raw import

* feat: remove art
2025-12-29 12:27:32 +08:00

80 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { load } from 'cheerio';
import type { Route } from '@/types';
import cache from '@/utils/cache';
import { getSubPath } from '@/utils/common-utils';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { renderDescription } from './templates/description';
export const route: Route = {
path: '*',
name: 'Unknown',
maintainers: [],
handler,
};
async function handler(ctx) {
const rootUrl = 'https://news.pts.org.tw';
const currentUrl = `${rootUrl}${getSubPath(ctx) === '/' ? '/dailynews' : getSubPath(ctx)}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response.data);
let items = $('h1 a,h2 a')
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 30)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
item.author = content('.reporter-container a')
.toArray()
.map((e) => ({ name: content(e).text() }));
item.pubDate = timezone(parseDate(content('.article-time .mr-2 time').text()), +8);
item.updated = timezone(parseDate(content('.article-time span:nth-child(2) time').text()), +8);
item.category = content('.tag-list')
.first()
.find('.blue-tag')
.toArray()
.map((t) => content(t).text())
.filter((t) => t !== '...');
item.description = renderDescription({
image: content('meta[property="og:image"]').attr('content'),
description: content('.post-article').html(),
});
return item;
})
)
);
return {
title: $('title')
.text()
.replace(/第\d+頁 | /, ''),
link: currentUrl,
item: items,
};
}