From 00170e320c16b7a6aa6e75a52f91c31b7a744c7c Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 7 Jun 2020 23:44:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=B8=AD=E8=AF=81=E7=BD=91?= =?UTF-8?q?=E4=B8=AD=E8=AF=81=E5=BF=AB=E8=AE=AF=20&=20=E8=A1=8C=E4=B8=9A?= =?UTF-8?q?=E8=B5=84=E8=AE=AF=20(#4920)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/finance.md | 12 +++++++++ lib/router.js | 3 +++ lib/routes/cs/news.js | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 lib/routes/cs/news.js diff --git a/docs/finance.md b/docs/finance.md index 050c13a64e..ac108dbf5d 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -105,3 +105,15 @@ pageClass: routes ### 货币政策司公开市场交易公告 + +## 中证网 + +### 资讯 + + + +| 中证快讯 | 行业资讯 | +| -------- | -------- | +| zzkx | hyzx | + + diff --git a/lib/router.js b/lib/router.js index fc0a165ca1..ea95498c99 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2814,6 +2814,9 @@ router.get('/gov/caict/bps', require('./routes/gov/caict/bps')); router.get('/gov/caict/qwsj', require('./routes/gov/caict/qwsj')); router.get('/gov/caict/caictgd', require('./routes/gov/caict/caictgd')); +// 中证网 +router.get('/cs/news/:caty', require('./routes/cs/news')); + // 财联社 router.get('/cls/telegraph', require('./routes/cls/telegraph')); router.get('/cls/depth', require('./routes/cls/depth')); diff --git a/lib/routes/cs/news.js b/lib/routes/cs/news.js new file mode 100644 index 0000000000..8ab6cd2ff1 --- /dev/null +++ b/lib/routes/cs/news.js @@ -0,0 +1,63 @@ +const url = require('url'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +const rootUrl = 'http://www.cs.com.cn/'; + +const config = { + zzkx: { + link: '/sylm/jsbd/', + title: '中证快讯', + }, + hyzx: { + link: '/cj/hyzx/', + title: '行业资讯', + }, +}; + +module.exports = async (ctx) => { + const cfg = config[ctx.params.caty]; + if (!cfg) { + throw Error('Bad category. See docs'); + } + + const currentUrl = url.resolve(rootUrl, cfg.link); + const response = await got({ + method: 'get', + url: currentUrl, + responseType: 'buffer', + }); + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + const list = $('ul.list-lm li') + .slice(0, 20) + .map((_, item) => { + item = $(item); + const a = item.find('a'); + return { + title: a.text(), + link: url.resolve(currentUrl, a.attr('href')), + pubDate: new Date(item.find('span').text()).toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ method: 'get', url: item.link, responseType: 'buffer' }); + const content = cheerio.load(iconv.decode(res.data, 'gbk')); + + item.description = content('div.article-t').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: '中证网 - ' + cfg.title, + link: rootUrl, + item: items, + }; +};