feat(route): nmpa (#10108)

* feat(route): nmpa

* docs: add puppeteer attr

* fix: close puppeteer page
This commit is contained in:
Tony
2022-07-03 19:21:51 +03:00
committed by GitHub
parent 5a3a4b2583
commit 7823d4d97e
5 changed files with 106 additions and 1 deletions
+16
View File
@@ -215,6 +215,22 @@ pageClass: routes
<Route author="y2361547758" example="/gov/nppa/318/45948" path="/gov/nppa/:channel/:content" :paramsDesc="['栏目名 id', '文章 id']" radar="1" rssbud="1"/>
## 国家药品监督管理局
### 通用
<Route author="TonyRL" example="/gov/nmpa/xxgk/ggtg" path="/gov/nmpa/:path+" :paramsDesc="['路径,默认为公告通告']" radar="1" rssbud="1" puppeteer="1">
::: tip 提示
路径处填写对应页面 URL 中 `https://www.nmpa.gov.cn/``/index.html` 之间的字段,下面是一个例子。
若订阅 [公告通告](https://www.nmpa.gov.cn/xxgk/ggtg/index.html) 则将对应页面 URL <https://www.nmpa.gov.cn/xxgk/ggtg/index.html> 中 `https://www.nmpa.gov.cn/``/index.html` 之间的字段 `xxgk/ggtg` 作为路径填入。此时路由为 [`/gov/nmpa/xxgk/ggtg`](https://rsshub.app/gov/nmpa/xxgk/ggtg)
:::
</Route>
## 国家药品审评网站
### 首页
+1
View File
@@ -11,6 +11,7 @@ module.exports = {
'/miit/wjgs': ['Yoge-Code'],
'/miit/zcjd': ['Yoge-Code'],
'/moe/:type': ['Crawler995'],
'/nmpa/:path+': ['TonyRL'],
'/nrta/news/:category?': ['yuxinliu-alex'],
'/pbc/goutongjiaoliu': ['nczitzk'],
'/pbc/gzlw': ['Fatpandac'],
+76
View File
@@ -0,0 +1,76 @@
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const config = require('@/config').value;
const baseUrl = 'https://www.nmpa.gov.cn';
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')();
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);
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').text().trim(),
link: new URL(item.find('a').attr('href'), baseUrl).href,
pubDate: parseDate(item.find('span').text(), 'YYYY-MM-DD'),
};
}),
};
},
config.cache.routeExpire,
false
);
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');
const html = await page.evaluate(() => document.documentElement.innerHTML);
await page.close();
const $ = cheerio.load(html);
item.description = $('.text').html();
return item;
})
)
);
await browser.close();
ctx.state.data = {
title: data.title,
description: data.description,
link: url,
item: items,
};
};
+12 -1
View File
@@ -455,12 +455,23 @@ module.exports = {
'.': [
{
title: '分类',
docs: 'https://docs.rsshub.app/government.html#nrta-gov',
docs: 'https://docs.rsshub.app/government.html#guo-jia-guang-bo-dian-shi-zong-ju',
source: ['/col/*category'],
target: (params) => `/gov/nrta/news/${params.category.replace('col', '').replace('/index.html', '')}`,
},
],
},
'nmpa.gov.cn': {
_name: '国家药品监督管理局',
'.': [
{
title: '通用',
docs: 'https://docs.rsshub.app/government.html#guo-jia-yao-pin-jian-du-guan-li-ju',
source: ['/*path'],
target: (params) => `/gov/nmpa/${params.path.replace('/index.html', '')}`,
},
],
},
'pbc.gov.cn': {
_name: '中国人民银行',
'.': [
+1
View File
@@ -11,6 +11,7 @@ module.exports = function (router) {
router.get('/miit/wjgs', require('./miit/wjgs'));
router.get('/miit/zcjd', require('./miit/zcjd'));
router.get('/moe/:type', require('./moe/moe'));
router.get(/\/nmpa\/([\w][\w/]+)?/, require('./nmpa/generic'));
router.get('/nrta/news/:category?', require('./nrta/news'));
router.get('/pbc/goutongjiaoliu', require('./pbc/goutongjiaoliu'));
router.get('/pbc/gzlw', require('./pbc/gzlw'));