Skip to content

Commit

Permalink
fix: qweather route example inaccuracy. (#17805)
Browse files Browse the repository at this point in the history
* fix: qweather route example inaccuracy.

qweather route example inaccuracy

* fix(qweather): throw on config not found

---------
  • Loading branch information
MiuNice authored Dec 5, 2024
1 parent db98160 commit ce2b89a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
7 changes: 4 additions & 3 deletions lib/routes/qweather/3days.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://docs.rsshub.app/zh/install/config#%E5%92%8C%E9%A3%8E%E5%A4%A9%E6%B0%94">relevant config</a>');
}
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;
Expand Down
21 changes: 12 additions & 9 deletions lib/routes/qweather/now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 <a href="https://docs.rsshub.app/zh/install/config#%E5%92%8C%E9%A3%8E%E5%A4%A9%E6%B0%94">relevant config</a>');
}

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;
Expand Down

0 comments on commit ce2b89a

Please sign in to comment.