From 86e1fde5d79b8da9aa2a88863db59870871e5213 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Tue, 26 Aug 2025 02:46:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=85=A8=E7=90=83?= =?UTF-8?q?=E5=8C=96=E6=99=BA=E5=BA=93=20(#19903)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/ccg/index.ts | 181 +++++++++++++++++++++++ lib/routes/ccg/namespace.ts | 9 ++ lib/routes/ccg/templates/description.art | 21 +++ 3 files changed, 211 insertions(+) create mode 100644 lib/routes/ccg/index.ts create mode 100644 lib/routes/ccg/namespace.ts create mode 100644 lib/routes/ccg/templates/description.art diff --git a/lib/routes/ccg/index.ts b/lib/routes/ccg/index.ts new file mode 100644 index 000000000..06110cfdb --- /dev/null +++ b/lib/routes/ccg/index.ts @@ -0,0 +1,181 @@ +import { type Data, type DataItem, type Route, ViewType } from '@/types'; + +import { art } from '@/utils/render'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +import { type CheerioAPI, type Cheerio, load } from 'cheerio'; +import type { Element } from 'domhandler'; +import { type Context } from 'hono'; +import path from 'node:path'; + +export const handler = async (ctx: Context): Promise => { + const { category = 'news' } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '7', 10); + + const baseUrl: string = 'http://www.ccg.org.cn'; + const targetUrl: string = new URL(category, baseUrl).href; + + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'zh'; + + let items: DataItem[] = []; + + items = $('ul.huodong-list li') + .slice(0, limit) + .toArray() + .map((el): Element => { + const $el: Cheerio = $(el); + + const title: string = $el.find('h5').text(); + const image: string | undefined = $el.find('div.huodong-img img').attr('src'); + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + intro: $el.find('p').html(), + }); + const pubDateStr: string | undefined = $el.find('span').text(); + const linkUrl: string | undefined = $el.find('a').attr('href'); + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr, 'YYYY年M月D日') : undefined, + link: linkUrl, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr, 'YYYY年M月D日') : undefined, + language, + }; + + return processedItem; + }); + + items = await Promise.all( + items.map((item) => { + if (!item.link) { + return item; + } + + return cache.tryGet(item.link, async (): Promise => { + const detailResponse = await ofetch(item.link); + const $$: CheerioAPI = load(detailResponse); + + const title: string = $$('div.pinpai-page h3').text(); + const pubDateStr: string | undefined = $$('span.time').text(); + + $$('div.pinpai-page h3').remove(); + $$('div.pinpai-page span.time').remove(); + + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + description: $$('div.pinpai-page').html(), + }); + + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr, 'YYYY年M月D日') : item.pubDate, + content: { + html: description, + text: description, + }, + updated: upDatedStr ? parseDate(upDatedStr, 'YYYY年M月D日') : item.updated, + language, + }; + + return { + ...item, + ...processedItem, + }; + }); + }) + ); + + const author: string = $('h1.nav-logo').first().text(); + + return { + title: `${author} - ${$('title').text()}`, + link: targetUrl, + item: items, + allowEmpty: true, + image: new URL('wp-content/themes/ccg/imgs/nav-logo.png', baseUrl).href, + author, + language, + id: targetUrl, + }; +}; + +export const route: Route = { + path: '/:category?', + name: '动态', + url: 'www.ccg.org.cn', + maintainers: ['nczitzk'], + handler, + example: '/ccg/news', + parameters: { + category: { + description: '分类,默认为 `news`,即新闻动态,可在对应分类页 URL 中找到', + options: [ + { + label: '新闻动态', + value: 'news', + }, + { + label: '媒体报道', + value: 'mtbd', + }, + ], + }, + }, + description: `:::tip +订阅 [新闻动态](http://www.ccg.org.cn/news),其源网址为 \`http://www.ccg.org.cn/news\`,请参考该 URL 指定部分构成参数,此时路由为 [\`/ccg/news\`](https://rsshub.app/ccg/news)。 +::: + +| 分类 | ID | +| -------------------------------------- | ----------------------------------- | +| [新闻动态](http://www.ccg.org.cn/news) | [news](https://rsshub.app/ccg/news) | +| [媒体报道](http://www.ccg.org.cn/mtbd) | [mtbd](https://rsshub.app/ccg/mtbd) | +`, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.ccg.org.cn/category'], + target: '/:category', + }, + { + title: '新闻动态', + source: ['www.ccg.org.cn/news'], + target: '/news', + }, + { + title: '媒体报道', + source: ['www.ccg.org.cn/mtbd'], + target: '/mtbd', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/ccg/namespace.ts b/lib/routes/ccg/namespace.ts new file mode 100644 index 000000000..6b696721e --- /dev/null +++ b/lib/routes/ccg/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '全球化智库', + url: 'ccg.org.cn', + categories: ['new-media'], + description: '', + lang: 'zh-CN', +}; diff --git a/lib/routes/ccg/templates/description.art b/lib/routes/ccg/templates/description.art new file mode 100644 index 000000000..249654e7e --- /dev/null +++ b/lib/routes/ccg/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