fix(route): migrate nmc weatheralarm (#14624)

This commit is contained in:
Tony 2024-03-02 22:41:47 +08:00 committed by GitHub
parent 0ab8996f40
commit 94cb794993
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 28 deletions

View File

@ -155,7 +155,7 @@ router.get('/mafengwo/ziyouxing/:code', lazyloadRouteHandler('./routes/mafengwo/
router.get('/novel/uukanshu/:uid', lazyloadRouteHandler('./routes/novel/uukanshu'));
// 中国气象网
router.get('/weatheralarm/:province?', lazyloadRouteHandler('./routes/weatheralarm'));
// router.get('/weatheralarm/:province?', lazyloadRouteHandler('./routes/weatheralarm'));
// Gitlab
router.get('/gitlab/explore/:type/:host?', lazyloadRouteHandler('./routes/gitlab/explore'));

View File

@ -1,26 +0,0 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const province = ctx.params.province || '&';
const alarmInfoURL = `http://www.nmc.cn/rest/findAlarm?pageNo=1&pageSize=20&signaltype=&signallevel=&province=${province}`;
const response = await got({
method: 'get',
url: alarmInfoURL,
});
const data = response.data.data;
const list = data.page.list;
ctx.state.data = {
title: '中央气象台全国气象预警',
link: `http://www.nmc.cn/publish/alarm.html`,
item: list.map((item) => {
const title = item.title;
const url = item.url;
return {
title,
link: `http://www.nmc.cn${url}`,
pubDate: item.issuetime,
};
}),
};
};

View File

@ -0,0 +1,3 @@
module.exports = {
'/weatheralarm/:province?': ['ylc395'],
};

13
lib/routes/nmc/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'nmc.cn': {
_name: '中央气象台',
'.': [
{
title: '全国气象预警',
docs: 'https://docs.rsshub.app/routes/forecast#zhong-yang-qi-xiang-tai',
source: ['/publish/alarm.html', '/'],
target: '/nmc/weatheralarm',
},
],
},
};

3
lib/routes/nmc/router.js Normal file
View File

@ -0,0 +1,3 @@
export default (router) => {
router.get('/weatheralarm/:province?', './weatheralarm');
};

View File

@ -0,0 +1,49 @@
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import * as cheerio from 'cheerio';
export default async (ctx) => {
const { province } = ctx.req.param();
const alarmInfoURL = `http://www.nmc.cn/rest/findAlarm`;
const { data: response } = await got(alarmInfoURL, {
searchParams: {
pageNo: 1,
pageSize: 20,
signaltype: '',
signallevel: '',
province,
},
});
const list = response.data.page.list.map((item) => ({
title: item.title,
link: `http://www.nmc.cn${item.url}`,
pubDate: timezone(parseDate(item.issuetime), 8),
}));
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
item.description =
$('#icon').html() +
$('#alarmtext')
.toArray()
.map((i) => $(i).html())
.join('');
return item;
})
)
);
ctx.set('data', {
title: '中央气象台全国气象预警',
link: 'http://www.nmc.cn/publish/alarm.html',
item: items,
});
};

View File

@ -316,6 +316,6 @@
### 全国气象预警 {#zhong-yang-qi-xiang-tai-quan-guo-qi-xiang-yu-jing}
<Route author="ylc395" example="/weatheralarm/广东省" path="/weatheralarm/:province?" paramsDesc={['省份']}>
<Route author="ylc395" example="/nmc/weatheralarm/广东省" path="/nmc/weatheralarm/:province?" paramsDesc={['省份']}>
可通过全局过滤参数订阅您感兴趣的地区.
</Route>