From 67542da7bf2a8babe0bdba17ca7196106d03c0ce Mon Sep 17 00:00:00 2001 From: sdsadfaf Date: Tue, 5 Oct 2021 11:18:03 +0200 Subject: [PATCH] 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 --- lib/routes/novel/ptwxz.js | 59 +++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/lib/routes/novel/ptwxz.js b/lib/routes/novel/ptwxz.js index df6746946..4dcf62c82 100644 --- a/lib/routes/novel/ptwxz.js +++ b/lib/routes/novel/ptwxz.js @@ -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, }; -}; +}; \ No newline at end of file