Skip to content

Commit

Permalink
👀v0.2.3,添加3个配置项目,展览详情支持发送活动介绍栏目内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Asankilp committed Aug 30, 2024
1 parent ffc28c9 commit 32f067c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ _✨ 从哔哩哔哩会员购获取展览简易信息 ✨_
| 配置项 | 必填 | 默认值 | 说明 |
| :---------------: | :--: | :----: | :----------------------------------------------------------: |
| ACGNSHOW_PAGESIZE || 8 | 单个图片的条目数,最大为 20,条目数过大可能导致 Bot 无法发送 |
| ACGNSHOW_BGIMAGE_PATH || 插件内置背景图 | 插件返回图片的背景图目录路径 |
| 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 无法发送 |
1 change: 1 addition & 0 deletions nonebot_plugin_acgnshow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
(如北京,福建,平顶山,绍兴,香港...,或海外/全国)
展览详情 <ID>
获取指定展览ID的详细信息
其中ID为展览列表处返回的ID
示例:
Expand Down
10 changes: 8 additions & 2 deletions nonebot_plugin_acgnshow/acgnapis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = []
Expand Down
15 changes: 12 additions & 3 deletions nonebot_plugin_acgnshow/acgnshower.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions nonebot_plugin_acgnshow/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
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)
2 changes: 1 addition & 1 deletion nonebot_plugin_acgnshow/res/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h3>{{ screen_name }}</h3>
<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>
Expand Down
50 changes: 49 additions & 1 deletion nonebot_plugin_acgnshow/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import random
import datetime
import json
Expand All @@ -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
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 32f067c

Please sign in to comment.