From bf56ef1dad613453401896ab491867c99c361d3e Mon Sep 17 00:00:00 2001 From: Jeason Lau Date: Tue, 18 Feb 2020 19:07:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=A8=BB=E8=8D=89=E4=BA=BA?= =?UTF-8?q?=E4=B9=A6=E5=B1=8B=20(#4019)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reading.md | 13 ++++++++++ lib/router.js | 3 +++ lib/routes/novel/dcrsw.js | 50 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 lib/routes/novel/dcrsw.js diff --git a/docs/reading.md b/docs/reading.md index ce40073f3f..481847214d 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -82,6 +82,19 @@ pageClass: routes +## 稻草人书屋 + +### 章节更新 + + + +::: warning 注意 + +count 的取值范围为 1-12,为防止请求次数过多,推荐设置为 5 以下。 +::: + + + ## 飞地 ### 分类 diff --git a/lib/router.js b/lib/router.js index db1d0badc6..ffb022234f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2259,4 +2259,7 @@ router.get('/haohaozhu/discover/:keyword?', require('./routes/haohaozhu/discover // 东北大学 router.get('/neu/news/:type', require('./routes/universities/neu/news')); +// 稻草人书屋 +router.get('/dcrsw/:name/:count?', require('./routes/novel/dcrsw')); + module.exports = router; diff --git a/lib/routes/novel/dcrsw.js b/lib/routes/novel/dcrsw.js new file mode 100644 index 0000000000..e09f409593 --- /dev/null +++ b/lib/routes/novel/dcrsw.js @@ -0,0 +1,50 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const baseUrl = 'https://www.daocaorenshuwu.com/book'; + +module.exports = async (ctx) => { + const name = ctx.params.name; + const count = ctx.params.count || 3; + const novelUrl = `${baseUrl}/${name}`; + const response = await got({ + method: 'get', + url: novelUrl, + }); + + const data = response.data; + const $ = cheerio.load(data); + + const title = $('h1.book-name') + .children() + .text(); + const description = $('div.book-detail').text(); + const chapters = $('.col-md-6') + .slice(0, count) + .get(); + const results = await Promise.all( + chapters.map(async (item) => { + const $ = cheerio.load(item); + + const chapterTitle = $('a').text(); + const chapterUrl = 'https:' + $('a').attr('href'); + const chapterContent = await ctx.cache.tryGet(chapterUrl, async () => { + const chapter = await got.get(chapterUrl); + const $ = cheerio.load(chapter.data); + $('div.cont-text > script').remove(); + return $('div.cont-text').html(); + }); + return Promise.resolve({ + title: chapterTitle, + link: chapterUrl, + description: chapterContent, + }); + }) + ); + ctx.state.data = { + title: `稻草人书屋-${title}`, + description: description, + link: novelUrl, + item: results, + }; +};