Skip to content

Commit

Permalink
使用 saa
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Nov 19, 2023
1 parent bbb003a commit a99f516
Show file tree
Hide file tree
Showing 4 changed files with 997 additions and 1,014 deletions.
26 changes: 15 additions & 11 deletions nonebot_plugin_heweather/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from nonebot import on_keyword
from nonebot.log import logger
from nonebot.matcher import Matcher
from nonebot.plugin import PluginMetadata
from nonebot.adapters.onebot.v11 import MessageEvent, MessageSegment
from nonebot import require, on_keyword
from nonebot.params import EventPlainText
from nonebot.plugin import PluginMetadata, inherit_supported_adapters

require("nonebot_plugin_saa")
require("nonebot_plugin_htmlrender")

from nonebot_plugin_saa import Image, MessageFactory

from .render_pic import render
from .weather_data import Weather, ConfigError, CityNotFoundError
Expand All @@ -15,8 +20,7 @@
type="application",
homepage="https://github.com/kexue-z/nonebot-plugin-heweather",
config=Config,
extra={},
supported_adapters={"~onebot.v11"},
supported_adapters=inherit_supported_adapters("nonebot_plugin_saa"),
)


Expand All @@ -28,32 +32,32 @@


@weather.handle()
async def _(matcher: Matcher, event: MessageEvent):
async def _(matcher: Matcher, arg: str = EventPlainText()):
if not (QWEATHER_APIKEY and QWEATHER_APITYPE):
raise ConfigError("请设置 qweather_apikey 和 qweather_apitype")

city = ""
if args := event.get_plaintext().split("天气"):
if args := arg.split("天气"):
city = args[0].strip() or args[1].strip()
if not city:
await weather.finish("地点是...空气吗?? >_<")
await matcher.finish("地点是...空气吗?? >_<")

# 判断指令前后是否都有内容,如果是则结束,否则跳过。
if (args[0].strip() == "") == (args[1].strip() == ""):
await weather.finish()
await matcher.finish()
w_data = Weather(city_name=city, api_key=QWEATHER_APIKEY, api_type=QWEATHER_APITYPE)
try:
await w_data.load_data()
except CityNotFoundError:
matcher.block = False
await weather.finish()
await matcher.finish()

img = await render(w_data)

if DEBUG:
debug_save_img(img)

await weather.finish(MessageSegment.image(img))
await MessageFactory([Image(img)]).send()


def debug_save_img(img: bytes) -> None:
Expand Down
4 changes: 0 additions & 4 deletions nonebot_plugin_heweather/render_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
from typing import List
from pathlib import Path

from nonebot import require

require("nonebot_plugin_htmlrender")

from nonebot_plugin_htmlrender import template_to_pic

from .config import QWEATHER_HOURLYTYPE
Expand Down
Loading

0 comments on commit a99f516

Please sign in to comment.