feat: add 稻草人书屋 (#4019)

This commit is contained in:
Jeason Lau
2020-02-18 19:07:13 +08:00
committed by GitHub
parent 04804d588e
commit bf56ef1dad
3 changed files with 66 additions and 0 deletions

View File

@@ -82,6 +82,19 @@ pageClass: routes
</Route>
## 稻草人书屋
### 章节更新
<Route author="JeasonLau" example="/dcrsw/zhongjidouluo/2" path="/dcrsw/:name/:count?" :paramsDesc="['小说名可在对应小说页URL中找到', '显示的章节数,缺省为`3`']">
::: warning 注意
count 的取值范围为 1-12为防止请求次数过多推荐设置为 5 以下。
:::
</Route>
## 飞地
### 分类

View File

@@ -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;

50
lib/routes/novel/dcrsw.js Normal file
View File

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