From b50d1d77253ae4069322e23741ecbea2dbd9c7d2 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 22 Jan 2024 04:52:00 +0000 Subject: [PATCH] fix: recover cntheory (#14300) * fix: recover cntheory * fix: replaceAll --- lib/v2/cntheory/maintainer.js | 3 + lib/v2/cntheory/paper.js | 87 +++++++++++++++++++++++ lib/v2/cntheory/radar.js | 13 ++++ lib/v2/cntheory/router.js | 3 + lib/v2/cntheory/templates/description.art | 2 + website/docs/routes/traditional-media.mdx | 8 +++ 6 files changed, 116 insertions(+) create mode 100644 lib/v2/cntheory/maintainer.js create mode 100644 lib/v2/cntheory/paper.js create mode 100644 lib/v2/cntheory/radar.js create mode 100644 lib/v2/cntheory/router.js create mode 100644 lib/v2/cntheory/templates/description.art diff --git a/lib/v2/cntheory/maintainer.js b/lib/v2/cntheory/maintainer.js new file mode 100644 index 000000000..f146840f7 --- /dev/null +++ b/lib/v2/cntheory/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/paper/:id?': ['nczitzk'], +}; diff --git a/lib/v2/cntheory/paper.js b/lib/v2/cntheory/paper.js new file mode 100644 index 000000000..c25edb81f --- /dev/null +++ b/lib/v2/cntheory/paper.js @@ -0,0 +1,87 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const rootUrl = 'https://paper.cntheory.com'; + + let response = await got({ + method: 'get', + url: rootUrl, + }); + + response = await got({ + method: 'get', + url: `${rootUrl}/${response.data.match(/URL=(.*)"/)[1]}`, + }); + + const $ = cheerio.load(response.data); + + const matches = response.data.match(/images\/(\d{4}-\d{2}\/\d{2})\/\w+\/\w+_brief/); + const link = `${rootUrl}/html/${matches[1]}`; + + let items = []; + + await Promise.all( + $('#pageLink') + .toArray() + .filter((p) => (id ? $(p).text().split(':').pop() === id : true)) + .map((p) => `${link}/${$(p).attr('href').replace(/\.\//, '')}`) + .map(async (p) => { + const pageResponse = await got({ + method: 'get', + url: p, + }); + + const page = cheerio.load(pageResponse.data); + + items.push( + ...page('table') + .last() + .find('a') + .toArray() + .map((a) => `${link}/${$(a).attr('href')}`) + ); + }) + ); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item, async () => { + const detailResponse = await got({ + method: 'get', + url: item, + }); + + const content = cheerio.load(detailResponse.data); + + return { + link: item, + title: content('h1').text(), + pubDate: parseDate(matches[1], 'YYYY-MM/DD'), + enclosure_url: `${rootUrl}${ + content('.ban_t a') + .first() + .attr('href') + .match(/(\/images.*)/)[1] + }`, + description: art(path.join(__dirname, 'templates/description.art'), { + resource: content('#reslist').html().replaceAll('display:none;', ''), + description: content('founder-content').html(), + }), + }; + }) + ) + ); + + ctx.state.data = { + title: `学习时报${id ? ` - ${id}` : ''}`, + link: rootUrl, + item: items, + allowEmpty: true, + }; +}; diff --git a/lib/v2/cntheory/radar.js b/lib/v2/cntheory/radar.js new file mode 100644 index 000000000..88efdb14c --- /dev/null +++ b/lib/v2/cntheory/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'cntheory.com': { + _name: '理论网', + paper: [ + { + title: '学习时报', + docs: 'https://docs.rsshub.app/routes/traditional-media#li-lun-wang-xue-xi-shi-bao', + source: ['/'], + target: (params, url) => `/cntheory/paper/${new URL(url).toString().match(/-(\w+)\.htm/)[1]}`, + }, + ], + }, +}; diff --git a/lib/v2/cntheory/router.js b/lib/v2/cntheory/router.js new file mode 100644 index 000000000..896721b6d --- /dev/null +++ b/lib/v2/cntheory/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/paper/:id?', require('./paper')); +}; diff --git a/lib/v2/cntheory/templates/description.art b/lib/v2/cntheory/templates/description.art new file mode 100644 index 000000000..9294c48ed --- /dev/null +++ b/lib/v2/cntheory/templates/description.art @@ -0,0 +1,2 @@ +{{@ resource }} +{{@ description }} diff --git a/website/docs/routes/traditional-media.mdx b/website/docs/routes/traditional-media.mdx index abba7c13c..33e96144e 100644 --- a/website/docs/routes/traditional-media.mdx +++ b/website/docs/routes/traditional-media.mdx @@ -1489,6 +1489,14 @@ This route adds the missing photo and Link element. (Offical RSS doesn't have Li | hakka | political | medical | local | international | +## 理论网 {#li-lun-wang} + +### 学习时报 {#li-lun-wang-xue-xi-shi-bao} + + + 如订阅 **第 A1 版:国内大局**,路由为 [`/cntheory/paper/国内大局`](https://rsshub.app/cntheory/paper/国内大局)。 + + ## 联合早报 {#lian-he-zao-bao} :::warning