From ee2e07717ff032dbe3d254d5f642063910b3c2ec Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 6 Aug 2024 20:46:56 +0800 Subject: [PATCH] :bug: Fix safe cut post text --- plugins/admin/post.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/plugins/admin/post.py b/plugins/admin/post.py index de83cdd..8557df6 100644 --- a/plugins/admin/post.py +++ b/plugins/admin/post.py @@ -1,5 +1,6 @@ import math import os +import re from asyncio import create_subprocess_shell, subprocess from functools import partial from typing import List, Optional, Tuple, TYPE_CHECKING, Union, Dict @@ -199,6 +200,18 @@ class Post(Plugin.Conversation): post_text += f"{escape_markdown(soup.get_text(), version=2)}\n" return post_text.strip(), too_long + @staticmethod + def safe_cut(text: str, length: int) -> str: + text = text[:length] + right_pattern = r"\[.*?\]\(.*?\)" + error_pattern = r"\[.*?\]" + right_length = re.findall(right_pattern, text) + error_length = re.findall(error_pattern, text) + if right_length == error_length: + return text + error_index = text.rindex(error_length[-1]) + return text[:error_index] + @staticmethod def input_media( media: "ArtworkImage", *args, **kwargs @@ -359,10 +372,11 @@ class Post(Plugin.Conversation): post_soup = BeautifulSoup(post_info.content, features="html.parser") post_text, too_long = self.parse_post_text(post_soup, post_subject) url = post_info.get_url(self.short_name) + max_len = MessageLimit.CAPTION_LENGTH - 100 + if too_long or len(post_text) >= max_len: + post_text = self.safe_cut(post_text, max_len) + await message.reply_text(f"警告!图片字符描述已经超过 {max_len} 个字,已经切割") post_text += f"\n\n[source]({url})" - if too_long or len(post_text) >= MessageLimit.CAPTION_LENGTH: - post_text = post_text[: MessageLimit.CAPTION_LENGTH] - await message.reply_text(f"警告!图片字符描述已经超过 {MessageLimit.CAPTION_LENGTH} 个字,已经切割") if post_info.video_urls: await message.reply_text("检测到视频,需要单独下载,视频链接:" + "\n".join(post_info.video_urls)) try: