From 0e75c8173d72e76caae7d7eaf54f23a12aa3faac Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Fri, 23 Oct 2020 23:40:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E8=AF=81=E5=88=B8=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E7=BD=91=E5=BF=AB=E8=AE=AF=20(#5965)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/finance.md | 10 +++++ lib/router.js | 1 + lib/routes/stcn/kuaixun.js | 92 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 lib/routes/stcn/kuaixun.js diff --git a/docs/finance.md b/docs/finance.md index 0d7e84196c..85431a16ff 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -160,6 +160,16 @@ pageClass: routes +### 快讯 + + + +| 快讯 | e 公司 | 研报 | 时事 | 财经 | +| ---- | ------ | ---- | ---- | ---- | +| | egs | yb | ss | cj | + + + ## 中国人民银行 ### 沟通交流 diff --git a/lib/router.js b/lib/router.js index e4c535b187..06420e2bd3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3372,6 +3372,7 @@ router.get('/allnow/column/:id', require('./routes/allnow/column')); // 证券时报网 router.get('/stcn/news/:id?', require('./routes/stcn/news')); router.get('/stcn/data/:id?', require('./routes/stcn/data')); +router.get('/stcn/kuaixun/:id?', require('./routes/stcn/kuaixun')); // dev.to router.get('/dev.to/top/:period', require('./routes/dev.to/top')); diff --git a/lib/routes/stcn/kuaixun.js b/lib/routes/stcn/kuaixun.js new file mode 100644 index 0000000000..b0ed55bd52 --- /dev/null +++ b/lib/routes/stcn/kuaixun.js @@ -0,0 +1,92 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const rootUrl = 'https://kuaixun.stcn.com'; + +const config = { + '': { + link: rootUrl, + title: '快讯', + }, + egs: { + link: `${rootUrl}/egs/`, + title: 'e公司', + }, + yb: { + link: `${rootUrl}/yb/`, + title: '研报', + }, + ss: { + link: `${rootUrl}/ss/`, + title: '时事', + }, + cj: { + link: `${rootUrl}/cj/`, + title: '财经', + }, +}; + +module.exports = async (ctx) => { + ctx.params.id = ctx.params.id || ''; + + const cfg = config[ctx.params.id]; + + const currentUrl = cfg.link; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('#news_list2 li a') + .slice(0, 20) + .map((_, item) => { + item = $(item); + let link = item.attr('href'); + if (link.indexOf('.') === 0) { + link = `${currentUrl}${link.replace('.', '')}`; + } + return { + link, + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + let info = content('div.info'); + + if (info.html() === null) { + info = content('h2 span'); + info.find('i').remove(); + } else { + info.find('span').remove(); + } + item.pubDate = new Date(info.text().split('来源')[0].trim()).toUTCString(); + + const title = content('h2'); + title.find('span').remove(); + + item.title = title.text().replace('为你推荐', ''); + item.description = content('#ctrlfscont').html() || content('.text').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: '证券时报网 - ' + cfg.title, + link: currentUrl, + item: items, + }; +};