From eb972671b97c5024fceda05b04641679463aecae Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Mon, 18 Jan 2021 04:49:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E5=8D=8A=E6=9C=88=E8=B0=88?= =?UTF-8?q?=E6=97=B6=E4=BA=8B=E5=A4=A7=E4=BA=8B=E5=BA=93=20(#6711)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add 半月谈时事大事库 * fix: fetch 10 posts each time --- docs/new-media.md | 12 +++++++++ lib/router.js | 1 + lib/routes/banyuetan/byt.js | 51 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/routes/banyuetan/byt.js diff --git a/docs/new-media.md b/docs/new-media.md index c36877c45..6570059bd 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -684,6 +684,18 @@ Supported sub-sites: +## 半月谈 + +### 时事大事库 + + + +| 每周 | 每月 | +| ------------- | ----- | +| shishidashiku | yiyue | + + + ## 報導者 ### 最新 diff --git a/lib/router.js b/lib/router.js index ad736c3e6..462adc2c4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1536,6 +1536,7 @@ router.get('/zjgsu/xszq', require('./routes/universities/zjgsu/xszq/scripts')); router.get('/dianping/user/:id?', require('./routes/dianping/user')); // 半月谈 +router.get('/banyuetan/byt/:time?', require('./routes/banyuetan/byt')); router.get('/banyuetan/:name', require('./routes/banyuetan')); // 人民日报 diff --git a/lib/routes/banyuetan/byt.js b/lib/routes/banyuetan/byt.js new file mode 100644 index 000000000..4c9efd7f1 --- /dev/null +++ b/lib/routes/banyuetan/byt.js @@ -0,0 +1,51 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const time = ctx.params.time || 'shishidashiku'; + + const rootUrl = 'http://www.banyuetan.org'; + const currentUrl = `${rootUrl}/byt/${time}/index.html`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.byt_mian_image_list_con ul li a') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + link: item.attr('href'), + title: item.attr('title'), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('#detail_content').html(); + item.author = content('.detail_tit_source').text().replace('来源:', ''); + item.pubDate = new Date(content('meta[property="og:release_date"]').attr('content')).toUTCString(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};