Skip to content

Commit

Permalink
feat(route) add qweather/now (#9413)
Browse files Browse the repository at this point in the history
* qweather_now

* now

* pubDate fix

* timezone

* parseDate
  • Loading branch information
Rein-Ou authored Mar 29, 2022
1 parent 95db3b4 commit 0a29a7c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
9 changes: 8 additions & 1 deletion docs/forecast.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ pageClass: routes

### 近三天天气

<Route author="Rein-Ou" example="/qweather/广州" path="/qweather/:location" selfhost="1">
<Route author="Rein-Ou" example="/qweather/3days/广州" path="/qweather/3days/:location" selfhost="1">

需自行注册获取 api 的 key,并在环境变量 HEFENG_KEY 中进行配置,获取订阅近三天天气预报

</Route>

### 实时天气
<Route author="Rein-Ou" example="/qweather/广州" path="/qweather/now/:location" selfhost="1">

需自行注册获取api的key,每小时更新一次数据

</Route>

## 上海市生态环境局

### 空气质量
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/qweather/index.js → lib/v2/qweather/3days.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/v2/qweather/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
'/weather/:location': ['Rein-Ou'],
'/qweather/3days/:location': ['Rein-Ou'],
'/qweather/now/:location': ['Rein-Ou'],
};
41 changes: 41 additions & 0 deletions lib/v2/qweather/now.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
3 changes: 2 additions & 1 deletion lib/v2/qweather/router.js
Original file line number Diff line number Diff line change
@@ -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'));
};
File renamed without changes.
16 changes: 16 additions & 0 deletions lib/v2/qweather/util/now.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<text>天气:{{item.text}}</text>
<br>
<text>气温:{{item.temp}}</text>
<br>
<text>体感温度:{{item.feelsLike}}</text>
<br>
<text>风向:{{item.windDir}}</text>
<br>
<text>风力:{{item.windScale}} 风速:{{item.windSpeed}}公里</text>
<br>
<text>湿度:{{item.humidity}}% 大气压强:{{item.pressure}}百帕</text>
<br>
<text>本小时降水量:{{item.precip}}</text>
<br>
<text>能见度:{{item.vis}}公里</text>
<br>

0 comments on commit 0a29a7c

Please sign in to comment.