app/liteyuki/plugins/liteyuki_weather/qweather.py

102 lines
4.0 KiB
Python
Raw Normal View History

2024-04-27 02:20:44 +08:00
from nonebot import require, on_endswith
from nonebot.adapters import satori
from nonebot.adapters.onebot.v11 import MessageSegment
from nonebot.internal.matcher import Matcher
2024-04-10 23:47:10 +08:00
2024-05-12 02:47:14 +08:00
from liteyuki.utils.base.config import get_config
from liteyuki.utils.base.ly_typing import T_MessageEvent
2024-04-11 13:15:29 +08:00
from .qw_api import *
2024-05-12 02:47:14 +08:00
from liteyuki.utils.base.data_manager import User, user_db
from liteyuki.utils.base.language import Language, get_user_lang
from liteyuki.utils.base.resource import get_path
from liteyuki.utils.message.html_tool import template2image
from liteyuki.utils import event as event_utils
2024-04-10 23:47:10 +08:00
require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma, UniMessage
2024-04-10 23:47:10 +08:00
2024-05-20 08:51:21 +08:00
wx_alc = on_alconna(
2024-04-10 23:47:10 +08:00
aliases={"天气"},
command=Alconna(
"weather",
Args["keywords", MultiVar(str), []],
2024-04-10 23:47:10 +08:00
),
)
2024-05-20 08:51:21 +08:00
@wx_alc.handle()
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
2024-04-11 14:41:46 +08:00
"""await alconna.send("weather", city)"""
2024-04-27 02:20:44 +08:00
kws = result.main_args.get("keywords")
image = await get_weather_now_card(matcher, event, kws)
2024-05-20 08:51:21 +08:00
await wx_alc.finish(UniMessage.image(raw=image))
2024-04-15 20:21:50 +08:00
2024-04-27 02:20:44 +08:00
@on_endswith(("天气", "weather")).handle()
async def _(event: T_MessageEvent, matcher: Matcher):
"""await alconna.send("weather", city)"""
# kws = event.message.extract_plain_text()
kws = event.get_plaintext()
2024-04-27 02:20:44 +08:00
image = await get_weather_now_card(matcher, event, [kws.replace("天气", "").replace("weather", "")], False)
if isinstance(event, satori.event.Event):
await matcher.finish(satori.MessageSegment.image(raw=image, mime="image/png"))
else:
await matcher.finish(MessageSegment.image(image))
2024-04-27 02:20:44 +08:00
async def get_weather_now_card(matcher: Matcher, event: T_MessageEvent, keyword: list[str], tip: bool = True):
ulang = get_user_lang(event_utils.get_user_id(event))
2024-04-27 02:20:44 +08:00
qw_lang = get_qw_lang(ulang.lang_code)
key = get_config("weather_key")
is_dev = get_memory_data("weather.is_dev", True)
user: User = user_db.where_one(User(), "user_id = ?", event_utils.get_user_id(event), default=User())
# params
unit = user.profile.get("unit", "m")
stored_location = user.profile.get("location", None)
if not key:
2024-04-27 02:20:44 +08:00
await matcher.finish(ulang.get("weather.no_key") if tip else None)
2024-04-27 02:20:44 +08:00
if keyword:
if len(keyword) >= 2:
adm = keyword[0]
city = keyword[-1]
else:
adm = ""
2024-04-27 02:20:44 +08:00
city = keyword[0]
city_info = await city_lookup(city, key, adm=adm, lang=qw_lang)
2024-04-27 02:20:44 +08:00
city_name = " ".join(keyword)
else:
if not stored_location:
2024-04-27 02:20:44 +08:00
await matcher.finish(ulang.get("liteyuki.invalid_command", TEXT="location") if tip else None)
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:
2024-04-27 02:20:44 +08:00
await matcher.finish(ulang.get("weather.city_not_found", CITY=city_name) if tip else None)
weather_now = await get_weather_now(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
weather_daily = await get_weather_daily(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
weather_hourly = await get_weather_hourly(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
aqi = await get_airquality(key, location_data.id, lang=qw_lang, dev=is_dev)
image = await template2image(
template=get_path("templates/weather_now.html", abs_path=True),
templates={
2024-05-25 12:09:54 +08:00
"data": {
"params" : {
"unit": unit,
"lang": ulang.lang_code,
},
"weatherNow" : weather_now,
"weatherDaily" : weather_daily,
"weatherHourly": weather_hourly,
"aqi" : aqi,
"location" : location_data.dump(),
"localization" : get_local_data(ulang.lang_code)
}
},
)
2024-04-27 02:20:44 +08:00
return image