From ce2b89a6428979a7ca91861c88a5fc41283581fe Mon Sep 17 00:00:00 2001
From: Yuhang <50909599+MiuNice@users.noreply.github.com>
Date: Fri, 6 Dec 2024 00:52:06 +0800
Subject: [PATCH] fix: qweather route example inaccuracy. (#17805)
* fix: qweather route example inaccuracy.
qweather route example inaccuracy
* fix(qweather): throw on config not found
---------
---
lib/routes/qweather/3days.ts | 7 ++++---
lib/routes/qweather/now.ts | 21 ++++++++++++---------
2 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/lib/routes/qweather/3days.ts b/lib/routes/qweather/3days.ts
index d077dde84f7d76..7602d89d493994 100644
--- a/lib/routes/qweather/3days.ts
+++ b/lib/routes/qweather/3days.ts
@@ -35,19 +35,20 @@ export const route: Route = {
name: '近三天天气',
maintainers: ['Rein-Ou', 'la3rence'],
handler,
- description: `需自行注册获取 api 的 key,并在环境变量 HEFENG\_KEY 中进行配置,获取订阅近三天天气预报`,
+ description: '获取订阅近三天天气预报',
};
async function handler(ctx) {
if (!config.hefeng.key) {
throw new ConfigNotFoundError('QWeather RSS is disabled due to the lack of relevant config');
}
- const id = await cache.tryGet(ctx.req.param('location') + '_id', async () => {
+
+ const id = await cache.tryGet('qweather:' + ctx.req.param('location') + ':id', async () => {
const response = await got(`${CIRY_LOOKUP_API}?location=${ctx.req.param('location')}&key=${config.hefeng.key}`);
return response.data.location[0].id;
});
const weatherData = await cache.tryGet(
- ctx.req.param('location'),
+ 'qweather:' + ctx.req.param('location'),
async () => {
const response = await got(`${WEATHER_API}?key=${config.hefeng.key}&location=${id}`);
return response.data;
diff --git a/lib/routes/qweather/now.ts b/lib/routes/qweather/now.ts
index c73e227b53bd9c..4233562d7fcc7c 100644
--- a/lib/routes/qweather/now.ts
+++ b/lib/routes/qweather/now.ts
@@ -8,17 +8,20 @@ import { art } from '@/utils/render';
import path from 'node:path';
import { parseDate } from '@/utils/parse-date';
import { config } from '@/config';
+import ConfigNotFoundError from '@/errors/types/config-not-found';
+
const rootUrl = 'https://devapi.qweather.com/v7/weather/now?';
+
export const route: Route = {
path: '/now/:location',
categories: ['forecast'],
- example: '/qweather/广州',
+ example: '/qweather/now/广州',
parameters: { location: 'N' },
features: {
requireConfig: [
{
name: 'HEFENG_KEY',
- description: '',
+ description: '访问 `https://www.qweather.com/` 注册开发 API Key。',
},
],
requirePuppeteer: false,
@@ -30,21 +33,21 @@ export const route: Route = {
name: '实时天气',
maintainers: ['Rein-Ou'],
handler,
- description: `需自行注册获取 api 的 key,每小时更新一次数据`,
};
async function handler(ctx) {
- const id = await cache.tryGet(ctx.req.param('location') + '_id', async () => {
+ if (!config.hefeng.key) {
+ throw new ConfigNotFoundError('QWeather RSS is disabled due to the lack of relevant config');
+ }
+
+ const id = await cache.tryGet('qweather:' + ctx.req.param('location') + ':id', async () => {
const response = await got(`https://geoapi.qweather.com/v2/city/lookup?location=${ctx.req.param('location')}&key=${config.hefeng.key}`);
- const data = [];
- for (const i in response.data.location) {
- data.push(response.data.location[i]);
- }
+ const data = response.data.location.map((loc) => loc);
return data[0].id;
});
const requestUrl = rootUrl + 'key=' + config.hefeng.key + '&location=' + id;
const responseData = await cache.tryGet(
- ctx.req.param('location') + '_now',
+ 'qweather:' + ctx.req.param('location') + ':now',
async () => {
const response = await got(requestUrl);
return response.data;