From fafb2d2ed44dad1eed42a7b1042be12657a2f1e5 Mon Sep 17 00:00:00 2001 From: FledgeShiu Date: Sun, 17 Apr 2022 21:42:03 +0800 Subject: [PATCH] fix(route): chinacerf (#9554) * fix(route): chinacerf * Remove redundant `await`. --- docs/finance.md | 2 +- lib/v2/chinacef/experts.js | 28 +++------- lib/v2/chinacef/hot.js | 27 +++------- lib/v2/chinacef/index.js | 52 +++++-------------- .../chinacef/templates/work_description.art | 1 - lib/v2/chinacef/utils.js | 48 ++++++++++++++++- 6 files changed, 72 insertions(+), 86 deletions(-) delete mode 100644 lib/v2/chinacef/templates/work_description.art diff --git a/docs/finance.md b/docs/finance.md index 6af1902d2..e303b493d 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -239,7 +239,7 @@ pageClass: routes ## 首席经济学家论坛 -### 最新更新文章列表 +### 最新更新 diff --git a/lib/v2/chinacef/experts.js b/lib/v2/chinacef/experts.js index 2e90e1785..eb48ecdbb 100644 --- a/lib/v2/chinacef/experts.js +++ b/lib/v2/chinacef/experts.js @@ -1,8 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { art } = require('@/utils/render'); -const path = require('path'); -const { parseDate } = require('@/utils/parse-date'); +const utils = require('./utils'); module.exports = async (ctx) => { const response = await got(`http://www.chinacef.cn/index.php/experts/zjmain/experts_id/${ctx.params.experts_id}`); @@ -12,8 +10,6 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const domArray = $('div[class^="leftnews"]'); - const itemAuthor = $('div[class=right515name] h3').text().trim(); - const siteLink = 'http://www.chinacef.cn'; const rssTitle = $('title').text().trim(); const itemArray = await Promise.all( @@ -21,32 +17,22 @@ module.exports = async (ctx) => { .filter((index, item) => undefined !== $(item).find('h2 > a').attr('href')) .map(async (index, item) => { item = $(item); - const itemLink = siteLink + item.find('h2 > a').attr('href'); - const detail = await ctx.cache.tryGet(itemLink, async () => { - const result = await got.get(itemLink); - const $ = cheerio.load(result.data); - const description = art(path.join(__dirname, 'templates/work_description.art'), { - desc: $('.newsmaintext').first().html().trim(), - }); - return { - title: $('.contenttitle').first().html().trim(), - article: description, - }; - }); + const itemLink = utils.siteLink + item.find('h2 > a').attr('href'); + const detail = await ctx.cache.tryGet(itemLink, async () => await utils.getArticleDetail(itemLink)); return { title: detail.title, - description: detail.article, + description: detail.description, link: itemLink, - pubDate: parseDate(item.find('span').first().text(), 'YYYY-MM-DD'), - author: itemAuthor, + pubDate: detail.time, + author: detail.author, }; }) ); ctx.state.data = { title: rssTitle, - link: siteLink, + link: utils.siteLink, item: itemArray, }; }; diff --git a/lib/v2/chinacef/hot.js b/lib/v2/chinacef/hot.js index bd0f50526..1637b9497 100644 --- a/lib/v2/chinacef/hot.js +++ b/lib/v2/chinacef/hot.js @@ -1,8 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { art } = require('@/utils/render'); -const path = require('path'); -const { parseDate } = require('@/utils/parse-date'); +const utils = require('./utils'); module.exports = async (ctx) => { const response = await got.get('http://www.chinacef.cn/index.php/index/index'); @@ -12,33 +10,20 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const domArray = $('#boardright ul'); - const siteLink = 'http://www.chinacef.cn'; const rssTitle = '金融热点 - 首席经济学家论坛'; const itemArray = await Promise.all( domArray .map(async (index, item) => { item = $(item); - const itemLink = siteLink + item.find('li a').attr('href'); - const detail = await ctx.cache.tryGet(itemLink, async () => { - const result = await got.get(itemLink); - const $ = cheerio.load(result.data); - const description = art(path.join(__dirname, 'templates/work_description.art'), { - desc: $('.newsmaintext').first().html().trim(), - }); - return { - title: $('.contenttitle').first().html().trim(), - author: $('.zzinfo').find('h2').first().text().trim(), - pubDate: parseDate($('.divwidth').text().trim().split(' ')[3], 'YYYY-MM-DD'), - article: description, - }; - }); + const itemLink = utils.siteLink + item.find('li a').attr('href'); + const detail = await ctx.cache.tryGet(itemLink, async () => await utils.getArticleDetail(itemLink)); return { title: detail.title, - description: detail.article, + description: detail.description, link: itemLink, - pubDate: detail.pubDate, + pubDate: detail.time, author: detail.author, }; }) @@ -47,7 +32,7 @@ module.exports = async (ctx) => { ctx.state.data = { title: rssTitle, - link: siteLink, + link: utils.siteLink, item: itemArray, }; }; diff --git a/lib/v2/chinacef/index.js b/lib/v2/chinacef/index.js index f84b2d265..18725a549 100644 --- a/lib/v2/chinacef/index.js +++ b/lib/v2/chinacef/index.js @@ -1,17 +1,12 @@ 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 rssTitle = '最新更新 - 首席经济学家论坛'; 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 response = await got.get('http://www.chinacef.cn/index.php/index/index'); const $ = cheerio.load(response.data); const articlesLinkListNode = $('a[target=_black]') @@ -19,39 +14,17 @@ module.exports = async (ctx) => { .map((_, item) => item.attribs.href); const articlesLinkList = Array.from(new Set(articlesLinkListNode)); - const out = await Promise.all( + const articles = 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); - }); + const articlesLink = utils.siteLink + item; - // 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 detail = await ctx.cache.tryGet(articlesLink, async () => await utils.getArticleDetail(articlesLink)); const element = { - title, - author, - pubDate, - description: utils.renderDesc(description), + title: detail.title, + author: detail.author, + pubDate: detail.time, + description: detail.description, link: articlesLink, }; return element; @@ -59,9 +32,8 @@ module.exports = async (ctx) => { ); ctx.state.data = { - title: '首席经济学家论坛', - description: '首席经济学家最近更新的文章', - link: rootUrl, - item: out, + title: rssTitle, + link: utils.siteLink, + item: articles, }; }; diff --git a/lib/v2/chinacef/templates/work_description.art b/lib/v2/chinacef/templates/work_description.art deleted file mode 100644 index 25baa934c..000000000 --- a/lib/v2/chinacef/templates/work_description.art +++ /dev/null @@ -1 +0,0 @@ -
{{ desc }}
diff --git a/lib/v2/chinacef/utils.js b/lib/v2/chinacef/utils.js index d9c27e31c..611eedc2a 100644 --- a/lib/v2/chinacef/utils.js +++ b/lib/v2/chinacef/utils.js @@ -1,11 +1,55 @@ const { art } = require('@/utils/render'); const path = require('path'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const siteLink = 'http://www.chinacef.cn'; const renderDesc = (desc) => art(path.join(__dirname, 'templates/description.art'), { desc, }); -module.exports = { - renderDesc, +const cleanDom = (dom) => { + dom('*[style]').removeAttr('style'); + dom('br').remove(); + dom('span:empty').remove(); + dom('p:empty').remove(); + dom('p').each((_, el) => { + if (dom(el).html().trim() === '') { + dom(el).remove(); + } + }); + return dom; +}; + +const getArticleDetail = async (link) => { + const response = await got({ + method: 'get', + url: link, + }); + const $ = cleanDom(cheerio.load(response.data)); + + const title = $('span[class=contenttitle]').first().text().trim(); + const author = $('a[rel=author]').first().text().trim(); + const time = parseDate($('.divwidth').text().trim().split(' ')[3], 'YYYY-MM-DD'); + const description = $('div[class=newsmaintext]').html(); + return new ArticleDetail(title, author, time, renderDesc(description)); +}; +class ArticleDetail { + constructor(title, author, time, description) { + this.title = title; + this.author = author; + this.time = time; + this.description = description; + } +} + +module.exports = { + siteLink, + renderDesc, + cleanDom, + getArticleDetail, + ArticleDetail, };