diff --git a/docs/forecast.md b/docs/forecast.md index 3285969ad..552d3618a 100644 --- a/docs/forecast.md +++ b/docs/forecast.md @@ -129,3 +129,13 @@ pageClass: routes 可通过全局过滤参数订阅您感兴趣的地区. + +## 和风天气 + +### 近三天天气 + + + +需自行注册获取和风天气 api 的 key,并在环境变量HEFENG_KEY中进行配置,获取订阅近三天天气预报 + + diff --git a/docs/install/README.md b/docs/install/README.md index b29ba9cd3..75ca6bb01 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -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=...` 即可获取全文 diff --git a/lib/config.js b/lib/config.js index cd0146551..0ecdd68a2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -149,6 +149,10 @@ const calculateValue = () => { google: { fontsApiKey: envs.GOOGLE_FONTS_API_KEY, }, + hefeng: { + // weather + key: envs.HEFENG_KEY, + }, infzm: { cookie: envs.INFZM_COOKIE, }, diff --git a/lib/v2/weather/index.js b/lib/v2/weather/index.js new file mode 100644 index 000000000..5fab477a7 --- /dev/null +++ b/lib/v2/weather/index.js @@ -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, + }; +}; diff --git a/lib/v2/weather/maintainer.js b/lib/v2/weather/maintainer.js new file mode 100644 index 000000000..1455db8f8 --- /dev/null +++ b/lib/v2/weather/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/weather/:location': ['Rein-Ou'], +}; diff --git a/lib/v2/weather/router.js b/lib/v2/weather/router.js new file mode 100644 index 000000000..c08018c1d --- /dev/null +++ b/lib/v2/weather/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:location', require('./index')); +}; diff --git a/lib/v2/weather/util/weather.art b/lib/v2/weather/util/weather.art new file mode 100644 index 000000000..c282d3a49 --- /dev/null +++ b/lib/v2/weather/util/weather.art @@ -0,0 +1,21 @@ +白天:{{item.textDay}}——夜间:{{item.textNight}} +
+气温:{{item.tempMin}}℃~{{item.tempMax}}℃ +
+相对湿度:{{item.humidity}}(%) +
+大气压强:{{item.pressure}}(百帕) +
+紫外线强度:{{item.uvIndex}} +
+白天风向:{{item.windDirDay}};风力:{{item.windScaleDay}}级;风速:{{item.windSpeedDay}}公里/小时 +
+夜间风向:{{item.windDirNight}};风力:{{item.windScaleNight}}级;风速:{{item.windSpeedNight}}公里/小时 +
+能见度:{{item.vis}}(公里) +
+日出:{{item.sunrise}}&日落: {{item.sunset}} +
+月相:{{item.moonPhase}}&月出:{{item.sunrise}}&月落:{{item.moonset}} +
+ diff --git a/yarn.lock b/yarn.lock index 5f92b2dad..194fbd558 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== \ No newline at end of file