From e6770d35afe955d5ee860c0a3298a87415d4fbb5 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 30 Nov 2023 00:59:49 +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=A0=87=E5=87=86=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=20(#13919)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 国家药品监督管理局医疗器械标准管理中心 * fix: radar path matching --------- --- lib/v2/gov/maintainer.js | 2 +- lib/v2/gov/nifdc/index.js | 73 ++++++++++++++++-------------- lib/v2/gov/radar.js | 4 +- lib/v2/gov/router.js | 2 +- website/docs/routes/government.mdx | 2 +- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index bd054bac081cf4..03abde8e698071 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -37,7 +37,7 @@ module.exports = { '/moj/aac/news/:type?': ['TonyRL'], '/mot/:category?': ['nczitzk'], '/news/:uid': ['EsuRt'], - '/nifdc/:path+': ['nczitzk'], + '/nifdc/:path?': ['nczitzk'], '/nmpa/:path+': ['TonyRL'], '/nopss/:path+': ['nczitzk'], '/npc/:caty': ['233yeee'], diff --git a/lib/v2/gov/nifdc/index.js b/lib/v2/gov/nifdc/index.js index 2a266c3f0f55cd..32a3defa38fd80 100644 --- a/lib/v2/gov/nifdc/index.js +++ b/lib/v2/gov/nifdc/index.js @@ -4,58 +4,55 @@ 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 { path = 'bshff/ylqxbzhgl/qxggtzh' } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; const rootUrl = 'https://www.nifdc.org.cn'; - const currentUrl = `${rootUrl}${params}/index.html`; + const currentUrl = new URL(`nifdc/${path}/`, rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const { data: response } = await got(currentUrl); - const $ = cheerio.load(response.data); + const $ = cheerio.load(response); - let items = $('.list ul li a') - .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 20) + let items = $('div.list ul li') + .slice(0, limit) .toArray() .map((item) => { item = $(item); - const link = item.attr('href'); + const a = item.find('a'); + const link = a.prop('href'); return { - title: item.text(), - link: /^http/.test(link) ? link : new URL(link, currentUrl).href, - pubDate: parseDate( - item - .next() - .text() - .match(/\((.*)\)/) - ), + title: a.prop('title') || a.text(), + link: link.startsWith('http') ? link : new URL(link, currentUrl).href, + pubDate: parseDate(item.find('span').text().replace(/\(|\)/g, '')), }; }); items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.link, async () => { - if (/^https:\/\/www\.nmpa\.gov\.cn\//.test(item.link)) { - const { data: html } = await got(item.link); + try { + const { data: detailResponse } = await got(item.link); - const content = cheerio.load(html); + const content = cheerio.load(detailResponse); - item.description = content('.text').html(); - item.pubDate = timezone(parseDate($('meta[name="PubDate"]').attr('content')), +8); - } else { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + item.title = content('.title').text(); + item.description = content('div.text').append(content('div.fujian')).html(); + item.author = content('meta[name="ContentSource"]').prop('content'); + item.category = [ + ...new Set([content('meta[name="ColumnName"]').prop('content'), content('meta[name="ColumnType"]').prop('content'), ...(content('meta[name="ColumnKeywords"]').prop('content').split(/,|;/) ?? [])]), + ].filter((c) => c); + item.pubDate = timezone(parseDate(content('meta[name="PubDate"]').prop('content')), +8); + item.enclosure_url = content('a.fujianClass').first().prop('href'); - const content = cheerio.load(detailResponse.data); - - item.description = content('.text').append(content('.fujian')).html(); - item.pubDate = timezone(parseDate(content('meta[name="PubDate"]').attr('content')), +8); + if (item.enclosure_url) { + item.enclosure_url = new URL(item.enclosure_url, rootUrl).href; + item.enclosure_type = `application/${item.enclosure_url.split(/\./).pop()}`; + } + } catch (e) { + // no-empty } return item; @@ -63,9 +60,19 @@ module.exports = async (ctx) => { ) ); + const image = new URL($('div.logo img').prop('src'), currentUrl).href; + const icon = new URL($('link[rel="shortcut icon"]').prop('href'), currentUrl).href; + ctx.state.data = { + item: items, title: $('title').text().replace(/----/, ' - '), link: currentUrl, - item: items, + description: $('meta[name="ColumnDescription"]').prop('content'), + language: 'zh', + image, + icon, + logo: icon, + subtitle: $('meta[ name="ColumnName"]').prop('content'), + author: $('meta[name="SiteName"]').prop('content'), }; }; diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index b0f0b432be727d..f1d7eb875b57b2 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -1054,8 +1054,8 @@ module.exports = { { title: '通用', docs: 'https://docs.rsshub.app/routes/government#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', '')}`, + source: ['/*'], + target: (params) => `/gov/nifdc/${params.path.replace(/\/index\.html$/, '')}`, }, ], }, diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index 13d7b3fcf7a71d..038443f0ac7c6d 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -30,7 +30,7 @@ module.exports = function (router) { router.get('/mofcom/article/:suffix+', require('./mofcom/article')); router.get('/moj/aac/news/:type?', require('./moj/aac/news')); router.get('/news/:uid', require('./news')); - router.get(/nifdc\/([\w/-]+)?/, require('./nifdc')); + router.get('/nifdc/:path*', require('./nifdc')); router.get(/\/nmpa\/([\w][\w/]+)?/, require('./nmpa/generic')); router.get(/nopss(\/[\w/-]+)?/, require('./nopss')); router.get('/npc/:caty', require('./npc/index')); diff --git a/website/docs/routes/government.mdx b/website/docs/routes/government.mdx index be15aeb3346593..7c7703fffb4360 100644 --- a/website/docs/routes/government.mdx +++ b/website/docs/routes/government.mdx @@ -669,7 +669,7 @@ Language ### 通用 {#guo-jia-yao-pin-jian-du-guan-li-ju-yi-liao-qi-xie-biao-zhun-guan-li-zhong-xin-tong-yong} - + :::tip