From 85afee25fd212eeb42887ce00243f80a2a11fc73 Mon Sep 17 00:00:00 2001 From: terrytw <24227628+terrytw@users.noreply.github.com> Date: Tue, 2 Aug 2022 21:32:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E5=9B=BD=E5=AE=B6=E8=8D=AF?= =?UTF-8?q?=E5=93=81=E7=9B=91=E7=9D=A3=E7=AE=A1=E7=90=86=E5=B1=80=E5=8C=BB?= =?UTF-8?q?=E7=96=97=E5=99=A8=E6=A2=B0=E6=8A=80=E6=9C=AF=E5=AE=A1=E8=AF=84?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=20=E8=8E=B7=E5=8F=96=E5=85=A8=E6=96=87=20(#1?= =?UTF-8?q?0357)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update index.js * Update index.js * Update index.js * Update index.js * Update cdrh.js * Revert "Update cdrh.js" This reverts commit 18b5174f109f8e7a26c974d47357b6309ec64781. * Update lib/v2/cmde/index.js * Update index.js --- lib/v2/cmde/index.js | 89 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/lib/v2/cmde/index.js b/lib/v2/cmde/index.js index 422f0f960..c45e32790 100644 --- a/lib/v2/cmde/index.js +++ b/lib/v2/cmde/index.js @@ -1,44 +1,75 @@ const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); const { parseDate } = require('@/utils/parse-date'); const rootURL = 'https://www.cmde.org.cn'; module.exports = async (ctx) => { const cate = ctx.params.cate ?? 'xwdt/zxyw'; - const link = `${rootURL}/${cate}/`; - + const url = `${rootURL}/${cate}/`; const browser = await require('@/utils/puppeteer')({ stealth: true }); - const page = await browser.newPage(); - await page.setRequestInterception(true); - page.on('request', (request) => { - request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort(); - }); - await page.goto(link); - await page.waitForSelector('.child-list .list ul'); - const html = await page.evaluate(() => document.querySelector('.child-list .list ul').innerHTML); - const title = await page.evaluate(() => document.title); - browser.close(); + const data = await ctx.cache.tryGet( + url, + async () => { + const page = await browser.newPage(); + await page.setRequestInterception(true); + page.on('request', (request) => { + request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort(); + }); + await page.goto(url, { + waitUntil: 'domcontentloaded', + }); + await page.waitForSelector('.list'); + const html = await page.evaluate(() => document.documentElement.innerHTML); + await page.close(); - const $ = cheerio.load(html); - const list = $('li'); + const $ = cheerio.load(html); - const data = { - title, - link, - item: list - .map((_, item) => ({ - title: $(item).find('a').attr('title'), - link: new URL($(item).find('a').attr('href'), link).href, - pubDate: parseDate($(item).find('span:last-child').text().replace(/\(|\)/g, '')), - })) - .get(), - }; + return { + title: $('head title').text(), + description: $('meta[name=ColumnDescription]').attr('content'), + items: $('.list ul li') + .toArray() + .map((item) => { + item = $(item); + return { + title: $(item).find('a').attr('title'), + link: new URL($(item).find('a').attr('href'), url).href, + }; + }), + }; + }, + ); - ctx.state.data = data; + const items = await Promise.all( + data.items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const page = await browser.newPage(); + await page.setRequestInterception(true); + page.on('request', (request) => { + request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort(); + }); + await page.goto(item.link, { + waitUntil: 'domcontentloaded', + }); + await page.waitForSelector('.text'); - ctx.state.json = { + const html = await page.evaluate(() => document.documentElement.innerHTML); + await page.close(); + const $ = cheerio.load(html); + item.description = $('.text').html(); + item.pubDate = timezone(parseDate($('meta[name="PubDate"]').attr('content')), +8); + return item; + }) + ) + ); + + await browser.close(); + + ctx.state.data = { title: data.title, - link: data.link, - item: data.item.map((item) => item.title), + description: data.description, + link: url, + item: items, }; };