feat(route): 生态环境部-要闻动态 (#9248)

* feat(route): 生态环境部-要闻动态
This commit is contained in:
liuxsdev
2022-03-08 21:49:30 +08:00
committed by GitHub
parent f5960cc285
commit 007948efcd
7 changed files with 99 additions and 59 deletions

View File

@@ -952,9 +952,13 @@ pageClass: routes
## 中华人民共和国生态环境部 ## 中华人民共和国生态环境部
### 公示 ### 要闻动态
<Route author="billyct" example="/gov/mee/gs" path="/gov/mee/gs"/> <Route author="liuxsdev" example="/gov/mee/ywdt/hjywnews" path="/gov/mee/ywdt/:category?" :paramsDesc="['分类名,预设 `szyw`']"/>
| 时政要闻 | 环境要闻 | 地方快讯 | 新闻发布 | 视频新闻 | 公示公告 |
| :--: | :------: | :----: | :--: | :--: | :--: |
| szyw | hjywnews | dfnews | xwfb | spxw | gsgg |
## 中华人民共和国退役军人事务部 ## 中华人民共和国退役军人事务部

View File

@@ -1262,9 +1262,6 @@ router.get('/gov/ndrc/xwdt/:caty?', lazyloadRouteHandler('./routes/gov/ndrc/xwdt
// 中华人民共和国-海关总署 // 中华人民共和国-海关总署
router.get('/gov/customs/list/:gchannel', lazyloadRouteHandler('./routes/gov/customs/list')); router.get('/gov/customs/list/:gchannel', lazyloadRouteHandler('./routes/gov/customs/list'));
// 中华人民共和国生态环境部
router.get('/gov/mee/gs', lazyloadRouteHandler('./routes/gov/mee/gs'));
// 中华人民共和国教育部 // 中华人民共和国教育部
router.get('/gov/moe/:type', lazyloadRouteHandler('./routes/gov/moe/moe')); router.get('/gov/moe/:type', lazyloadRouteHandler('./routes/gov/moe/moe'));

View File

@@ -1,54 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const linkBase = `http://www.mee.gov.cn/xxgk/gs/gsq/`;
const listData = await got.get(linkBase);
const $ = cheerio.load(listData.data);
ctx.state.data = {
title: `公示 - 中华人民共和国生态环境部`,
link: linkBase,
item: await Promise.all(
$('.main .main_top li')
.map(async (_, el) => {
const $el = $(el);
const $a = $el.find('>a');
const href = $a.attr('href');
const key = `gov_gs: ${href}`;
let description;
const value = await ctx.cache.get(key);
// 移除 href 中的 ./,并且拼接原来的 url
const link = `${linkBase}${href.slice(2)}`;
if (value) {
description = value;
} else {
const contentData = await got.get(link);
const $content = cheerio.load(contentData.data);
description = $content('.TRS_Editor').html();
ctx.cache.set(key, description);
}
const title = $a.text();
// 获取 date
const pubDate = new Date(
$el
.find('.shover')
.first()
.text()
.match(/\d{4}-\d{2}-\d{2}/)
).toUTCString();
return {
title,
description,
link,
pubDate,
};
})
.get()
),
};
};

View File

@@ -12,4 +12,5 @@ module.exports = {
'/miit/zcwj': ['Yoge-Code'], '/miit/zcwj': ['Yoge-Code'],
'/miit/wjgs': ['Yoge-Code'], '/miit/wjgs': ['Yoge-Code'],
'/miit/zcjd': ['Yoge-Code'], '/miit/zcjd': ['Yoge-Code'],
'/mee/ywdt?': ['liuxsdev'],
}; };

80
lib/v2/gov/mee/ywdt.js Normal file
View File

@@ -0,0 +1,80 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const baseUrl = 'https://www.mee.gov.cn/';
const columns = {
szyw: { name: '时政要闻', order: 1 },
hjywnews: { name: '环境要闻', order: 2 },
dfnews: { name: '地方快讯', order: 3 },
xwfb: { name: '新闻发布', order: 4 },
spxw: { name: '视频新闻', order: 5 },
gsgg: { name: '公示公告', order: 6 },
};
module.exports = async (ctx) => {
const cate = ctx.params.category ?? 'szyw';
const url = `${baseUrl}ywdt/`;
const title = `${columns[cate].name} - 要闻动态 - 中华人民共和国生态环境部`;
const response = await got(url);
const $ = cheerio.load(response.data);
const all = $('.bd');
const list = all
.find(`div:nth-child(${columns[cate].order})`)
.find('.mobile_none li , .mobile_clear li')
.map((_, item) => {
const title = $(item).find('a.cjcx_biaob').text().trim();
const href = $(item).find('a').attr('href');
let absolute_path;
if (href.search('\\./') === 0) {
absolute_path = `${url}${href.slice(2)}`;
} else if (href.search('\\./') === 1) {
absolute_path = `${baseUrl}${href.slice(3)}`;
} else {
absolute_path = href;
}
const link = absolute_path;
return {
title,
link,
};
})
.get();
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link);
const content = cheerio.load(detailResponse.data);
try {
item.pubDate = timezone(parseDate(content('meta[name=PubDate]').attr('content')), +8);
// 视频新闻规则不一样
if (cate === 'spxw') {
item.title = content('meta[name=ArticleTitle]').attr('content');
// 取消视频自动播放
const video_control = content('.neiright_JPZ_GK_CP video');
video_control.removeAttr('autoplay');
// 视频路径转绝对路径
const video_source = content('.neiright_JPZ_GK_CP source');
const video_href = video_source.attr('src');
const _title_href = item.link.split('/').slice(-1)[0];
const _video_src = item.link.replace(_title_href, video_href.slice(2));
video_source.attr('src', _video_src);
}
item.description = content('.neiright_JPZ_GK_CP').html();
} catch (e) {
item.description = '';
}
return item;
})
)
);
ctx.state.data = {
title,
link: url,
item: items,
};
};

View File

@@ -393,4 +393,15 @@ module.exports = {
}, },
], ],
}, },
'mee.gov.cn': {
_name: '生态环境部',
www: [
{
title: '要闻动态',
docs: 'https://docs.rsshub.app/government.html#zhong-hua-ren-min-gong-he-guo-sheng-tai-huan-jing-bu',
source: ['/ywdt/:category'],
target: '/gov/mee/ywdt/:category',
},
],
},
}; };

View File

@@ -13,4 +13,5 @@ module.exports = function (router) {
router.get('/miit/zcwj', require('./miit/zcwj')); router.get('/miit/zcwj', require('./miit/zcwj'));
router.get('/miit/wjgs', require('./miit/wjgs')); router.get('/miit/wjgs', require('./miit/wjgs'));
router.get('/miit/zcjd', require('./miit/zcjd')); router.get('/miit/zcjd', require('./miit/zcjd'));
router.get('/mee/ywdt/:category?', require('./mee/ywdt'));
}; };