From e3ffd5d7c13ee94aec88b22fb6da75379f730f7f Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 28 Aug 2022 16:33:16 -0600 Subject: [PATCH] fix(route): mofcom (#10641) * fix(route): mofcom * fix: typo --- docs/government.md | 2 +- lib/router.js | 4 +-- lib/routes/mofcom/article.js | 54 ---------------------------- lib/v2/gov/maintainer.js | 1 + lib/v2/gov/mofcom/article.js | 68 ++++++++++++++++++++++++++++++++++++ lib/v2/gov/radar.js | 11 ++++++ lib/v2/gov/router.js | 1 + 7 files changed, 84 insertions(+), 57 deletions(-) delete mode 100644 lib/routes/mofcom/article.js create mode 100644 lib/v2/gov/mofcom/article.js diff --git a/docs/government.md b/docs/government.md index 5231109df7..6675a51899 100644 --- a/docs/government.md +++ b/docs/government.md @@ -1247,7 +1247,7 @@ pageClass: routes ### 政务公开 - + ## 中华人民共和国生态环境部 diff --git a/lib/router.js b/lib/router.js index ae3b97754c..1f8b037a37 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1945,8 +1945,8 @@ router.get('/dhl/:id', lazyloadRouteHandler('./routes/dhl/shipment-tracking')); // Japanpost router.get('/japanpost/track/:reqCode/:locale?', lazyloadRouteHandler('./routes/japanpost/track')); -// 中华人民共和国商务部 -router.get('/mofcom/article/:suffix', lazyloadRouteHandler('./routes/mofcom/article')); +// 中华人民共和国商务部 migrated to v2 +// router.get('/mofcom/article/:suffix', lazyloadRouteHandler('./routes/mofcom/article')); // 品玩 router.get('/pingwest/status', lazyloadRouteHandler('./routes/pingwest/status')); diff --git a/lib/routes/mofcom/article.js b/lib/routes/mofcom/article.js deleted file mode 100644 index a7cb76d1cc..0000000000 --- a/lib/routes/mofcom/article.js +++ /dev/null @@ -1,54 +0,0 @@ -const cheerio = require('cheerio'); -const got = require('@/utils/got'); -const { resolve } = require('url'); - -const host = 'http://www.mofcom.gov.cn'; - -module.exports = async (ctx) => { - const suffix = ctx.params.suffix; - const url = resolve('http://www.mofcom.gov.cn/article/', suffix); - const res = await got.get(url); - const $ = cheerio.load(res.data); - const title = $('head > title').text(); - const list = $('.txtList_01 li').get(); - - const out = await Promise.all( - list.map(async (item) => { - const $ = cheerio.load(item); - const title = $('a').attr('title'); - const sub_url = $('a').attr('href'); - const itemUrl = resolve(host, sub_url); - const cache = await ctx.cache.get(itemUrl); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - let responses = await got.get(itemUrl); - const redirect = responses.data.match(/window\.location\.href='(.*)'/); - if (redirect) { - responses = await got.get(redirect[1], { - headers: { - Referer: itemUrl, - 'Accept-Encoding': 'gzip, deflate', - 'Accept-Language': 'zh-CN,zh;q=0.9', - Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', - }, - }); - } - const $d = cheerio.load(responses.data); - - const single = { - title, - link: itemUrl, - description: $d('.art-con').html() || $d('.textlive').html(), - }; - ctx.cache.set(itemUrl, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - ctx.state.data = { - title, - link: url, - item: out, - }; -}; diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index edf9388ab5..3ec630e6ff 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -22,6 +22,7 @@ module.exports = { '/miit/wjgs': ['Yoge-Code'], '/miit/zcjd': ['Yoge-Code'], '/moe/:type': ['Crawler995'], + '/mofcom/article/:suffix+': ['LogicJake'], '/moj/aac/news/:type?': ['TonyRL'], '/nifdc/:path+': ['nczitzk'], '/nmpa/:path+': ['TonyRL'], diff --git a/lib/v2/gov/mofcom/article.js b/lib/v2/gov/mofcom/article.js new file mode 100644 index 0000000000..c5802bc84a --- /dev/null +++ b/lib/v2/gov/mofcom/article.js @@ -0,0 +1,68 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +const host = 'http://www.mofcom.gov.cn'; + +module.exports = async (ctx) => { + const suffix = ctx.params.suffix; + const url = `http://www.mofcom.gov.cn/article/${suffix}/`; + const { data: res } = await got(url); + const $ = cheerio.load(res); + $('.listline').remove(); + const list = $('.txtList_01 li') + .toArray() + .map((item) => { + item = $(item); + const date = + item.find('span').length !== 0 + ? item + .find('span') + .text() + .match(/((\d{4}-\d{2}-\d{2})(\s\d{2}:\d{2}:\d{2})?)/)[1] + : item + .find('a') + .attr('title') + .match(/(\d{4}年\d{1,2}月\d{1,2})/)[1]; + return { + title: item.find('a').attr('title'), + link: host + item.find('a').attr('href'), + pubDate: timezone(parseDate(date, ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD', 'YYYY年M月D']), 8), + }; + }); + + await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + let responses = await got(item.link); + // xwfb/xwlxfbh || xwfb/xwztfbh + const redirect = responses.data.match(/_cofing1=\{href:"(.*)",type/) || responses.data.match(/window\.location\.href='(.*)'/); + if (redirect) { + responses = await got(redirect[1], { + headers: { + Referer: item.link, + }, + }); + } + const $ = cheerio.load(responses.data); + const iframe = $('.art-con iframe').attr('src'); + if ($('.art-con iframe').attr('src')) { + const { data: res } = await got(iframe); + item.description = cheerio.load(res)('tbody').html(); + } else { + item.description = $('.art-con').html() || /* xwfb/xwztfbh */ $('.textlive').html(); + } + item.pubDate = $('meta[name="PubDate"]').length ? timezone(parseDate($('meta[name="PubDate"]').attr('content'), 'YYYY-MM-DD HH:mm'), 8) : item.pubDate; + + return item; + }) + ) + ); + ctx.state.data = { + title: $('head > title').text(), + description: $('meta[name=description]').attr('content'), + link: url, + item: list, + }; +}; diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index 4fde967f95..5a2f9b3437 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -526,6 +526,17 @@ module.exports = { }, ], }, + 'mofcom.gov.cn': { + _name: '中华人民共和国商务部', + '.': [ + { + title: '新闻发布', + docs: 'https://docs.rsshub.app/government.html#zhong-hua-ren-min-gong-he-guo-shang-wu-bu', + source: ['/article/*'], + target: (_, url) => `/gov/mofcom/article/${new URL(url).pathname.replace('/article/', '')}`, + }, + ], + }, 'moj.gov.tw': { _name: '台灣法務部廉政署', 'www.aac': [ diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index 61483fb1f3..12395f547d 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -14,6 +14,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('/mofcom/article/:suffix+', require('./mofcom/article')); router.get('/moj/aac/news/:type?', require('./moj/aac/news')); router.get(/nifdc\/([\w/-]+)?/, require('./nifdc')); router.get(/\/nmpa\/([\w][\w/]+)?/, require('./nmpa/generic'));