diff --git a/docs/finance.md b/docs/finance.md index 49653029e..467219e05 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -138,6 +138,18 @@ pageClass: routes +## 证券时报网 + +### 要闻 + + + +| 要闻 | 滚动 | 深度 | 评论 | +| ---- | ---- | ---- | ---- | +| news | gd | sd | pl | + + + ## 中国人民银行 ### 沟通交流 diff --git a/lib/router.js b/lib/router.js index 1803bf2f2..c5db46425 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3342,4 +3342,7 @@ router.get('/yuanshen/:location?/:category?', require('./routes/yuanshen/index') // World Trade Organization router.get('/wto/dispute-settlement/:year?', require('./routes/wto/dispute-settlement')); +// 证券时报网 +router.get('/stcn/news/:id?', require('./routes/stcn/news')); + module.exports = router; diff --git a/lib/routes/stcn/news.js b/lib/routes/stcn/news.js new file mode 100644 index 000000000..832cf85db --- /dev/null +++ b/lib/routes/stcn/news.js @@ -0,0 +1,86 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const config = { + news: { + link: 'https://news.stcn.com/news', + title: '要闻', + }, + gd: { + link: 'https://www.stcn.com/gd', + title: '滚动', + }, + sd: { + link: 'https://www.stcn.com/sd', + title: '深度', + }, + pl: { + link: 'https://www.stcn.com/pl', + title: '评论', + }, +}; + +module.exports = async (ctx) => { + ctx.params.id = ctx.params.id || 'news'; + + const cfg = config[ctx.params.id]; + const currentUrl = cfg.link; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + let query = '.tit a'; + if (ctx.params.id === 'gd') { + query = 'li a[title]'; + } + + const $ = cheerio.load(response.data); + const list = $(query) + .slice(0, 20) + .map((_, item) => { + item = $(item); + let link = item.attr('href'); + if (link.indexOf('.') === 0) { + link = `${currentUrl}${link.replace('.', '')}`; + } + return { + title: item.text(), + 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(); + + item.description = content('#ctrlfscont').html() || content('.text').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: '证券时报网 - ' + cfg.title, + link: currentUrl, + item: items, + }; +};