feat: 添加对【深圳证券交易所-上市公告-可转换债券】的支持 (#3171)

This commit is contained in:
Jeason0228 2019-09-30 15:08:30 +08:00 committed by DIYgod
parent 607e836a22
commit 6586f09907
3 changed files with 95 additions and 0 deletions

View File

@ -289,6 +289,12 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="kt286" example="/sse/convert/beginDate=2018-08-18&endDate=2019-08-18&companyCode=603283&title=股份" path="/sse/convert/:query?" :paramsDesc="['筛选条件,见示例']"/>
## 深圳证券交易所
### 上市公告-可转换债券
<Route author="Jeason0228" example="/szse/notice" path="/szse/notice"/>
## 搜狗
### 搜狗特色 LOGO

View File

@ -1692,6 +1692,9 @@ router.get('/latexstudio/home', require('./routes/latexstudio/home'));
// 上证债券信息网 - 可转换公司债券公告
router.get('/sse/convert/:query?', require('./routes/sse/convert'));
// 深圳证券交易所——上市公告
router.get('/szse/notice/', require('./routes/szse/notice'));
// 前端艺术家每日整理&&飞冰早报
router.get('/jskou/:type?', require('./routes/jskou/index'));

86
lib/routes/szse/notice.js Normal file
View File

@ -0,0 +1,86 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const host = 'http://www.szse.cn/';
module.exports = async (ctx) => {
const link = 'http://www.szse.cn/disclosure/notice/company/index.html';
const response = await got.get(link, {
Referer: host,
});
const $ = cheerio.load(response.data);
// 正则表达式匹配Script标签的url和title变量
function getData(jscontent, option) {
// eslint-disable-next-line
const urlpattern = /(?<=curHref \= \'\.\/).*?(?=\'\;)/;
// eslint-disable-next-line
const titlepattern = /(?<=curTitle \=\').*?(?=\'\;)/;
switch (option) {
case 'title': {
const jsoutput = jscontent.match(titlepattern);
return jsoutput;
}
case 'url': {
const jsoutput = jscontent.match(urlpattern);
return jsoutput;
}
default: {
// console.log('default');
break;
}
}
}
const list = $('.article-list .newslist li')
.map(function() {
const info = {
title: getData(
$(this)
.find('script')
.html(),
'title'
),
link: `http://www.szse.cn/disclosure/notice/company/${getData(
$(this)
.find('script')
.html(),
'url'
)}`,
date: $(this)
.find('span.time')
.text()
.trim(), // trim移除多余的空格
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const date = info.date;
const itemUrl = url.resolve(host, info.link);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl, {
Referer: host,
});
const $ = cheerio.load(response.data);
const description = $('#desContent').html();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: new Date(date).toDateString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: '深圳证券交易所——上市公告-可转换债券',
link: link,
item: out,
};
};