diff --git a/README.md b/README.md index c8015f3..c91b2ea 100644 --- a/README.md +++ b/README.md @@ -80,4 +80,7 @@ _✨ 从哔哩哔哩会员购获取展览简易信息 ✨_ | 配置项 | 必填 | 默认值 | 说明 | | :---------------: | :--: | :----: | :----------------------------------------------------------: | | ACGNSHOW_PAGESIZE | 否 | 8 | 单个图片的条目数,最大为 20,条目数过大可能导致 Bot 无法发送 | -| ACGNSHOW_BGIMAGE_PATH | 否 | 插件内置背景图 | 插件返回图片的背景图目录路径 | \ No newline at end of file +| ACGNSHOW_BGIMAGE_PATH | 否 | 插件内置背景图 | 插件返回图片的背景图目录路径 | +| ACGNSHOW_SEND_SHOW_DETAILS_HTML | 否 | false | 是否发送展览的 HTML 详情信息图片(会员购的“活动介绍”栏目),实验性功能 | +| ACGNSHOW_SHOW_DETAILS_HTML_SCALE | 否 | 0.2 | HTML 详情信息图片的缩放比例,过大可能导致 Bot 无法发送 | +| ACGNSHOW_SHOW_DETAILS_HTML_IMG_COUNT | 否 | 2 | 每一张 HTML 详情信息图片中包含的图片个数,过大可能导致 Bot 无法发送 | \ No newline at end of file diff --git a/nonebot_plugin_acgnshow/__init__.py b/nonebot_plugin_acgnshow/__init__.py index 3c2abde..c9d8c43 100644 --- a/nonebot_plugin_acgnshow/__init__.py +++ b/nonebot_plugin_acgnshow/__init__.py @@ -15,6 +15,7 @@ usage = """命令格式: (如北京,福建,平顶山,绍兴,香港...,或海外/全国) 展览详情 +获取指定展览ID的详细信息 其中ID为展览列表处返回的ID 示例: diff --git a/nonebot_plugin_acgnshow/acgnapis.py b/nonebot_plugin_acgnshow/acgnapis.py index 81a189b..b1a430d 100644 --- a/nonebot_plugin_acgnshow/acgnapis.py +++ b/nonebot_plugin_acgnshow/acgnapis.py @@ -110,6 +110,11 @@ def process_show_details_data_to_template(show_details_data: dict): else: guests = "" + desc = data["performance_desc"]["list"] + for item in desc: + if item.get("module") == "activity_content": + details_html = item.get("details", "") + # 构建返回的字典 item_dict = { "banner_url": banner_url, @@ -123,10 +128,11 @@ def process_show_details_data_to_template(show_details_data: dict): "guests": guests, "is_refund": is_refund, "id_bind": id_bind, - "has_eticket": has_eticket + "has_eticket": has_eticket, + "details_html": details_html } - return item_dict + return [item_dict, details_html] def process_shows_data_to_template(shows_data: dict): showlist = [] diff --git a/nonebot_plugin_acgnshow/acgnshower.py b/nonebot_plugin_acgnshow/acgnshower.py index 4600cef..dbf0369 100644 --- a/nonebot_plugin_acgnshow/acgnshower.py +++ b/nonebot_plugin_acgnshow/acgnshower.py @@ -3,7 +3,7 @@ import traceback from nonebot.typing import T_State from typing import Optional from .acgnapis import * -from nonebot_plugin_htmlrender import template_to_pic +from nonebot_plugin_htmlrender import template_to_pic, html_to_pic from nonebot_plugin_alconna import on_alconna from nonebot_plugin_alconna.uniseg import UniMessage from arclet.alconna import Alconna, Args @@ -48,9 +48,9 @@ async def get_show_details_cmd( if show_details["errno"] != 0: await UniMessage("发生错误").send() ; return try: show_details_data = process_show_details_data_to_template(show_details) - print(show_details_data) + #print(show_details_data) template = { - "show": show_details_data, + "show": show_details_data[0], "bgimage": choose_random_bgimage(), } pic = await template_to_pic(str(RES_PATH), DETAILS_TEMPLATE_NAME, template) @@ -59,6 +59,15 @@ async def get_show_details_cmd( traceback.print_exc() return await UniMessage.image(raw=pic).send() + if config.acgnshow_send_show_details_html: + details_html_fragments = split_html_into_fragments(add_https_to_urls(show_details_data[1])) + details_html_groups = join_fragments_in_groups(details_html_fragments, config.acgnshow_show_details_html_img_count) + #print(details_html_groups) + #print(details_html) + for html in details_html_groups: + html_pic = await html_to_pic(html=html, device_scale_factor=config.acgnshow_show_details_html_scale) + #print(html_pic) + await UniMessage.image(raw=html_pic).send() @showcmd.handle() async def find_shows_cmd( diff --git a/nonebot_plugin_acgnshow/config.py b/nonebot_plugin_acgnshow/config.py index 7da792f..3b0c0ea 100644 --- a/nonebot_plugin_acgnshow/config.py +++ b/nonebot_plugin_acgnshow/config.py @@ -11,5 +11,8 @@ BGIMAGE_PATH = RES_PATH / "bgimage" class ConfigModel(BaseModel): acgnshow_pagesize: int = 8 acgnshow_bgimage_path: str = BGIMAGE_PATH + acgnshow_send_show_details_html: bool = False + acgnshow_show_details_html_scale: float = 0.6 + acgnshow_show_details_html_img_count: int = 2 config: ConfigModel = get_plugin_config(ConfigModel) diff --git a/nonebot_plugin_acgnshow/res/details.html b/nonebot_plugin_acgnshow/res/details.html index bc3fe67..d15a4b6 100644 --- a/nonebot_plugin_acgnshow/res/details.html +++ b/nonebot_plugin_acgnshow/res/details.html @@ -69,7 +69,7 @@ diff --git a/nonebot_plugin_acgnshow/util.py b/nonebot_plugin_acgnshow/util.py index 6d3e681..7bf1d6f 100644 --- a/nonebot_plugin_acgnshow/util.py +++ b/nonebot_plugin_acgnshow/util.py @@ -1,4 +1,5 @@ import os +import re import random import datetime import json @@ -19,10 +20,57 @@ def choose_random_bgimage() -> str: def convert_timestamp(timestamp) -> str: """ 将时间戳转换为日期格式 + + :param timestamp: unix 时间戳 + :return: yyyy-mm-dd hh:mm:ss时间 """ return datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") def extract_banner_url(value) -> str: a = json.loads(value) url = "https:"+a["banner"]["url"] - return url \ No newline at end of file + return url + +def add_https_to_urls(html_content): + """ + 为 HTML 内容中的缺失 https: 前缀的 URL 添加 https: 前缀 + + :param html_content: 包含 HTML 的字符串 + :return: 修正后的 HTML 字符串 + """ + # 使用正则表达式查找所有以 "//" 开头的 URL + updated_html_content = re.sub(r'(?<=src=["\'])//', 'https://', html_content) + return updated_html_content + +def split_html_into_fragments(html_content): + """ + 将 HTML 内容按照元素分割成多个片段,并存储在列表中 + + :param html_content: 包含 HTML 的字符串 + :return: 存储 HTML 片段的列表 + """ + # 使用正则表达式匹配 HTML 标签及其内容 + pattern = re.compile(r'(<[^>]+>[^<]*<\/[^>]+>|<[^>]+\/>|<[^>]+>)') + fragments = pattern.findall(html_content) + return fragments + +def join_fragments_in_groups(fragments, image_count=2): + """ + + :param fragments: 存储 HTML 片段的列表 + :param image_count: 每个组包含的图片数量,默认为2 + :return: 拼接后的 HTML 列表 + """ + grouped_html = [] + count = 0 + buffer = "" + for group in fragments: + buffer += group + if "img" in group: + count += 1 # 发现图片则计数器+1 + if count >= image_count: + grouped_html.append(buffer) + count = 0 + buffer = "" # 初始化计数器和缓冲区 + grouped_html.append(buffer)# 把缓冲区剩余内容一起添加 + return grouped_html \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 35e4a15..3875715 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-plugin-acgnshow" -version = "0.2.2" +version = "0.2.3" description = "Nonebot2插件,从哔哩哔哩会员购获取简易展览数据" readme = "README.md" requires-python = "<4.0,>=3.9"