🎨 🚧 格式化代码

This commit is contained in:
Chenric 2024-06-22 00:10:19 +08:00
parent 6e4154cdec
commit 471585beb0
7 changed files with 61 additions and 43 deletions

View File

@ -9,6 +9,7 @@
"chatrecorder",
"dialectlist",
"displayname",
"htmlrender",
"parameterless",
"pyecharts",
"pygal",

View File

@ -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<type>今日|昨日|本周|上周|本月|上月|年度|历史)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)

View File

@ -0,0 +1 @@
# TODO 更好的图片渲染,支持自定义模板渲染。

View File

@ -0,0 +1 @@
# TODO 使用计数缓存进行数据库查询优化,避免一次性查询过多消息导致内存爆炸。

View File

@ -0,0 +1 @@
# TODO 时间处理模块,用于处理时间相关操作。

View File

@ -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:
"""获取当前时间,并包含时区信息"""

View File

@ -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