mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-28 16:24:51 +08:00
fix: 天气查询失败的问题
This commit is contained in:
parent
79d8063b5d
commit
0d3361dc99
@ -28,6 +28,6 @@ $$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ |
|
|||||||
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||||
""" + "\033[0m")
|
""" + "\033[0m")
|
||||||
|
|
||||||
sys_lang = get_default_lang()
|
sys_lang = Language(get_default_lang_code())
|
||||||
nonebot.logger.info(sys_lang.get("main.current_language", LANG=sys_lang.get("language.name")))
|
nonebot.logger.info(sys_lang.get("main.current_language", LANG=sys_lang.get("language.name")))
|
||||||
nonebot.logger.info(sys_lang.get("main.enable_webdash", URL=f"http://127.0.0.1:{config.get('port', 20216)}"))
|
nonebot.logger.info(sys_lang.get("main.enable_webdash", URL=f"http://127.0.0.1:{config.get('port', 20216)}"))
|
||||||
|
@ -8,7 +8,7 @@ from nonebot.adapters.onebot.v11 import MessageSegment
|
|||||||
from nonebot.permission import SUPERUSER
|
from nonebot.permission import SUPERUSER
|
||||||
from liteyuki.utils import __NAME__, __VERSION__, load_from_yaml
|
from liteyuki.utils import __NAME__, __VERSION__, load_from_yaml
|
||||||
from liteyuki.utils.message.html_tool import template2image
|
from liteyuki.utils.message.html_tool import template2image
|
||||||
from liteyuki.utils.base.language import Language, get_default_lang, get_user_lang
|
from liteyuki.utils.base.language import Language, get_default_lang_code, get_user_lang
|
||||||
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
|
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||||
from liteyuki.utils.base.resource import get_path
|
from liteyuki.utils.base.resource import get_path
|
||||||
from liteyuki.utils.message.tools import convert_size
|
from liteyuki.utils.message.tools import convert_size
|
||||||
@ -100,7 +100,7 @@ async def get_stats_data(self_id: str = None, lang: str = None) -> dict:
|
|||||||
if self_id is None:
|
if self_id is None:
|
||||||
self_id = list(nonebot.get_bots().keys())[0] if len(nonebot.get_bots()) > 0 else "liteyuki"
|
self_id = list(nonebot.get_bots().keys())[0] if len(nonebot.get_bots()) > 0 else "liteyuki"
|
||||||
if lang is None:
|
if lang is None:
|
||||||
ulang = get_default_lang()
|
ulang = Language(get_default_lang_code())
|
||||||
else:
|
else:
|
||||||
ulang = Language(lang)
|
ulang = Language(lang)
|
||||||
|
|
||||||
|
@ -1,33 +1,22 @@
|
|||||||
from .qw_models import *
|
from .qw_models import *
|
||||||
import httpx
|
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
|
dev_url = "https://devapi.qweather.com/" # 开发HBa
|
||||||
com_url = "https://api.qweather.com/" # 正式环境
|
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(
|
async def city_lookup(
|
||||||
location: str,
|
location: str,
|
||||||
key: str,
|
key: str,
|
||||||
@ -53,7 +42,7 @@ async def city_lookup(
|
|||||||
"adm" : adm,
|
"adm" : adm,
|
||||||
"number" : number,
|
"number" : number,
|
||||||
"key" : key,
|
"key" : key,
|
||||||
"lang" : language_map.get(lang, lang),
|
"lang" : lang,
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
resp = await client.get(url, params=params)
|
resp = await client.get(url, params=params)
|
||||||
@ -72,7 +61,7 @@ async def get_weather_now(
|
|||||||
params = {
|
params = {
|
||||||
"location": location,
|
"location": location,
|
||||||
"key" : key,
|
"key" : key,
|
||||||
"lang" : language_map.get(lang, lang),
|
"lang" : lang,
|
||||||
"unit" : unit,
|
"unit" : unit,
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
@ -92,7 +81,7 @@ async def get_weather_daily(
|
|||||||
params = {
|
params = {
|
||||||
"location": location,
|
"location": location,
|
||||||
"key" : key,
|
"key" : key,
|
||||||
"lang" : language_map.get(lang, lang),
|
"lang" : lang,
|
||||||
"unit" : unit,
|
"unit" : unit,
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
@ -112,7 +101,7 @@ async def get_weather_hourly(
|
|||||||
params = {
|
params = {
|
||||||
"location": location,
|
"location": location,
|
||||||
"key" : key,
|
"key" : key,
|
||||||
"lang" : language_map.get(lang, lang),
|
"lang" : lang,
|
||||||
"unit" : unit,
|
"unit" : unit,
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient() as client:
|
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
|
url = dev_url + url_path if dev else com_url + url_path
|
||||||
params = {
|
params = {
|
||||||
"key" : key,
|
"key" : key,
|
||||||
"lang" : language_map.get(lang, lang),
|
"lang" : lang,
|
||||||
"pollutant": pollutant,
|
"pollutant": pollutant,
|
||||||
"station" : station,
|
"station" : station,
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
resp = await client.get(url, params=params)
|
resp = await client.get(url, params=params)
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|
||||||
|
@ -25,9 +25,10 @@ from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma
|
|||||||
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
|
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
|
||||||
"""await alconna.send("weather", city)"""
|
"""await alconna.send("weather", city)"""
|
||||||
ulang = get_user_lang(str(event.user_id))
|
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")
|
key = get_config("weather_key")
|
||||||
is_dev = get_config("weather_dev")
|
is_dev = get_config("weather_dev")
|
||||||
|
|
||||||
user: User = user_db.first(User(), "user_id = ?", str(event.user_id), default=User())
|
user: User = user_db.first(User(), "user_id = ?", str(event.user_id), default=User())
|
||||||
|
|
||||||
# params
|
# params
|
||||||
@ -38,7 +39,6 @@ async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
|
|||||||
await matcher.finish(ulang.get("weather.no_key"))
|
await matcher.finish(ulang.get("weather.no_key"))
|
||||||
|
|
||||||
kws = result.main_args.get("keywords")
|
kws = result.main_args.get("keywords")
|
||||||
|
|
||||||
if kws:
|
if kws:
|
||||||
if len(kws) >= 2:
|
if len(kws) >= 2:
|
||||||
adm = kws[0]
|
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"))
|
await matcher.finish(ulang.get("liteyuki.invalid_command", TEXT="location"))
|
||||||
city_info = await city_lookup(stored_location, key, lang=qw_lang)
|
city_info = await city_lookup(stored_location, key, lang=qw_lang)
|
||||||
city_name = stored_location
|
city_name = stored_location
|
||||||
|
|
||||||
if city_info.code == "200":
|
if city_info.code == "200":
|
||||||
location_data = city_info.location[0]
|
location_data = city_info.location[0]
|
||||||
else:
|
else:
|
||||||
|
@ -102,9 +102,10 @@ def load_from_dict(data: dict, lang_code: str):
|
|||||||
|
|
||||||
class Language:
|
class Language:
|
||||||
def __init__(self, lang_code: str = None, fallback_lang_code: str = "zh-CN"):
|
def __init__(self, lang_code: str = None, fallback_lang_code: str = "zh-CN"):
|
||||||
if lang_code is None:
|
|
||||||
lang_code = config.get("default_language", get_default_lang())
|
|
||||||
self.lang_code = lang_code
|
self.lang_code = lang_code
|
||||||
|
if self.lang_code is None:
|
||||||
|
self.lang_code = get_default_lang_code()
|
||||||
|
|
||||||
self.fallback_lang_code = fallback_lang_code
|
self.fallback_lang_code = fallback_lang_code
|
||||||
if self.fallback_lang_code is None:
|
if self.fallback_lang_code is None:
|
||||||
self.fallback_lang_code = config.get("default_language", get_system_lang_code())
|
self.fallback_lang_code = config.get("default_language", get_system_lang_code())
|
||||||
@ -159,7 +160,7 @@ def get_user_lang(user_id: str) -> Language:
|
|||||||
username="Unknown"
|
username="Unknown"
|
||||||
))
|
))
|
||||||
|
|
||||||
return Language(user.profile.get("lang", get_default_lang()))
|
return Language(user.profile.get("lang", get_default_lang_code()))
|
||||||
|
|
||||||
|
|
||||||
def get_system_lang_code() -> str:
|
def get_system_lang_code() -> str:
|
||||||
@ -169,11 +170,13 @@ def get_system_lang_code() -> str:
|
|||||||
return locale.getdefaultlocale()[0].replace('_', '-')
|
return locale.getdefaultlocale()[0].replace('_', '-')
|
||||||
|
|
||||||
|
|
||||||
def get_default_lang() -> Language:
|
def get_default_lang_code() -> str:
|
||||||
"""
|
"""
|
||||||
获取配置默认/系统语言
|
获取默认语言代码
|
||||||
|
Returns:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return Language(config.get("default_language", get_system_lang_code()))
|
return config.get("default_language", get_system_lang_code())
|
||||||
|
|
||||||
|
|
||||||
def get_all_lang() -> dict[str, str]:
|
def get_all_lang() -> dict[str, str]:
|
||||||
|
@ -2,7 +2,7 @@ import sys
|
|||||||
import loguru
|
import loguru
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from .config import load_from_yaml
|
from .config import load_from_yaml
|
||||||
from .language import get_default_lang
|
from .language import Language, get_default_lang_code
|
||||||
|
|
||||||
logger = loguru.logger
|
logger = loguru.logger
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -64,7 +64,7 @@ def init_log():
|
|||||||
format=get_format(config.get("log_level", "INFO")),
|
format=get_format(config.get("log_level", "INFO")),
|
||||||
)
|
)
|
||||||
show_icon = config.get("log_icon", True)
|
show_icon = config.get("log_icon", True)
|
||||||
lang = get_default_lang()
|
lang = Language(get_default_lang_code())
|
||||||
|
|
||||||
debug = lang.get("log.debug", default="==DEBUG")
|
debug = lang.get("log.debug", default="==DEBUG")
|
||||||
info = lang.get("log.info", default="===INFO")
|
info = lang.get("log.info", default="===INFO")
|
||||||
|
@ -7,11 +7,11 @@ import nonebot
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .data import LiteModel
|
from .data import LiteModel
|
||||||
from .language import get_default_lang
|
from .language import Language, get_default_lang_code
|
||||||
|
|
||||||
_loaded_resource_packs: list["ResourceMetadata"] = [] # 按照加载顺序排序
|
_loaded_resource_packs: list["ResourceMetadata"] = [] # 按照加载顺序排序
|
||||||
temp_resource_root = "data/liteyuki/resources"
|
temp_resource_root = "data/liteyuki/resources"
|
||||||
lang = get_default_lang()
|
lang = Language(get_default_lang_code())
|
||||||
|
|
||||||
|
|
||||||
class ResourceMetadata(LiteModel):
|
class ResourceMetadata(LiteModel):
|
||||||
|
Loading…
Reference in New Issue
Block a user