fix(route)(ptwxz): ptwxz add fulltext (#8331)

Co-authored-by: auto-bot-ty <75887908+auto-bot-ty@users.noreply.github.com>
Co-authored-by: NeverBehave <gayhub@never.pet>
This commit is contained in:
sdsadfaf 2021-10-05 11:18:03 +02:00 committed by GitHub
parent b46232191a
commit 67542da7bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 15 deletions

View File

@ -3,6 +3,8 @@ const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const baseUrl = 'https://www.ptwxz.com/bookinfo/';
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
// 获取小说的最新章节列表
module.exports = async (ctx) => {
@ -24,24 +26,51 @@ module.exports = async (ctx) => {
const title = $('span>h1').text();
const list = $('.grid>tbody>tr>td>li>a');
const chapter_item = $('#content li a')
.map((_, item) => {
item = $(item);
const date = item.attr('title').match(/更新时间:(.*)/)[1]; // "10-04 00:17"
return {
title: item.text(),
link: item.attr('href'),
pubDate: timezone(parseDate(date, 'MM-DD HH:mm'), +8),
};
})
.get();
const items = await Promise.all(
chapter_item.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got({
method: 'get',
url: `https://www.ptwxz.com/${item.link}`,
responseType: 'buffer',
});
const responseHtml = iconv.decode(response.data, 'GBK');
const $ = cheerio.load(responseHtml);
// remove all unwanted elements
// TODO: cleaner solution to rip text from body
$('div').remove();
$('h1').remove();
$('table').remove();
$('center').remove();
$('table').remove();
$('script').remove();
const description = $('body').html();
const single = {
title: item.title,
description,
link: item.link,
pubDate: item.pubDate,
};
return single;
})
)
);
ctx.state.data = {
title,
link: `${baseUrl}${id}.html`,
description: '',
item:
list &&
list
.map((index, item) => {
item = $(item);
return {
title: item[0].children[0].data,
description: '',
link: item.attr('href'),
};
})
.get(),
item: items,
};
};
};