From 04f7329b5d55f0c34e4e46fd483a624ec10aa803 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Wed, 25 May 2022 20:15:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E7=90=86=E8=AE=BA?= =?UTF-8?q?=E7=BD=91=E5=AD=A6=E4=B9=A0=E6=97=B6=E6=8A=A5=20(#9813)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/traditional-media.md | 20 +++++ lib/v2/cntheory/maintainer.js | 3 + lib/v2/cntheory/paper.js | 93 +++++++++++++++++++++++ lib/v2/cntheory/radar.js | 13 ++++ lib/v2/cntheory/router.js | 3 + lib/v2/cntheory/templates/description.art | 2 + 6 files changed, 134 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/docs/traditional-media.md b/docs/traditional-media.md index e1e4fce36..53e221634 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -1095,6 +1095,26 @@ category 对应的关键词有 +## 理论网 + +### 学习时报 + + + +| 版 | 编号 | +| ----------- | -- | +| 全部 | | +| 第 A1 版:国内大局 | A1 | +| 第 A2 版:市场经济 | A2 | +| 第 A3 版:民主法治 | A3 | +| 第 A4 版:读书治学 | A4 | +| 第 A5 版:特别策划 | A5 | +| 第 A6 版:科技前沿 | A6 | +| 第 A7 版:社会治理 | A7 | +| 第 A8 版:学员天地 | A8 | + + + ## 连线 Wired 非订阅用户每月有阅读全文次数限制。 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..17088b030 --- /dev/null +++ b/lib/v2/cntheory/paper.js @@ -0,0 +1,93 @@ +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 = 'http://paper.cntheory.com'; + + let response = await got({ + method: 'get', + url: rootUrl, + }); + + response = await got({ + method: 'get', + url: `${rootUrl}/${id ? response.data.match(/URL=(.*)"/)[1].replace(/xxsb_\w+\.htm$/, `xxsb_${id}.htm`) : 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 = $('table') + .last() + .find('a') + .toArray() + .map((a) => `${link}/${$(a).attr('href')}`); + + if (!id) { + await Promise.all( + $('#pageLink') + .toArray() + .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() + .replace(/display:none;/g, ''), + description: content('founder-content').html(), + }), + }; + }) + ) + ); + + ctx.state.data = { + title: `学习时报${id ? ` - ${$('.l_t').first().text()}` : ''}`, + link: rootUrl, + item: items, + }; +}; diff --git a/lib/v2/cntheory/radar.js b/lib/v2/cntheory/radar.js new file mode 100644 index 000000000..0b917888e --- /dev/null +++ b/lib/v2/cntheory/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'cntheory.com': { + _name: '理论网', + paper: [ + { + title: '学习时报', + docs: 'https://docs.rsshub.app/traditional-media.html#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..6276cecf5 --- /dev/null +++ b/lib/v2/cntheory/templates/description.art @@ -0,0 +1,2 @@ +{{@ resource }} +{{@ description }} \ No newline at end of file