增加: 创业邦 (#1440)

Closes #1398
This commit is contained in:
凉凉
2019-01-20 22:03:00 +08:00
committed by DIYgod
parent 210f6dbc08
commit b776aa6dd3
3 changed files with 63 additions and 0 deletions

View File

@@ -2805,3 +2805,7 @@ board 和 build 可在[这里](http://api.ineal.me/tss/status)查看
### 多抓鱼
<route name="搜索结果" author="fengkx" example="/duozhuayu/search/JavaScript" path="/duozhuayu/search/:wd" :paramsDesc="['搜索关键词']"/>
### 创业邦
<route name="作者" author="xyqfer" example="/cyzone/author/1225562" path="/cyzone/author/:id" :paramsDesc="['作者 id']"/>

View File

@@ -995,4 +995,7 @@ router.get('/wallstreetcn/news/global', require('./routes/wallstreetcn/news'));
// 多抓鱼搜索
router.get('/duozhuayu/search/:wd', require('./routes/duozhuayu/search'));
// 创业邦
router.get('/cyzone/author/:id', require('./routes/cyzone/author'));
module.exports = router;

View File

@@ -0,0 +1,56 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
module.exports = async (ctx) => {
const { id } = ctx.params;
const url = `https://www.cyzone.cn/author/${id}`;
const res = await axios.get(url);
const data = res.data;
const $ = cheerio.load(data);
const list = $('.article-item');
const out = await Promise.all(
list
.map(async (index, elem) => {
const $elem = $(elem);
const $title = $elem.find('.item-title');
const link = 'https:' + $title.attr('href');
const pubDate = new Date(+$elem.find('.time').attr('data-time') * 1000).toUTCString();
const item = {
title: $title.text(),
pubDate,
link,
};
const key = `cyzone-${link}`;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const storyDetail = await axios.get(item.link);
const data = storyDetail.data;
const $ = cheerio.load(data);
$('.article-content img').each(function() {
const $img = $(this);
const src = $img.attr('src');
if (!src.startsWith('http')) {
$img.attr('src', 'https:' + src);
}
});
item.description = $('.article-content').html();
ctx.cache.set(key, item.description, 12 * 60 * 60);
}
return Promise.resolve(item);
})
.get()
);
ctx.state.data = {
title: `创业邦-作者 ${$('.ss-title').text()}`,
link: url,
description: '创业邦致力于成为中国创业者的信息平台和服务平台,帮助中国创业者实现创业梦想。创业邦为创业者和风险投资人提供各种创业类最新资讯和实用知识手册,打造创业者和投资人的社交平台。',
item: out,
};
};