From c93e63559a6ca05e93845c74c0ff7944591fab21 Mon Sep 17 00:00:00 2001 From: drgnchan <40224023+drgnchan@users.noreply.github.com> Date: Mon, 8 May 2023 02:35:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=96=B0=E5=A2=9E=E7=BD=91?= =?UTF-8?q?=E4=BF=A1=E5=8A=9E=E8=AE=A2=E9=98=85=20(#12444)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 新增网信办订阅 * refactor: 时区设置 * polish * polish * refactor: cache path map --- docs/government.md | 17 +++++++++++ lib/v2/gov/cac/index.js | 64 ++++++++++++++++++++++++++++++++++++++++ lib/v2/gov/maintainer.js | 1 + lib/v2/gov/radar.js | 11 +++++++ lib/v2/gov/router.js | 1 + 5 files changed, 94 insertions(+) create mode 100644 lib/v2/gov/cac/index.js diff --git a/docs/government.md b/docs/government.md index 08ebe3ae6..ad419b470 100644 --- a/docs/government.md +++ b/docs/government.md @@ -1634,3 +1634,20 @@ pageClass: routes ::: + +## 中央网信办 + +### 分类 + + + +::: tip 提示 + +路径填写对应页面 URL 中间部分。例如: + +首页 > 权威发布 > 办公室发布: +此时,path 参数为:/qwfb/bgsfb + +::: + + diff --git a/lib/v2/gov/cac/index.js b/lib/v2/gov/cac/index.js new file mode 100644 index 000000000..e7cbc1ffd --- /dev/null +++ b/lib/v2/gov/cac/index.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); +const config = require('@/config').value; + +module.exports = async (ctx) => { + const path = ctx.params[0]; + const host = 'http://www.cac.gov.cn'; + const homepage = `${host}/index.htm`; + // 在首页查找出所有的目录完整路径,比如http://www.cac.gov.cn/xxh/A0906index_1.htm + // xxh --> {"path": "/xxh", "completeUrl": "http://www.cac.gov.cn/xxh/A0906index_1.htm"} + const pathList = await ctx.cache.tryGet( + 'gov:cac:pathList', + async () => { + const { data: homepageResponse } = await got(homepage); + const $ = cheerio.load(homepageResponse); + return $('a') + .toArray() + .map((item) => { + const href = $(item).attr('href'); + if (href && href.startsWith('http://www.cac.gov.cn/')) { + const matchArray = href.match(/http:\/\/www\.cac\.gov\.cn(.*?)\/(A.*?\.htm)/); + if (matchArray && matchArray.length > 2) { + const path = matchArray[1]; + const htmlName = matchArray[2]; + return { + path, + completeUrl: `${host}${path}/${htmlName}`, + }; + } + } + return null; + }) + .filter((item) => item); + }, + config.cache.routeExpire, + false, + ); + + const completeUrl = pathList.find((item) => item && item.path === path).completeUrl; + const { data: channelResponse } = await got(completeUrl); + const $1 = cheerio.load(channelResponse); + const items = $1('li.clearfix') + .toArray() + .map((item) => { + const c = $1(item); + const a = c.find('a'); + const articleHref = a.attr('href'); + const title = a.text(); + const date = parseDate(c.find('span.times').text()); + return { + link: articleHref, + pubDate: timezone(date), + title, + description: title, + }; + }); + ctx.state.data = { + title: $1('head title').text(), + link: completeUrl, + item: items, + }; +}; diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index 7430e6c34..95610711b 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -1,5 +1,6 @@ module.exports = { // ministry + '/cac/:path+': ['drgnchan'], '/ccdi/:path+': ['bigfei'], '/cmse/xwzx/zhxw': ['nczitzk'], '/cmse/xwzx/yzjz': ['nczitzk'], diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index d1355eee1..535cd8dbc 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -308,6 +308,17 @@ module.exports = { }, ], }, + 'cac.gov.cn': { + _name: '中央网信办', + '.': [ + { + title: '分类', + docs: 'https://docs.rsshub.app/government.html#zhong-yang-wang-xin-ban', + source: ['/*'], + target: (params, url) => `/gov/cac/${new URL(url).href.match(/cac\.gov\.cn(.*?)\/(A.*?\.htm)/)[1]}`, + }, + ], + }, 'ccdi.gov.cn': { _name: '中央纪委国家监委', www: [ diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index 94140dbe0..a82b03e22 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -1,5 +1,6 @@ module.exports = function (router) { // ministry + router.get(/cac(\/[\w/-]+)?/, require('./cac/index')); router.get(/ccdi(\/[\w/-]+)?/, require('./ccdi')); router.get('/cmse/fxrw', require('./cmse/fxrw')); router.get(/cmse(\/[\w/-]+)?/, require('./cmse'));