From a0f067ca71755d7409db4812d38bbefe03158844 Mon Sep 17 00:00:00 2001 From: FledgeShiu Date: Fri, 15 Apr 2022 21:07:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E9=A6=96=E5=B8=AD=E7=BB=8F?= =?UTF-8?q?=E6=B5=8E=E5=AD=A6=E5=AE=B6=20(#9543)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add chinacef router. * Update the document. * Move to V2. * Fix document errors and improve the code. --- docs/finance.md | 6 ++ lib/v2/chinacef/index.js | 67 +++++++++++++++++++++++ lib/v2/chinacef/maintainer.js | 3 + lib/v2/chinacef/radar.js | 13 +++++ lib/v2/chinacef/router.js | 3 + lib/v2/chinacef/templates/description.art | 1 + lib/v2/chinacef/utils.js | 11 ++++ 7 files changed, 104 insertions(+) create mode 100644 lib/v2/chinacef/index.js create mode 100644 lib/v2/chinacef/maintainer.js create mode 100644 lib/v2/chinacef/radar.js create mode 100644 lib/v2/chinacef/router.js create mode 100644 lib/v2/chinacef/templates/description.art create mode 100644 lib/v2/chinacef/utils.js diff --git a/docs/finance.md b/docs/finance.md index a748f4312..5ca9d2c5e 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -237,6 +237,12 @@ pageClass: routes +## 首席经济学家论坛 + +### 最新更新文章列表 + + + ## 淘股吧股票论坛 ### 论坛总版 diff --git a/lib/v2/chinacef/index.js b/lib/v2/chinacef/index.js new file mode 100644 index 000000000..f84b2d265 --- /dev/null +++ b/lib/v2/chinacef/index.js @@ -0,0 +1,67 @@ +const utils = require('./utils'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const rootUrl = 'http://www.chinacef.cn'; + const regex = /^\/index\.php\/index\/article\/article_id\/\d+/g; + const timeRegex = /(\d{4}-\d{2}-\d{2})/g; + + const response = await got({ + method: 'get', + url: rootUrl, + }); + const $ = cheerio.load(response.data); + + const articlesLinkListNode = $('a[target=_black]') + .filter((_, item) => item.attribs.href.match(regex)) + .map((_, item) => item.attribs.href); + const articlesLinkList = Array.from(new Set(articlesLinkListNode)); + + const out = await Promise.all( + articlesLinkList.map(async (item) => { + const articlesLink = rootUrl + item; + const $ = await await ctx.cache.tryGet(articlesLink, async () => { + const response = await got({ + method: 'get', + url: articlesLink, + }); + return cheerio.load(response.data); + }); + + // Clean article style + $('*[style]').removeAttr('style'); + $('br').remove(); + $('span:empty').remove(); + $('p:empty').remove(); + $('p').each((_, el) => { + if ($(el).html().trim() === '') { + $(el).remove(); + } + }); + + const title = $('span[class=contenttitle]').first().text().trim(); + const author = $('a[rel=author]').first().text().trim(); + const time = $('a[rel=author]').parent().text().match(timeRegex)[0]; + const pubDate = parseDate(time, 'YYYY-MM-DD'); + const description = $('div[class=newsmaintext]').html(); + + const element = { + title, + author, + pubDate, + description: utils.renderDesc(description), + link: articlesLink, + }; + return element; + }) + ); + + ctx.state.data = { + title: '首席经济学家论坛', + description: '首席经济学家最近更新的文章', + link: rootUrl, + item: out, + }; +}; diff --git a/lib/v2/chinacef/maintainer.js b/lib/v2/chinacef/maintainer.js new file mode 100644 index 000000000..ea25bbdb5 --- /dev/null +++ b/lib/v2/chinacef/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/chinacef': ['FledgeXu'], +}; diff --git a/lib/v2/chinacef/radar.js b/lib/v2/chinacef/radar.js new file mode 100644 index 000000000..6a390fdcb --- /dev/null +++ b/lib/v2/chinacef/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'chinacef.cn': { + _name: '首席经济学家论坛', + '.': [ + { + title: '最新文章列表', + docs: 'https://docs.rsshub.app/finance.html#shou-xi-jing-ji-xue-jia-lun-tan', + source: ['/'], + target: '/chinacef', + }, + ], + }, +}; diff --git a/lib/v2/chinacef/router.js b/lib/v2/chinacef/router.js new file mode 100644 index 000000000..20c52b09d --- /dev/null +++ b/lib/v2/chinacef/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/', require('./index')); +}; diff --git a/lib/v2/chinacef/templates/description.art b/lib/v2/chinacef/templates/description.art new file mode 100644 index 000000000..7755ee9f6 --- /dev/null +++ b/lib/v2/chinacef/templates/description.art @@ -0,0 +1 @@ +{{@ desc }} diff --git a/lib/v2/chinacef/utils.js b/lib/v2/chinacef/utils.js new file mode 100644 index 000000000..d9c27e31c --- /dev/null +++ b/lib/v2/chinacef/utils.js @@ -0,0 +1,11 @@ +const { art } = require('@/utils/render'); +const path = require('path'); + +const renderDesc = (desc) => + art(path.join(__dirname, 'templates/description.art'), { + desc, + }); + +module.exports = { + renderDesc, +};