diff --git a/docs/forecast.md b/docs/forecast.md index 9d87c667c..cea9fb737 100644 --- a/docs/forecast.md +++ b/docs/forecast.md @@ -98,7 +98,7 @@ pageClass: routes ### 全国气象预警 - + 可通过全局过滤参数订阅您感兴趣的地区. diff --git a/lib/router.js b/lib/router.js index bf2a43620..a415d610b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -528,7 +528,7 @@ router.get('/novel/ptwxz/:id1/:id2', require('./routes/novel/ptwxz')); router.get('/novel/zhaishuyuan/:id', require('./routes/novel/zhaishuyuan')); // 中国气象网 -router.get('/weatheralarm', require('./routes/weatheralarm')); +router.get('/weatheralarm/:province?', require('./routes/weatheralarm')); // Gitlab router.get('/gitlab/explore/:type', require('./routes/gitlab/explore')); diff --git a/lib/routes/weatheralarm/index.js b/lib/routes/weatheralarm/index.js index f41b9815f..4537ef7f9 100644 --- a/lib/routes/weatheralarm/index.js +++ b/lib/routes/weatheralarm/index.js @@ -1,23 +1,25 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const date = require('@/utils/date'); module.exports = async (ctx) => { - const alarmInfoURL = 'http://www.nmc.cn/f/alarm.html'; - const html = (await got.get(alarmInfoURL)).data; - const $ = cheerio.load(html); - const alarmElements = $('.alarmlist > div:not(.pagination)').toArray(); + const province = encodeURIComponent(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: alarmInfoURL, - item: alarmElements.map((el) => { - const $el = $(el); - const $aEl = $el.find('a'); + link: `http://www.nmc.cn/publish/alarm.html`, + item: list.map((item) => { + const title = item.title; + const url = item.url; return { - title: $aEl.text(), - link: `http://www.nmc.cn${$aEl.attr('href')}`, - pubDate: date($el.find('.date').text(), 8), + title: `${title}`, + link: `http://www.nmc.cn${url}`, + pubDate: `${item.issuetime}`, }; }), };