From f8ec86e235478f77535eb52f2a76ee72458ca7da Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sun, 22 Sep 2024 07:40:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E8=B4=A2=E8=81=94?= =?UTF-8?q?=E7=A4=BE=E8=AF=9D=E9=A2=98=20(#16840)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 财联社话题 * fix typo * fix typo --- lib/routes/cls/subject.ts | 153 +++++++++++++++++++++++ lib/routes/cls/templates/description.art | 21 ++++ 2 files changed, 174 insertions(+) create mode 100644 lib/routes/cls/subject.ts create mode 100644 lib/routes/cls/templates/description.art diff --git a/lib/routes/cls/subject.ts b/lib/routes/cls/subject.ts new file mode 100644 index 0000000000000..d91efef8d549e --- /dev/null +++ b/lib/routes/cls/subject.ts @@ -0,0 +1,153 @@ +import { Route } from '@/types'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; +import path from 'node:path'; + +import { rootUrl, getSearchParams } from './utils'; + +export const handler = async (ctx) => { + const { id = '1103' } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20; + + const currentUrl = new URL(`subject/${id}`, rootUrl).href; + const apiUrl = new URL(`api/subject/${id}/article`, rootUrl).href; + + const { data: response } = await got(apiUrl, { + searchParams: getSearchParams({ + Subject_Id: id, + }), + }); + + let items = response.data.slice(0, limit).map((item) => { + const title = item.article_title; + const description = art(path.join(__dirname, 'templates/description.art'), { + intro: item.article_brief, + }); + const guid = `cls-${item.article_id}`; + const image = item.article_img; + + return { + title, + description, + pubDate: parseDate(item.article_time, 'X'), + link: new URL(`detail/${item.article_id}`, rootUrl).href, + category: item.subjects.map((s) => s.subject_name), + author: item.article_author, + guid, + id: guid, + content: { + html: description, + text: item.article_brief, + }, + image, + banner: image, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + const data = JSON.parse($$('script#__NEXT_DATA__').text())?.props?.initialState?.detail?.articleDetail ?? undefined; + + if (!data) { + return item; + } + + const title = data.title; + const description = art(path.join(__dirname, 'templates/description.art'), { + images: data.images.map((i) => ({ + src: i, + alt: title, + })), + intro: data.brief, + description: data.content, + }); + const guid = `cls-${data.id}`; + const image = data.images?.[0] ?? undefined; + + item.title = title; + item.description = description; + item.pubDate = parseDate(data.ctime, 'X'); + item.category = [...new Set(data.subject?.flatMap((s) => [s.name, ...(s.subjectCategory?.flatMap((c) => [c.columnName || [], c.name || []]) ?? [])]) ?? [])].filter(Boolean); + item.author = data.author?.name ?? item.author; + item.guid = guid; + item.id = guid; + item.content = { + html: description, + text: data.content, + }; + item.image = image; + item.banner = image; + item.enclosure_url = data.audioUrl; + item.enclosure_type = item.enclosure_url ? `audio/${item.enclosure_url.split(/\./).pop()}` : undefined; + item.enclosure_title = title; + + return item; + }) + ) + ); + + const { data: currentResponse } = await got(currentUrl); + + const $ = load(currentResponse); + + const data = JSON.parse($('script#__NEXT_DATA__').text())?.props?.initialProps?.pageProps?.subjectDetail ?? undefined; + + const author = '财联社'; + const image = data?.img ?? undefined; + + return { + title: `${author} - ${data?.name ?? $('title').text()}`, + description: data?.description ?? undefined, + link: currentUrl, + item: items, + allowEmpty: true, + image, + author, + }; +}; + +export const route: Route = { + path: '/subject/:id?', + name: '话题', + url: 'www.cls.cn', + maintainers: ['nczitzk'], + handler, + example: '/cls/subject/1103', + parameters: { category: '分类,默认为 1103,即A股盘面直播,可在对应话题页 URL 中找到' }, + description: `:::tip + 若订阅 [有声早报](https://www.cls.cn/subject/1151),网址为 \`https://www.cls.cn/subject/1151\`。截取 \`https://www.cls.cn/subject/\` 到末尾的部分 \`1151\` 作为参数填入,此时路由为 [\`/cls/subject/1151\`](https://rsshub.app/cls/subject/1151)。 + ::: + `, + categories: ['finance'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.cls.cn/subject/:id'], + target: (params) => { + const id = params.id; + + return `/subject${id ? `/${id}` : ''}`; + }, + }, + ], +}; diff --git a/lib/routes/cls/templates/description.art b/lib/routes/cls/templates/description.art new file mode 100644 index 0000000000000..249654e7e618a --- /dev/null +++ b/lib/routes/cls/templates/description.art @@ -0,0 +1,21 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} + +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file