支持解析coze.cn的图片链接

This commit is contained in:
wangxyd
2024-06-24 17:57:48 +08:00
parent 7eed6c249b
commit 423c3bec64
2 changed files with 35 additions and 24 deletions

View File

@@ -1,10 +1,21 @@
一款优化coze-discord-proxy返回结果的chatgpt-on-wechat插件 一款优化Coze返回结果中的图片和网址链接的chatgpt-on-wechat插件,包含两个功能:
安装方法: + 提取Coze包括coze.com和coze.cn返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片
+ 去掉每行结尾的Markdown链接中网址部分的小括号避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。
**安装方法:**
```sh ```sh
#installp https://github.com/wangxyd/nicecoze.git #installp https://github.com/wangxyd/nicecoze.git
#scanp #scanp
``` ```
配置方法:无需任何配置! **配置方法:**
无需任何配置!
**更新方法:**
```sh
#updatep nicecoze
```

View File

@@ -12,8 +12,8 @@ from plugins import *
name="NiceCoze", name="NiceCoze",
desire_priority=66, desire_priority=66,
hidden=False, hidden=False,
desc="优化coze-discord-proxy的返回结果。", desc="优化Coze返回结果中的图片和网址链接",
version="1.2", version="1.3",
author="空心菜", author="空心菜",
) )
class NiceCoze(Plugin): class NiceCoze(Plugin):
@@ -36,8 +36,9 @@ class NiceCoze(Plugin):
# 避免图片无法下载时,重复调用插件导致没有响应的问题 # 避免图片无法下载时,重复调用插件导致没有响应的问题
if content.startswith("[DOWNLOAD_ERROR]"): if content.startswith("[DOWNLOAD_ERROR]"):
return return
# 提取CDP返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片 # 提取Coze返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片
if all(x in content for x in ['![', 'http']) and any(x in content for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']): #if all(x in content for x in ['![', 'http']) and any(x in content for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
if 'http' in content and any(x in content for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
logger.debug(f"[Nicecoze] starting decorate_markdown_image, content={content}") logger.debug(f"[Nicecoze] starting decorate_markdown_image, content={content}")
replies = self.decorate_markdown_image(content) replies = self.decorate_markdown_image(content)
if replies: if replies:
@@ -61,29 +62,29 @@ class NiceCoze(Plugin):
e_context.action = EventAction.CONTINUE e_context.action = EventAction.CONTINUE
def decorate_markdown_image(self, content): def decorate_markdown_image(self, content):
# 完全匹配Coze画图的Markdown图片 # 完全匹配Coze画图的Markdown图片coze.com对应ciciai.comcoze.cn对应coze.cn
markdown_image_ciciai = r"([\S\s]*)\!?\[(?P<image_name>.*)\]\((?P<image_url>https\:\/\/\S+?\.ciciai\.com\/[\S]*\.png(\?[\S]*)?)\)([\S\s]*)" markdown_image_official = r"([\S\s]*)\!?\[(?P<image_name>.*)\]\((?P<image_url>https\:\/\/\S+?\.(ciciai\.com|coze\.cn)\/[\S]*\.png(\?[\S]*)?)\)([\S\s]*)"
match_obj_ciciai = re.fullmatch(markdown_image_ciciai, content) match_obj_official = re.fullmatch(markdown_image_official, content)
if match_obj_ciciai and match_obj_ciciai.group('image_url'): if match_obj_official and match_obj_official.group('image_url'):
image_name, image_url = match_obj_ciciai.group('image_name'), match_obj_ciciai.group('image_url') image_name, image_url = match_obj_official.group('image_name'), match_obj_official.group('image_url')
logger.info(f"[Nicecoze] markdown_image_ciciai found, image_name={image_name}, image_url={image_url}") logger.info(f"[Nicecoze] markdown_image_official found, image_name={image_name}, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url) reply = Reply(ReplyType.IMAGE_URL, image_url)
return [reply] return [reply]
# 完全匹配一张Markdown图片格式`![name](url)` # 完全匹配一张Markdown图片格式`![name](url)`
markdown_image1 = r"\!\[(?P<image_name>.*)\]\((?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\)" markdown_image_single = r"\!\[(?P<image_name>.*)\]\((?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\)"
match_obj1 = re.fullmatch(markdown_image1, content, re.DOTALL) match_obj_single = re.fullmatch(markdown_image_single, content, re.DOTALL)
if match_obj1 and match_obj1.group('image_url'): if match_obj_single and match_obj_single.group('image_url'):
image_name, image_url = match_obj1.group('image_name'), match_obj1.group('image_url') image_name, image_url = match_obj_single.group('image_name'), match_obj_single.group('image_url')
logger.info(f"[Nicecoze] markdown_image1 found, image_name={image_name}, image_url={image_url}") logger.info(f"[Nicecoze] markdown_image_single found, image_name={image_name}, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url) reply = Reply(ReplyType.IMAGE_URL, image_url)
return [reply] return [reply]
# 匹配多张Markdown图片(格式:`url\n![Image](url)`) # 匹配多张Markdown图片(格式:`url\n![Image](url)`)
markdown_image2 = r"(?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\n*\!\[Image\]\((?P=image_url)\)" markdown_image_multi = r"(?P<image_url>https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\n*\!\[Image\]\((?P=image_url)\)"
match_iter2 = re.finditer(markdown_image2, content) match_iter_multi = re.finditer(markdown_image_multi, content)
replies = [] replies = []
for match in match_iter2: for match in match_iter_multi:
image_url = match.group('image_url') image_url = match.group('image_url')
logger.info(f"[Nicecoze] markdown_image2 found, image_url={image_url}") logger.info(f"[Nicecoze] markdown_image_multi found, image_url={image_url}")
reply = Reply(ReplyType.IMAGE_URL, image_url) reply = Reply(ReplyType.IMAGE_URL, image_url)
replies.append(reply) replies.append(reply)
if replies: if replies:
@@ -92,5 +93,4 @@ class NiceCoze(Plugin):
logger.info(f"[Nicecoze] it seems markdown image in the content but not matched, content={content}.") logger.info(f"[Nicecoze] it seems markdown image in the content but not matched, content={content}.")
def get_help_text(self, **kwargs): def get_help_text(self, **kwargs):
return "优化coze-discord-proxy的返回结果。" return "优化Coze返回结果中的图片和网址链接"