mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-acgnshow.git
synced 2025-09-06 12:16:25 +00:00
😊优化代码结构,统一图片主题,优化总体质感,非常完美
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
import os
|
||||
import re
|
||||
import random
|
||||
import datetime
|
||||
@ -7,30 +6,58 @@ from pathlib import Path
|
||||
from .config import config
|
||||
|
||||
|
||||
bgpath: Path = Path(config.acgnshow_bgimage_path)
|
||||
bg_list: list[Path] = [
|
||||
f
|
||||
for f in bgpath.iterdir()
|
||||
if f.suffix
|
||||
in (
|
||||
".jpg",
|
||||
".png",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".webp",
|
||||
".bmp",
|
||||
".svg",
|
||||
".ico",
|
||||
".tiff",
|
||||
".tif",
|
||||
".jfif",
|
||||
".jpe",
|
||||
".jif",
|
||||
".jfi",
|
||||
".jfif",
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def choose_random_bgimage() -> str:
|
||||
"""
|
||||
从背景图片文件夹中随机选择一张图片,返回图片的uri地址
|
||||
从背景图片文件夹中随机选择一张图片,返回图片的file协议uri地址
|
||||
"""
|
||||
bgpath = Path(config.acgnshow_bgimage_path)
|
||||
randomfile = random.choice(os.listdir(bgpath))
|
||||
randomurl = (bgpath / randomfile).as_uri()
|
||||
return randomurl
|
||||
# randomfile = random.choice(os.listdir(bgpath)) # 天哪
|
||||
|
||||
return random.choice(bg_list).as_uri()
|
||||
|
||||
|
||||
def convert_timestamp(timestamp) -> str:
|
||||
def convert_timestamp(timestamp, strformat: str = "%Y-%m-%d %H:%M:%S") -> str:
|
||||
"""
|
||||
将时间戳转换为日期格式
|
||||
|
||||
:param timestamp: unix 时间戳
|
||||
:param strformat: 日期格式,默认为 "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
:return: yyyy-mm-dd hh:mm:ss时间
|
||||
"""
|
||||
return datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
return datetime.datetime.fromtimestamp(timestamp).strftime(strformat)
|
||||
|
||||
|
||||
def extract_banner_url(value) -> str:
|
||||
a = json.loads(value)
|
||||
url = "https:"+a["banner"]["url"]
|
||||
url = "https:" + a["banner"]["url"]
|
||||
return url
|
||||
|
||||
|
||||
def add_https_to_urls(html_content):
|
||||
"""
|
||||
为 HTML 内容中的缺失 https: 前缀的 URL 添加 https: 前缀
|
||||
@ -39,9 +66,10 @@ def add_https_to_urls(html_content):
|
||||
:return: 修正后的 HTML 字符串
|
||||
"""
|
||||
# 使用正则表达式查找所有以 "//" 开头的 URL
|
||||
updated_html_content = re.sub(r'(?<=src=["\'])//', 'https://', html_content)
|
||||
updated_html_content = re.sub(r'(?<=src=["\'])//', "https://", html_content)
|
||||
return updated_html_content
|
||||
|
||||
|
||||
def split_html_into_fragments(html_content):
|
||||
"""
|
||||
将 HTML 内容按照元素分割成多个片段,并存储在列表中
|
||||
@ -50,10 +78,11 @@ def split_html_into_fragments(html_content):
|
||||
:return: 存储 HTML 片段的列表
|
||||
"""
|
||||
# 使用正则表达式匹配 HTML 标签及其内容
|
||||
pattern = re.compile(r'(<[^>]+>[^<]*<\/[^>]+>|<[^>]+\/>|<[^>]+>)')
|
||||
pattern = re.compile(r"(<[^>]+>[^<]*<\/[^>]+>|<[^>]+\/>|<[^>]+>)")
|
||||
fragments = pattern.findall(html_content)
|
||||
return fragments
|
||||
|
||||
|
||||
def join_fragments_in_groups(fragments, image_count=2):
|
||||
"""
|
||||
|
||||
@ -67,10 +96,10 @@ def join_fragments_in_groups(fragments, image_count=2):
|
||||
for group in fragments:
|
||||
buffer += group
|
||||
if "img" in group:
|
||||
count += 1 # 发现图片则计数器+1
|
||||
count += 1 # 发现图片则计数器+1
|
||||
if count >= image_count:
|
||||
grouped_html.append(buffer)
|
||||
count = 0
|
||||
buffer = "" # 初始化计数器和缓冲区
|
||||
grouped_html.append(buffer)# 把缓冲区剩余内容一起添加
|
||||
return grouped_html
|
||||
buffer = "" # 初始化计数器和缓冲区
|
||||
grouped_html.append(buffer) # 把缓冲区剩余内容一起添加
|
||||
return grouped_html
|
||||
|
Reference in New Issue
Block a user