mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow.git
synced 2024-11-11 01:27:39 +08:00
👀v0.2.3,添加3个配置项目,展览详情支持发送活动介绍栏目内容
This commit is contained in:
parent
ffc28c98f5
commit
32f067c33a
@ -81,3 +81,6 @@ _✨ 从哔哩哔哩会员购获取展览简易信息 ✨_
|
||||
| :---------------: | :--: | :----: | :----------------------------------------------------------: |
|
||||
| ACGNSHOW_PAGESIZE | 否 | 8 | 单个图片的条目数,最大为 20,条目数过大可能导致 Bot 无法发送 |
|
||||
| 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 无法发送 |
|
@ -15,6 +15,7 @@ usage = """命令格式:
|
||||
(如北京,福建,平顶山,绍兴,香港...,或海外/全国)
|
||||
|
||||
展览详情 <ID>
|
||||
获取指定展览ID的详细信息
|
||||
其中ID为展览列表处返回的ID
|
||||
|
||||
示例:
|
||||
|
@ -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 = []
|
||||
|
@ -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(
|
||||
|
@ -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)
|
||||
|
@ -69,7 +69,7 @@
|
||||
<div class="footer">
|
||||
<div class="designer">Designed by Asankilp?</div>
|
||||
<div class="project_name">nonebot-plugin-acgnshow</div>
|
||||
<div class="notice_text">本页信息仅供参考,具体内容请访问展览官方详情页,并自行检索实际信息</div>
|
||||
<div class="notice_text">本页信息仅供参考,具体内容请访问哔哩哔哩会员购,并自行检索实际信息</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
import random
|
||||
import datetime
|
||||
import json
|
||||
@ -19,6 +20,9 @@ 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")
|
||||
|
||||
@ -26,3 +30,47 @@ def extract_banner_url(value) -> str:
|
||||
a = json.loads(value)
|
||||
url = "https:"+a["banner"]["url"]
|
||||
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
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user