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 ( from . import (
admin_page, admin_page,
config, config,

View File

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

View File

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

View File

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