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, + }; +};