From 0ea5e3c3b01bd4e9ad009790f86f1eb543d9c98e Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Wed, 27 Mar 2019 11:08:39 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E4=B9=A0=E5=A4=A7=E5=A4=A7=E8=AE=B2?= =?UTF-8?q?=E8=AF=9D=20(#1830)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #1824 --- docs/traditional-media.md | 1 + lib/router.js | 1 + lib/routes/people/xjpjh.js | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/routes/people/xjpjh.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index f401d52e8..0f1b0d832 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -204,6 +204,7 @@ category 对应的关键词有 ## 人民日报 + ## 半月谈 diff --git a/lib/router.js b/lib/router.js index 8855142d3..e9859f471 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1190,6 +1190,7 @@ router.get('/banyuetan/:name', require('./routes/banyuetan')); // 人民日报 router.get('/people/opinion/:id', require('./routes/people/opinion')); +router.get('/people/xjpjh/:keyword?/:year?', require('./routes/people/xjpjh')); // gamersky router.get('/gamersky/news', require('./routes/gamersky/news')); diff --git a/lib/routes/people/xjpjh.js b/lib/routes/people/xjpjh.js new file mode 100644 index 000000000..e669d8d7c --- /dev/null +++ b/lib/routes/people/xjpjh.js @@ -0,0 +1,74 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'http://jhsjk.people.cn'; + +module.exports = async (ctx) => { + let keyword = ctx.params.keyword || 'all'; + let year = ctx.params.year || 0; + + let title = '习近平系列重要讲话'; + title = title + '-' + keyword; + if (keyword === 'all') { + keyword = ''; + } + if (year !== 0) { + title = title + '-' + year; + year = year - 1811; + } else { + title = title + '-all'; + } + + const link = `http://jhsjk.people.cn/result?keywords=${keyword}&year=${year}`; + const response = await axios.get(link); + + const $ = cheerio.load(response.data); + + const list = $('ul.list_14.p1_2.clearfix li') + .slice(0, 10) + .map(function() { + const info = { + title: $(this) + .find('a') + .text(), + link: $(this) + .find('a') + .attr('href'), + }; + return info; + }) + .get(); + + const out = await Promise.all( + list.map(async (info) => { + const title = info.title; + 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 axios.get(itemUrl); + const $ = cheerio.load(response.data); + const description = $('div.d2txt_con.clearfix') + .html() + .trim(); + + const single = { + title: title, + link: itemUrl, + description: description, + }; + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: title, + link: link, + item: out, + }; +};