From c55867fa55ab18c517540965770dc9e5fc5d8e84 Mon Sep 17 00:00:00 2001 From: MisteryMonster <40703811+MisteryMonster@users.noreply.github.com> Date: Wed, 22 Apr 2020 20:30:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E8=B7=AF=E7=94=B1=20?= =?UTF-8?q?=E9=A2=84=E6=8A=A5=E9=A2=84=E8=AD=A6|=E5=85=A8=E5=9B=BD?= =?UTF-8?q?=E6=B0=94=E8=B1=A1=E9=A2=84=E8=AD=A6=20(#4511)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/forecast.md | 2 +- lib/router.js | 2 +- lib/routes/weatheralarm/index.js | 28 +++++++++++++++------------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/forecast.md b/docs/forecast.md index 9d87c667ce..cea9fb737d 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 bf2a436206..a415d610be 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 f41b9815f6..4537ef7f91 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}`, }; }), };