fix(route): NMPA get accurate pubdate (#12801)

* Update generic.js

* Update index.js

* fix: nmpa ditch puppeteer

---------
This commit is contained in:
terrytw 2023-07-15 16:45:47 +08:00 committed by GitHub
parent 252f53beb5
commit e52c82971f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 46 deletions

View File

@ -331,7 +331,7 @@ pageClass: routes
### 通用
<Route author="TonyRL" example="/gov/nmpa/xxgk/ggtg" path="/gov/nmpa/:path+" :paramsDesc="['路径,默认为公告通告']" radar="1" rssbud="1" puppeteer="1">
<Route author="TonyRL" example="/gov/nmpa/xxgk/ggtg" path="/gov/nmpa/:path+" :paramsDesc="['路径,默认为公告通告']" radar="1" rssbud="1">
::: tip 提示
@ -347,7 +347,7 @@ pageClass: routes
### 通用
<Route author="nczitzk" example="/gov/nifdc/bshff/ylqxbzhgl/qxggtzh" path="/gov/nifdc/:path+" :paramsDesc="['路径,默认为公告通告']" radar="1" rssbud="1" puppeteer="1">
<Route author="nczitzk" example="/gov/nifdc/bshff/ylqxbzhgl/qxggtzh" path="/gov/nifdc/:path+" :paramsDesc="['路径,默认为公告通告']" radar="1" rssbud="1">
::: tip 提示

View File

@ -36,29 +36,16 @@ module.exports = async (ctx) => {
};
});
const browser = await require('@/utils/puppeteer')();
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
if (/^https:\/\/www\.nmpa\.gov\.cn\//.test(item.link)) {
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');
const html = await page.evaluate(() => document.documentElement.innerHTML);
await page.close();
const { data: html } = await got(item.link);
const content = cheerio.load(html);
item.description = content('.text').html();
item.pubDate = timezone(parseDate($('meta[name="PubDate"]').attr('content')), +8);
} else {
const detailResponse = await got({
method: 'get',
@ -76,8 +63,6 @@ module.exports = async (ctx) => {
)
);
await browser.close();
ctx.state.data = {
title: $('title').text().replace(/----/, ' - '),
link: currentUrl,

View File

@ -1,28 +1,18 @@
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const { finishArticleItem } = require('@/utils/wechat-mp');
const config = require('@/config').value;
const baseUrl = 'https://www.nmpa.gov.cn';
const got = require('@/utils/got');
module.exports = async (ctx) => {
const path = ctx.params[0];
const url = `${baseUrl}/${path.endsWith('/') ? path.slice(0, -1) : path}/index.html`;
const browser = await require('@/utils/puppeteer')({ stealth: true });
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 { data: html } = await got(url);
const $ = cheerio.load(html);
return {
@ -48,20 +38,10 @@ module.exports = async (ctx) => {
data.items.map((item) => {
if (/^https:\/\/www\.nmpa\.gov\.cn\//.test(item.link)) {
return 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');
const html = await page.evaluate(() => document.documentElement.innerHTML);
await page.close();
const { data: html } = await got(item.link);
const $ = cheerio.load(html);
item.description = $('.text').html();
item.pubDate = timezone(parseDate($('meta[name="PubDate"]').attr('content')), +8);
return item;
});
} else if (/^https:\/\/mp\.weixin\.qq\.com\//.test(item.link)) {
@ -72,8 +52,6 @@ module.exports = async (ctx) => {
})
);
await browser.close();
ctx.state.data = {
title: data.title,
description: data.description,