feat(route): 新增网信办订阅 (#12444)

* feat(route): 新增网信办订阅

* refactor: 时区设置

* polish

* polish

* refactor: cache path map
This commit is contained in:
drgnchan 2023-05-08 02:35:44 +08:00 committed by GitHub
parent 6083aa9a9a
commit c93e63559a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 0 deletions

View File

@ -1634,3 +1634,20 @@ pageClass: routes
:::
</Route>
## 中央网信办
### 分类
<Route author="drgnchan" example="/gov/cac/xxh" path="/gov/cac/:path+" :paramsDesc="['路径比如xxh表示信息化']" radar='1'>
::: tip 提示
路径填写对应页面 URL 中间部分。例如:
首页 > 权威发布 > 办公室发布: <http://www.cac.gov.cn/qwfb/bgsfb/A090302index_1.htm>
此时path 参数为:/qwfb/bgsfb
:::
</Route>

64
lib/v2/gov/cac/index.js Normal file
View File

@ -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,
};
};

View File

@ -1,5 +1,6 @@
module.exports = {
// ministry
'/cac/:path+': ['drgnchan'],
'/ccdi/:path+': ['bigfei'],
'/cmse/xwzx/zhxw': ['nczitzk'],
'/cmse/xwzx/yzjz': ['nczitzk'],

View File

@ -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: [

View File

@ -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'));