diff --git a/docs/README.md b/docs/README.md index 780eb08e58..d7579511f2 100755 --- a/docs/README.md +++ b/docs/README.md @@ -3227,3 +3227,7 @@ type 为 all 时,category 参数不支持 cost 和 free + +### 半月谈 + + diff --git a/lib/router.js b/lib/router.js index 640a4b2933..3ff476f861 100755 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/banyuetan/index.js b/lib/routes/banyuetan/index.js new file mode 100644 index 0000000000..287b1b5169 --- /dev/null +++ b/lib/routes/banyuetan/index.js @@ -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, + }; +};