From 54aea1e4e79301605850de9cd6c5fc74bff19f2e Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 16 Jul 2022 20:47:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=9B=BD=E5=AE=B6?= =?UTF-8?q?=E8=8D=AF=E5=93=81=E7=9B=91=E7=9D=A3=E7=AE=A1=E7=90=86=E5=B1=80?= =?UTF-8?q?=E5=8C=BB=E7=96=97=E5=99=A8=E6=A2=B0=E6=A0=87=E5=87=86=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=B8=AD=E5=BF=83=20(#10206)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 国家药品监督管理局医疗器械标准管理中心 * fix: shorten the route * fix: bad urls * fix: only 1 puppeteer request --- docs/government.md | 16 ++++++++ lib/v2/gov/maintainer.js | 1 + lib/v2/gov/nifdc/index.js | 86 +++++++++++++++++++++++++++++++++++++++ lib/v2/gov/radar.js | 11 +++++ lib/v2/gov/router.js | 1 + 5 files changed, 115 insertions(+) create mode 100644 lib/v2/gov/nifdc/index.js diff --git a/docs/government.md b/docs/government.md index 9cb0ecc5d5..5aaba0ddef 100644 --- a/docs/government.md +++ b/docs/government.md @@ -231,6 +231,22 @@ pageClass: routes +## 国家药品监督管理局医疗器械标准管理中心 + +### 通用 + + + +::: tip 提示 + +路径处填写对应页面 URL 中 `https://www.nifdc.gov.cn/nifdc/` 与 `/index.html` 之间的字段,下面是一个例子。 + +若订阅 [公告通告](https://www.nifdc.org.cn/nifdc/bshff/ylqxbzhgl/qxggtzh/index.html) 则将对应页面 URL 中 `https://www.nifdc.org.cn/nifdc/` 和 `/index.html` 之间的字段 `bshff/ylqxbzhgl/qxggtzh` 作为路径填入。此时路由为 [`/gov/nifdc/bshff/ylqxbzhgl/qxggtzh`](https://rsshub.app/gov/nifdc/bshff/ylqxbzhgl/qxggtzh) + +::: + + + ## 国家药品审评网站 ### 首页 diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index 0c73631f9f..942cb3bbf2 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -12,6 +12,7 @@ module.exports = { '/miit/zcjd': ['Yoge-Code'], '/moe/:type': ['Crawler995'], '/moj/aac/news/:type?': ['TonyRL'], + '/nifdc/:path+': ['nczitzk'], '/nmpa/:path+': ['TonyRL'], '/nrta/news/:category?': ['yuxinliu-alex'], '/nsfc/news/:type?': ['Derekmini', 'nczitzk'], diff --git a/lib/v2/gov/nifdc/index.js b/lib/v2/gov/nifdc/index.js new file mode 100644 index 0000000000..87dd041635 --- /dev/null +++ b/lib/v2/gov/nifdc/index.js @@ -0,0 +1,86 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const params = ctx.path === '/nifdc' ? '/nifdc/bshff/ylqxbzhgl/qxggtzh' : ctx.path; + + const rootUrl = 'https://www.nifdc.org.cn'; + const currentUrl = `${rootUrl}${params}/index.html`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.list ul li a') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 20) + .toArray() + .map((item) => { + item = $(item); + + const link = item.attr('href'); + + return { + title: item.text(), + link: /^http/.test(link) ? link : new URL(link, currentUrl).href, + pubDate: parseDate( + item + .next() + .text() + .match(/\((.*)\)/) + ), + }; + }); + + 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 content = cheerio.load(html); + + item.description = content('.text').html(); + } else { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.description = content('.text').html(); + item.pubDate = timezone(parseDate(content('meta[name="PubDate"]').attr('content')), +8); + } + + return item; + }) + ) + ); + + await browser.close(); + + ctx.state.data = { + title: $('title').text().replace(/----/, ' - '), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index cd4820b63a..1a6e826317 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -461,6 +461,17 @@ module.exports = { }, ], }, + 'nifdc.gov.cn': { + _name: '国家药品监督管理局医疗器械标准管理中心', + '.': [ + { + title: '通用', + docs: 'https://docs.rsshub.app/government.html#guo-jia-yao-pin-jian-du-guan-li-ju-yi-liao-qi-xie-biao-zhun-guan-li-zhong-xin', + source: ['/*path'], + target: (params) => `/gov/nifdc/${params.path.replace('/index.html', '')}`, + }, + ], + }, 'nmpa.gov.cn': { _name: '国家药品监督管理局', '.': [ diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index e9937b23ba..81955c07ae 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -12,6 +12,7 @@ module.exports = function (router) { router.get('/miit/zcjd', require('./miit/zcjd')); router.get('/moe/:type', require('./moe/moe')); router.get('/moj/aac/news/:type?', require('./moj/aac/news')); + router.get(/nifdc\/([\w/-]+)?/, require('./nifdc')); router.get(/\/nmpa\/([\w][\w/]+)?/, require('./nmpa/generic')); router.get('/nrta/news/:category?', require('./nrta/news')); router.get('/nsfc/news/:type?', require('./nsfc'));