add 半月谈 (#1799)

This commit is contained in:
Chenyang Shi
2019-03-22 17:13:41 +08:00
committed by DIYgod
parent 13e759f91c
commit 02c7b212cd
3 changed files with 71 additions and 0 deletions

View File

@@ -3227,3 +3227,7 @@ type 为 all 时category 参数不支持 cost 和 free
<route name="观点" author="LogicJake" example="/people/opinion/223228" path="/people/opinion/:id" :paramsDesc="['板块id可在 URL 中找到']"/>
</route>
### 半月谈
<route name="板块" author="LogicJake" example="/banyuetan/jicengzhili" path="/banyuetan/:name" :paramsDesc="['板块名称,可在 URL 中找到']"/>

View File

@@ -1174,6 +1174,9 @@ router.get('/zjgsu/tzgg', require('./routes/universities/zjgsu/tzgg/scripts'));
router.get('/zjgsu/gsgg', require('./routes/universities/zjgsu/gsgg/scripts'));
router.get('/zjgsu/xszq', require('./routes/universities/zjgsu/xszq/scripts'));
// 半月谈
router.get('/banyuetan/:name', require('./routes/banyuetan'));
// 人民日报
router.get('/people/opinion/:id', require('./routes/people/opinion'));

View File

@@ -0,0 +1,64 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const name = ctx.params.name;
const link = `http://www.banyuetan.org/byt/${name}/index.html`;
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const list = $('ul.clearFix li')
.slice(0, 10)
.map(function() {
const info = {
title: $(this)
.find('h3 a')
.text(),
link: $(this)
.find('h3 a')
.attr('href'),
date: $(this)
.find('span.tag3')
.text(),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const date = info.date;
const itemUrl = info.link;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios.get(itemUrl);
const $ = cheerio.load(response.data);
const description = $('div.detail_content')
.html()
.trim();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: new Date(date).toUTCString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${name}-半月谈`,
link: link,
item: out,
};
};