fix: 天气查询失败的问题

This commit is contained in:
2024-04-15 20:21:50 +08:00
parent 79d8063b5d
commit 0d3361dc99
7 changed files with 34 additions and 44 deletions

View File

@ -1,33 +1,22 @@
from .qw_models import *
import httpx
language_map = {
"zh-CN" : "zh",
"zh-HK" : "zh-hant",
"en-US" : "en",
"ja-JP" : "ja",
"ko-KR" : "ko",
"fr-FR" : "fr",
"es-ES" : "es",
"de-DE" : "de",
"it-IT" : "it",
"ru-RU" : "ru",
"ar-SA" : "ar",
"pt-BR" : "pt",
"nl-NL" : "nl",
"pl-PL" : "pl",
"tr-TR" : "tr",
"th-TH" : "th",
"vi-VN" : "vi",
"id-ID" : "id",
"ms-MY" : "ms",
"fil-PH": "fil",
} # 其他使用默认对应
dev_url = "https://devapi.qweather.com/" # 开发HBa
com_url = "https://api.qweather.com/" # 正式环境
def get_qw_lang(lang: str) -> str:
if lang in ["zh-HK", "zh-TW"]:
return "zh-hant"
elif lang.startswith("zh"):
return "zh"
elif lang.startswith("en"):
return "en"
else:
return lang
async def city_lookup(
location: str,
key: str,
@ -53,7 +42,7 @@ async def city_lookup(
"adm" : adm,
"number" : number,
"key" : key,
"lang" : language_map.get(lang, lang),
"lang" : lang,
}
async with httpx.AsyncClient() as client:
resp = await client.get(url, params=params)
@ -72,7 +61,7 @@ async def get_weather_now(
params = {
"location": location,
"key" : key,
"lang" : language_map.get(lang, lang),
"lang" : lang,
"unit" : unit,
}
async with httpx.AsyncClient() as client:
@ -92,7 +81,7 @@ async def get_weather_daily(
params = {
"location": location,
"key" : key,
"lang" : language_map.get(lang, lang),
"lang" : lang,
"unit" : unit,
}
async with httpx.AsyncClient() as client:
@ -112,7 +101,7 @@ async def get_weather_hourly(
params = {
"location": location,
"key" : key,
"lang" : language_map.get(lang, lang),
"lang" : lang,
"unit" : unit,
}
async with httpx.AsyncClient() as client:
@ -132,11 +121,10 @@ async def get_airquality(
url = dev_url + url_path if dev else com_url + url_path
params = {
"key" : key,
"lang" : language_map.get(lang, lang),
"lang" : lang,
"pollutant": pollutant,
"station" : station,
}
async with httpx.AsyncClient() as client:
resp = await client.get(url, params=params)
return resp.json()

View File

@ -25,9 +25,10 @@ from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
"""await alconna.send("weather", city)"""
ulang = get_user_lang(str(event.user_id))
qw_lang = language_map.get(ulang.lang_code, ulang.lang_code)
qw_lang = get_qw_lang(ulang.lang_code)
key = get_config("weather_key")
is_dev = get_config("weather_dev")
user: User = user_db.first(User(), "user_id = ?", str(event.user_id), default=User())
# params
@ -38,7 +39,6 @@ async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
await matcher.finish(ulang.get("weather.no_key"))
kws = result.main_args.get("keywords")
if kws:
if len(kws) >= 2:
adm = kws[0]
@ -53,7 +53,6 @@ async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
await matcher.finish(ulang.get("liteyuki.invalid_command", TEXT="location"))
city_info = await city_lookup(stored_location, key, lang=qw_lang)
city_name = stored_location
if city_info.code == "200":
location_data = city_info.location[0]
else: