From d424adf02161dc106ab78511afc73f6acf28bb3d Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 2 Oct 2023 18:17:25 -0400 Subject: [PATCH] fix(route): NPC URLs have been changed (#13449) * fix(route): NPC URLs have been changed * fix relative URLs * refactor: migrate to v2 --------- --- lib/router.js | 2 +- lib/routes/npc/index.js | 42 --------------------------- lib/v2/gov/maintainer.js | 1 + lib/v2/gov/npc/index.js | 47 +++++++++++++++++++++++++++++++ lib/v2/gov/radar.js | 11 ++++++++ lib/v2/gov/router.js | 1 + website/docs/routes/government.md | 4 ++- 7 files changed, 64 insertions(+), 44 deletions(-) delete mode 100644 lib/routes/npc/index.js create mode 100644 lib/v2/gov/npc/index.js diff --git a/lib/router.js b/lib/router.js index 82924afe5..3abc28d7c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2064,7 +2064,7 @@ router.get('/nobelprize/:caty?', lazyloadRouteHandler('./routes/nobelprize/index router.get('/gov/taiwan/mnd', lazyloadRouteHandler('./routes/gov/taiwan/mnd')); // 中国人大网 -router.get('/npc/:caty', lazyloadRouteHandler('./routes/npc/index')); +// router.get('/npc/:caty', lazyloadRouteHandler('./routes/npc/index')); // 高科技行业门户 router.get('/ofweek/news', lazyloadRouteHandler('./routes/ofweek/news')); diff --git a/lib/routes/npc/index.js b/lib/routes/npc/index.js deleted file mode 100644 index 220ee6a3a..000000000 --- a/lib/routes/npc/index.js +++ /dev/null @@ -1,42 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const { caty } = ctx.params; - // 主页 - const response = await got(`http://www.npc.gov.cn/npc/${caty}/list.shtml`); - const data = response.body; - const $ = cheerio.load(data, { decodeEntities: false }); - const title = $('title').text(); - // 获取每条的链接 - const links = $('.clist a') - .map((index, item) => [$(item).attr('href')]) - .get(); - // 获取标题、日期、内容 - const list = await Promise.all( - links.map(async (link) => { - const response = await got(`http://www.npc.gov.cn${link}`); - const data = response.body; - const $ = cheerio.load(data, { decodeEntities: false }); - const title = $('title').text().replace('_中国人大网', ''); - const time = $('span.fr').text(); - const description = $('#Zoom p') - .map((index, item) => $.html(item)) - .get() - .join(''); - return Promise.resolve([title, link, time, description]); - }) - ); - // 整合 - ctx.state.data = { - title, - link: `http://www.npc.gov.cn/npc/${caty}/list.shtml`, - description: title, - item: list.map((item) => ({ - title: item[0], - link: `http://www.npc.gov.cn${item[1]}`, - pubDate: new Date(Date.parse(item[2].replace(/[年月日]/g, '/'))).toUTCString(), - description: item[3], - })), - }; -}; diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index 01e32681a..0463128bd 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -35,6 +35,7 @@ module.exports = { '/nifdc/:path+': ['nczitzk'], '/nmpa/:path+': ['TonyRL'], '/nopss/:path+': ['nczitzk'], + '/npc/:caty': ['233yeee'], '/nrta/news/:category?': ['yuxinliu-alex'], '/nrta/dsj/:category?': ['nczitzk'], '/nsfc/news/:type?': ['Derekmini', 'nczitzk'], diff --git a/lib/v2/gov/npc/index.js b/lib/v2/gov/npc/index.js new file mode 100644 index 000000000..3271a450a --- /dev/null +++ b/lib/v2/gov/npc/index.js @@ -0,0 +1,47 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +module.exports = async (ctx) => { + const { caty } = ctx.params; + // 主页 + const baseurl = `http://www.npc.gov.cn/npc/c2/${caty}/`; + const response = await got(baseurl); + const data = response.data; + const $ = cheerio.load(data); + const title = $('title').text(); + // 获取每条的链接 + const links = $('.clist a') + .toArray() + .map((item) => new URL($(item).attr('href'), baseurl).href); + // 获取标题、日期、内容 + const items = await Promise.all( + links.map((link) => + ctx.cache.tryGet(link, async () => { + const response = await got(link); + const data = response.data; + const $ = cheerio.load(data); + const title = $('title').text().replace('_中国人大网', ''); + const time = $('script:contains("fbrq")') + .text() + .match(/fbrq = "(.*?)"/)[1]; + const description = $('#Zoom').html(); + + return { + title, + link, + description, + pubDate: timezone(parseDate(time, 'YYYY年MM月DD日 HH:mm'), +8), + }; + }) + ) + ); + // 整合 + ctx.state.data = { + title, + link: baseurl, + description: title, + item: items, + }; +}; diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index 43714f95b..6a7d62d06 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -836,6 +836,17 @@ module.exports = { }, ], }, + 'npc.gov.cn': { + _name: '中国人大网', + '.': [ + { + title: '通用', + docs: 'https://docs.rsshub.app/routes/government#zhong-guo-ren-da-wang', + source: ['/npc/c2/:caty'], + target: '/gov/npc/:caty', + }, + ], + }, 'nrta.gov.cn': { _name: '国家广播电视总局', '.': [ diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index 33b2e9ca9..46ceb8588 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -27,6 +27,7 @@ module.exports = function (router) { router.get(/nifdc\/([\w/-]+)?/, require('./nifdc')); router.get(/\/nmpa\/([\w][\w/]+)?/, require('./nmpa/generic')); router.get(/nopss(\/[\w/-]+)?/, require('./nopss')); + router.get('/npc/:caty', require('./npc/index')); router.get('/nrta/news/:category?', require('./nrta/news')); router.get('/nrta/dsj/:category?', require('./nrta/dsj')); router.get(/nsfc(\/[\w-/]+)?/, require('./nsfc')); diff --git a/website/docs/routes/government.md b/website/docs/routes/government.md index a3c7e2500..f740ffc06 100644 --- a/website/docs/routes/government.md +++ b/website/docs/routes/government.md @@ -1244,7 +1244,9 @@ Language ## 中国人大网 {#zhong-guo-ren-da-wang} - +### 通用 {#zhong-guo-ren-da-wang-tong-yong} + + | 立法 | 监督 | 代表 | 理论 | 权威发布 | 滚动新闻 | | ---- | ---- | ---- | ---- | -------- | -------- |