From 0a29a7c4e872775880c267e2ba82e13caa42fc8c Mon Sep 17 00:00:00 2001 From: Rein-Ou <52846432+Rein-Ou@users.noreply.github.com> Date: Tue, 29 Mar 2022 21:29:07 +0800 Subject: [PATCH] feat(route) add qweather/now (#9413) * qweather_now * now * pubDate fix * timezone * parseDate --- docs/forecast.md | 9 +++- lib/v2/qweather/{index.js => 3days.js} | 2 +- lib/v2/qweather/maintainer.js | 3 +- lib/v2/qweather/now.js | 41 +++++++++++++++++++ lib/v2/qweather/router.js | 3 +- .../qweather/util/{weather.art => 3days.art} | 0 lib/v2/qweather/util/now.art | 16 ++++++++ 7 files changed, 70 insertions(+), 4 deletions(-) rename lib/v2/qweather/{index.js => 3days.js} (95%) create mode 100644 lib/v2/qweather/now.js rename lib/v2/qweather/util/{weather.art => 3days.art} (100%) create mode 100644 lib/v2/qweather/util/now.art diff --git a/docs/forecast.md b/docs/forecast.md index ea24876da99677..b00d86a764ad04 100644 --- a/docs/forecast.md +++ b/docs/forecast.md @@ -62,12 +62,19 @@ pageClass: routes ### 近三天天气 - + 需自行注册获取 api 的 key,并在环境变量 HEFENG_KEY 中进行配置,获取订阅近三天天气预报 +### 实时天气 + + +需自行注册获取api的key,每小时更新一次数据 + + + ## 上海市生态环境局 ### 空气质量 diff --git a/lib/v2/qweather/index.js b/lib/v2/qweather/3days.js similarity index 95% rename from lib/v2/qweather/index.js rename to lib/v2/qweather/3days.js index 55bcb435697167..544cc9f3a1b567 100644 --- a/lib/v2/qweather/index.js +++ b/lib/v2/qweather/3days.js @@ -29,7 +29,7 @@ module.exports = async (ctx) => { } const items = data.map((item) => ({ title: '预报日期:' + item.fxDate, - description: art(path.join(__dirname, './util/weather.art'), { + description: art(path.join(__dirname, './util/3days.art'), { item, }), pubDate: responseData.updateTime, diff --git a/lib/v2/qweather/maintainer.js b/lib/v2/qweather/maintainer.js index 1455db8f8d0062..907904212644f0 100644 --- a/lib/v2/qweather/maintainer.js +++ b/lib/v2/qweather/maintainer.js @@ -1,3 +1,4 @@ module.exports = { - '/weather/:location': ['Rein-Ou'], + '/qweather/3days/:location': ['Rein-Ou'], + '/qweather/now/:location': ['Rein-Ou'], }; diff --git a/lib/v2/qweather/now.js b/lib/v2/qweather/now.js new file mode 100644 index 00000000000000..b0982d6052c8c7 --- /dev/null +++ b/lib/v2/qweather/now.js @@ -0,0 +1,41 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); +const { parseDate } = require('@/utils/parse-date'); +const config = require('@/config').value; +const rootUrl = 'https://devapi.qweather.com/v7/weather/now?'; +module.exports = async (ctx) => { + const id = await ctx.cache.tryGet(ctx.params.location + '_id', async () => { + const response = await got(`https://geoapi.qweather.com/v2/city/lookup?location=${ctx.params.location}&key=${config.hefeng.key}`); + const data = []; + for (const i in response.data.location) { + data.push(response.data.location[i]); + } + return data[0].id; + }); + const requestUrl = rootUrl + 'key=' + config.hefeng.key + '&location=' + id; + const responseData = await ctx.cache.tryGet( + ctx.params.location, + async () => { + const response = await got(requestUrl); + return response.data; + }, + 3600, // second + false + ); + + const data = [responseData.now]; + + ctx.state.data = { + title: ctx.params.location + '实时天气', + description: ctx.params.location + '实时天气状况', + item: data.map((item) => ({ + title: '观测时间:' + parseDate(responseData.updateTime), + description: art(path.join(__dirname, './util/now.art'), { item }), + pubDate: parseDate(responseData.updateTime), + guid: '位置:' + ctx.params.location + '--时间:' + parseDate(responseData.updateTime), + link: item.fxLink, + })), + link: responseData.fxLink, + }; +}; diff --git a/lib/v2/qweather/router.js b/lib/v2/qweather/router.js index c08018c1d94b9d..63c591437cbccb 100644 --- a/lib/v2/qweather/router.js +++ b/lib/v2/qweather/router.js @@ -1,3 +1,4 @@ module.exports = function (router) { - router.get('/:location', require('./index')); + router.get('/3days/:location', require('./3days')); + router.get('/now/:location', require('./now')); }; diff --git a/lib/v2/qweather/util/weather.art b/lib/v2/qweather/util/3days.art similarity index 100% rename from lib/v2/qweather/util/weather.art rename to lib/v2/qweather/util/3days.art diff --git a/lib/v2/qweather/util/now.art b/lib/v2/qweather/util/now.art new file mode 100644 index 00000000000000..a32ee4b3b019a1 --- /dev/null +++ b/lib/v2/qweather/util/now.art @@ -0,0 +1,16 @@ +天气:{{item.text}} +
+气温:{{item.temp}} +
+体感温度:{{item.feelsLike}} +
+风向:{{item.windDir}} +
+风力:{{item.windScale}} 风速:{{item.windSpeed}}公里 +
+湿度:{{item.humidity}}% 大气压强:{{item.pressure}}百帕 +
+本小时降水量:{{item.precip}} +
+能见度:{{item.vis}}公里 +
\ No newline at end of file