Skip to content

Commit

Permalink
feat(route): add Weather forcast (DIYgod#9382)
Browse files Browse the repository at this point in the history
* weather_forcast

* format

* cache

* Update lib/v2/weather/index.js

Co-authored-by: Tony <[email protected]>

* Update lib/v2/weather/util/location.js

Co-authored-by: Tony <[email protected]>

* Update docs/forecast.md

Co-authored-by: Tony <[email protected]>

* modify

* Update forecast.md

* readme.md

* docs: reorder `HEFENG_KEY`
  • Loading branch information
Rein-Ou authored and RikkaBlue committed Apr 9, 2022
1 parent 6e9c9e1 commit 3925f1b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/forecast.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ pageClass: routes
可通过全局过滤参数订阅您感兴趣的地区.

</Route>

## 和风天气

### 近三天天气

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

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

</Route>
4 changes: 4 additions & 0 deletions docs/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行
- `FANFOU_USERNAME`: 饭否登录用户名、邮箱、手机号
- `FANFOU_PASSWORD`: 饭否密码

- 和风天气:[申请地址](https://id.qweather.com/#/register?redirect=https%3A%2F%2Fconsole.qweather.com)

- `HEFENG_KEY`:API key

- 南方周末付费全文

- `INFZM_COOKIE`: infzm 账户登陆后的 cookie,目前只需要 `passport_session=...` 即可获取全文
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ const calculateValue = () => {
google: {
fontsApiKey: envs.GOOGLE_FONTS_API_KEY,
},
hefeng: {
// weather
key: envs.HEFENG_KEY,
},
infzm: {
cookie: envs.INFZM_COOKIE,
},
Expand Down
37 changes: 37 additions & 0 deletions lib/v2/weather/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const got = require('@/utils/got');
const { art } = require('@/utils/render');
const path = require('path');
const config = require('@/config').value;
const rootUrl = 'https://devapi.qweather.com/v7/weather/3d?';

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);
// console.log(response.data);
return response.data;
});
const data = [];
for (const i in responseData.daily) {
data.push(responseData.daily[i]);
}
const items = data.map((item) => ({
title: '预报日期:' + item.fxDate,
description: art(path.join(__dirname, './util/weather.art'), {
item,
}),
}));

ctx.state.data = {
title: ctx.params.location + '未来三天天气',
item: items,
};
};
3 changes: 3 additions & 0 deletions lib/v2/weather/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/weather/:location': ['Rein-Ou'],
};
3 changes: 3 additions & 0 deletions lib/v2/weather/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:location', require('./index'));
};
21 changes: 21 additions & 0 deletions lib/v2/weather/util/weather.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<text>白天:{{item.textDay}}——夜间:{{item.textNight}}</text>
<br>
<text>气温:{{item.tempMin}}℃~{{item.tempMax}}℃</text>
<br>
<text>相对湿度:{{item.humidity}}(%)</text>
<br>
<text>大气压强:{{item.pressure}}(百帕)</text>
<br>
<text>紫外线强度:{{item.uvIndex}}</text>
<br>
<text>白天风向:{{item.windDirDay}};风力:{{item.windScaleDay}}级;风速:{{item.windSpeedDay}}公里/小时</text>
<br>
<text>夜间风向:{{item.windDirNight}};风力:{{item.windScaleNight}}级;风速:{{item.windSpeedNight}}公里/小时</text>
<br>
<text>能见度:{{item.vis}}(公里)</text>
<br>
<text>日出:{{item.sunrise}}&日落: {{item.sunset}}</text>
<br>
<text>月相:{{item.moonPhase}}&月出:{{item.sunrise}}&月落:{{item.moonset}}
<br>

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14587,4 +14587,4 @@ zepto@^1.2.0:
zwitch@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-1.0.5.tgz#d11d7381ffed16b742f6af7b3f223d5cd9fe9920"
integrity sha512-V50KMwwzqJV0NpZIZFwfOD5/lyny3WlSzRiXgA0G7VUnRlqttta1L6UQIHzd6EuBY/cHGfwTIck7w1yH6Q5zUw==
integrity sha512-V50KMwwzqJV0NpZIZFwfOD5/lyny3WlSzRiXgA0G7VUnRlqttta1L6UQIHzd6EuBY/cHGfwTIck7w1yH6Q5zUw==

0 comments on commit 3925f1b

Please sign in to comment.