Skip to content

Commit

Permalink
fix(route): migrate nmc weatheralarm (#14624)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Mar 2, 2024
1 parent 0ab8996 commit 94cb794
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
26 changes: 0 additions & 26 deletions lib/routes-deprecated/weatheralarm/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/routes/nmc/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/weatheralarm/:province?': ['ylc395'],
};
13 changes: 13 additions & 0 deletions lib/routes/nmc/radar.js
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions lib/routes/nmc/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (router) => {
router.get('/weatheralarm/:province?', './weatheralarm');
};
49 changes: 49 additions & 0 deletions lib/routes/nmc/weatheralarm.js
Original file line number Diff line number Diff line change
@@ -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,
});
};
2 changes: 1 addition & 1 deletion website/docs/routes/forecast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 94cb794

Please sign in to comment.