From 471585beb015dec3ed2eba8371b40aca0d31f354 Mon Sep 17 00:00:00 2001 From: Chenric <91937041+ChenXu233@users.noreply.github.com> Date: Sat, 22 Jun 2024 00:10:19 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20:construction:=20=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 1 + nonebot_plugin_dialectlist/__init__.py | 54 ++++++++------------------ nonebot_plugin_dialectlist/render.py | 1 + nonebot_plugin_dialectlist/storage.py | 1 + nonebot_plugin_dialectlist/time.py | 1 + nonebot_plugin_dialectlist/utils.py | 29 +++++++++++--- pyproject.toml | 17 +++++++- 7 files changed, 61 insertions(+), 43 deletions(-) create mode 100644 nonebot_plugin_dialectlist/render.py create mode 100644 nonebot_plugin_dialectlist/storage.py create mode 100644 nonebot_plugin_dialectlist/time.py diff --git a/.vscode/settings.json b/.vscode/settings.json index d99f56e..69e2b4b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,7 @@ "chatrecorder", "dialectlist", "displayname", + "htmlrender", "parameterless", "pyecharts", "pygal", diff --git a/nonebot_plugin_dialectlist/__init__.py b/nonebot_plugin_dialectlist/__init__.py index 9be93f6..bc60cdb 100644 --- a/nonebot_plugin_dialectlist/__init__.py +++ b/nonebot_plugin_dialectlist/__init__.py @@ -8,11 +8,11 @@ require("nonebot_plugin_alconna") require("nonebot_plugin_cesaa") import re -import os import nonebot_plugin_saa as saa from pyecharts.charts import Bar -from pyecharts.render import make_snapshot +from pyecharts import options as opts +from pyecharts.globals import ThemeType from typing import Union, Optional, List from datetime import datetime, timedelta @@ -56,6 +56,7 @@ from .utils import ( got_rank, msg_counter, persist_id2user_id, + parse_datetime ) __plugin_meta__ = PluginMetadata( @@ -80,25 +81,25 @@ class SameTime(ArparmaBehavior): if type is None and time: interface.behave_fail() +def wrapper(slot: Union[int, str], content: Optional[str]) -> str: + if slot == "type" and content: + return content + return "" # pragma: no cover rank_cmd = on_alconna( Alconna( "B话榜", Args["type?", ["今日", "昨日", "本周", "上周", "本月", "上月", "年度", "历史"]][ - "time?", str + "time?", str, + "group_id?", int ], behaviors=[SameTime()], ), + aliases={"废话榜"}, use_cmd_start=True, ) -def wrapper(slot: Union[int, str], content: Optional[str]) -> str: - if slot == "type" and content: - return content - return "" # pragma: no cover - - rank_cmd.shortcut( r"(?P今日|昨日|本周|上周|本月|上月|年度|历史)B话榜", { @@ -109,41 +110,21 @@ rank_cmd.shortcut( }, ) - -def parse_datetime(key: str): - """解析数字,并将结果存入 state 中""" - - async def _key_parser( - matcher: AlconnaMatcher, - state: T_State, - input: Union[datetime, Message] = Arg(key), - ): - if isinstance(input, datetime): - return - - plaintext = input.extract_plain_text() - try: - state[key] = get_datetime_fromisoformat_with_timezone(plaintext) - except ValueError: - await matcher.reject_arg(key, "请输入正确的日期,不然我没法理解呢!") - - return _key_parser - - -# TODO 处理函数更新 -# 参考词云 - - # 这段函数完全抄的词云 @rank_cmd.handle() async def _group_message( state: T_State, + session: Session = Depends(extract_session), type: Optional[str] = None, time: Optional[str] = None, + group_id: Optional[int] = None, ): dt = get_datetime_now_with_timezone() + if not group_id: + state["group_id"] = session.id2 + if not type: await rank_cmd.finish(__plugin_meta__.usage) @@ -217,7 +198,6 @@ async def handle_rank( start: datetime = Arg(), stop: datetime = Arg(), ): - """生成词云""" messages = await get_message_records( session=session, id_type=SessionIdType.GROUP, @@ -258,12 +238,12 @@ async def handle_rank( ) string += str_example - bar = Bar() + bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(nicknames) bar.add_yaxis("B话数量", [i[1] for i in rank]) # type: ignore bar.render(str(get_cache_file("nonebot_plugin_dialectlist","cache.html"))) with open(get_cache_file("nonebot_plugin_dialectlist","cache.html")) as f: a = f.read() - image = await html_to_pic(a) + image = await html_to_pic(a,device_scale_factor=3.2) await (saa.Text(string)+saa.Image(image)).finish(reply=True) diff --git a/nonebot_plugin_dialectlist/render.py b/nonebot_plugin_dialectlist/render.py new file mode 100644 index 0000000..3946f32 --- /dev/null +++ b/nonebot_plugin_dialectlist/render.py @@ -0,0 +1 @@ +# TODO 更好的图片渲染,支持自定义模板渲染。 \ No newline at end of file diff --git a/nonebot_plugin_dialectlist/storage.py b/nonebot_plugin_dialectlist/storage.py new file mode 100644 index 0000000..288968b --- /dev/null +++ b/nonebot_plugin_dialectlist/storage.py @@ -0,0 +1 @@ +# TODO 使用计数缓存进行数据库查询优化,避免一次性查询过多消息导致内存爆炸。 \ No newline at end of file diff --git a/nonebot_plugin_dialectlist/time.py b/nonebot_plugin_dialectlist/time.py new file mode 100644 index 0000000..86e4c40 --- /dev/null +++ b/nonebot_plugin_dialectlist/time.py @@ -0,0 +1 @@ +# TODO 时间处理模块,用于处理时间相关操作。 \ No newline at end of file diff --git a/nonebot_plugin_dialectlist/utils.py b/nonebot_plugin_dialectlist/utils.py index 347b1c0..2907243 100644 --- a/nonebot_plugin_dialectlist/utils.py +++ b/nonebot_plugin_dialectlist/utils.py @@ -1,4 +1,3 @@ -import contextlib from datetime import datetime, time, tzinfo from typing import Optional, Dict, List, Union from zoneinfo import ZoneInfo @@ -6,22 +5,42 @@ from sqlalchemy import or_, select from sqlalchemy.sql import ColumnElement from nonebot.log import logger -from nonebot.params import Depends +from nonebot.params import Arg, Depends +from nonebot.typing import T_State from nonebot.matcher import Matcher - -# from nonebot.permission import SUPERUSER +from nonebot.adapters import Message from nonebot_plugin_orm import get_session -from nonebot_plugin_saa import PlatformTarget, get_target from nonebot_plugin_session import Session, SessionLevel, extract_session from nonebot_plugin_session_orm import SessionModel from nonebot_plugin_userinfo import EventUserInfo, UserInfo from nonebot_plugin_apscheduler import scheduler from nonebot_plugin_chatrecorder import MessageRecord +from nonebot_plugin_alconna import AlconnaMatcher + from .config import plugin_config +def parse_datetime(key: str): + """解析数字,并将结果存入 state 中""" + + async def _key_parser( + matcher: AlconnaMatcher, + state: T_State, + input: Union[datetime, Message] = Arg(key), + ): + if isinstance(input, datetime): + return + + plaintext = input.extract_plain_text() + try: + state[key] = get_datetime_fromisoformat_with_timezone(plaintext) + except ValueError: + await matcher.reject_arg(key, "请输入正确的日期,不然我没法理解呢!") + + return _key_parser + def get_datetime_now_with_timezone() -> datetime: """获取当前时间,并包含时区信息""" diff --git a/pyproject.toml b/pyproject.toml index 8c8fdab..f8b2e0a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot_plugin_dialectlist" -version = "2.0.3" +version = "2.0.4" description = "Default template for PDM package" authors = [ {name = "Chen_Xu233", email = "woyerpa@outlook.com"}, @@ -37,3 +37,18 @@ line-length = 80 [tool.ruff.format] quote-style = "single" indent-style = "tab" + +[tool.pdm.scripts] + +build = 'pdm run setup.py sdist' +publish = 'pdm run python -m twine upload dist/*' + + +# 以下为智普 AI 生成,还不知道这玩意有啥用。 +# [tool.pdm.dev-dependencies] +# black = "^23.1.0" +# isort = "^5.10.1" +# pre-commit = "^2.20.0" + +# [tool.pdm.global-options] +# --no-pip-version-check = true \ No newline at end of file