Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

Commit

Permalink
use htmlrender to render chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Perseus037 committed Sep 15, 2023
1 parent 0ffa31e commit 3e8dc5e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
1 change: 1 addition & 0 deletions nonebot_plugin_uma/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from nonebot.plugin import PluginMetadata, require

require("nonebot_plugin_apscheduler")
require("nonebot_plugin_htmlrender")

from . import __main__ as __main__ # noqa: E402
from .config import ConfigModel # noqa: E402
Expand Down
67 changes: 32 additions & 35 deletions nonebot_plugin_uma/uma_skills/generate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from io import BytesIO
from typing import List, Tuple, Union

from fuzzywuzzy import process
from nonebot.adapters.onebot.v11 import MessageSegment
from pil_utils import BuildImage, Text2Image
from prettytable import PrettyTable
from nonebot_plugin_htmlrender import md_to_pic

from ..utils import md_escape
from .data_source import SkillConfigDict, SkillInfoDict


Expand Down Expand Up @@ -67,41 +66,39 @@ async def get_skill_info(skill_name: str, f_data: SkillConfigDict) -> str:


# 生成图片
def create_img(title: str, info_data: SkillInfoType) -> BytesIO:
async def create_img(title: str, info_data: SkillInfoType) -> bytes:
if not info_data:
raise ValueError("info_data is empty")

field_names = info_data[0][1].keys()
table = PrettyTable(field_names=field_names, title=title)

for name, trans in info_data:
table.add_row(
[
name,
trans["中文名"],
trans["稀有度"].replace("·", ""),
trans["颜色"],
trans["繁中译名"],
trans["条件限制"],
trans["技能数值"],
trans["持续时间"],
trans["评价分"],
trans["需要PT"],
trans["PT评价比"],
trans["触发条件"],
trans["技能类型"],
],
)

space = 5
table_text = Text2Image.from_text(table.get_string(), 20)
bg = BuildImage.new(
"RGB",
(table_text.width + space * 2, table_text.height + space * 2),
(255, 255, 255),
table_head = ["技能名", *list(info_data[0][1].keys())]
table_rows = [
[
name,
trans["中文名"],
trans["稀有度"].replace("·", ""),
trans["颜色"],
trans["繁中译名"],
trans["条件限制"],
trans["技能数值"],
trans["持续时间"],
trans["评价分"],
trans["需要PT"],
trans["PT评价比"],
trans["触发条件"],
trans["技能类型"],
]
for name, trans in info_data
]

title = md_escape(title)
table_head_md = f"|{'|'.join(table_head)}|"
table_align_md = f"|{'|'.join([':-:' for _ in table_head])}|"
table_rows_md = "\n".join(
[f"|{'|'.join([md_escape(i) for i in row])}|" for row in table_rows],
)
table_text.draw_on_image(bg.image, (space, space))
return bg.save("jpg")
extra_html = "<style>.markdown-body { max-width: 100% !important; }</style>"
md = f"# {title}\n\n{table_head_md}\n{table_align_md}\n{table_rows_md}\n\n{extra_html}"
return await md_to_pic(md, width=1800, type="jpeg")


# 获取马娘技能列表
Expand Down Expand Up @@ -161,4 +158,4 @@ async def get_skill_list(
name_list.append(name)
title = f"检索:{' + '.join(name_list)} 的结果"

return MessageSegment.image(create_img(title, infos))
return MessageSegment.image(await create_img(title, infos))
6 changes: 6 additions & 0 deletions nonebot_plugin_uma/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import re


def md_escape(text: str) -> str:
pattern = r"([\\_*\[\](){}#+\-.!])"
return re.sub(pattern, r"\\\1", text)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ authors = [
{ name = "Perseus037", email = "1209228678@qq.com" },
{ name = "student_2333", email = "lgc2333@126.com" },
]
version = "0.1.0.dev1"
version = "0.1.0.dev2"
description = "A ported version of azmiao/uma_plugin for NoneBot2."
dependencies = [
"nonebot2>=2.1.0",
"nonebot-adapter-onebot>=2.2.0",
"nonebot-plugin-apscheduler>=0.3.0",
"nonebot-plugin-htmlrender>=0.2.2",
"pydantic>=1.10.4,<2",
"httpx>=0.25.0",
"lxml>=4.9.3",
"beautifulsoup4>=4.12.2",
"pillow<10",
"fuzzywuzzy>=0.18.0",
"prettytable>=3.9.0",
"pil-utils>=0.1.8",
]
requires-python = ">=3.8,<4.0"
Expand Down

0 comments on commit 3e8dc5e

Please sign in to comment.