require htmlrender when needed

This commit is contained in:
felinae98 2022-02-24 15:28:48 +08:00
parent af8cbf6ecf
commit 6cdb4e625e
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
4 changed files with 12 additions and 9 deletions

View File

@ -1,7 +1,3 @@
from nonebot.plugin import require
require("nonebot_plugin_htmlrender")
from . import (
admin_page,
config,

View File

@ -1,10 +1,9 @@
import json
from typing import Any, Type
from typing import Any
import httpx
from bs4 import BeautifulSoup as bs
from nonebot.plugin import require
from nonebot_plugin_htmlrender import capture_element
from ..post import Post
from ..types import Category, RawPost, Target
@ -52,6 +51,8 @@ class Arknights(NewMessage):
if soup.find("div", class_="standerd-container"):
# 图文
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import capture_element
pic_data = await capture_element(
announce_url,
"div.main",

View File

@ -1,5 +1,3 @@
import warnings
import nonebot
from pydantic import BaseSettings
@ -13,6 +11,7 @@ class PlugConfig(BaseSettings):
bison_outer_url: str = "http://localhost:8080/bison/"
bison_filter_log: bool = False
bison_to_me: bool = True
bison_skip_browser_check: bool = False
class Config:
extra = "ignore"

View File

@ -6,7 +6,7 @@ import nonebot
from bs4 import BeautifulSoup as bs
from nonebot.adapters.onebot.v11.message import MessageSegment
from nonebot.log import default_format, logger
from nonebot_plugin_htmlrender import text_to_pic as _text_to_pic
from nonebot.plugin import require
from .plugin_config import plugin_config
@ -23,11 +23,18 @@ class Singleton(type):
async def parse_text(text: str) -> MessageSegment:
"return raw text if don't use pic, otherwise return rendered opcode"
if plugin_config.bison_use_pic:
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import text_to_pic as _text_to_pic
return MessageSegment.image(await _text_to_pic(text))
else:
return MessageSegment.text(text)
if not plugin_config.bison_skip_browser_check:
require("nonebot_plugin_htmlrender")
def html_to_text(html: str, query_dict: dict = {}) -> str:
html = re.sub(r"<br\s*/?>", "<br>\n", html)
html = html.replace("</p>", "</p>\n")