-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import base64 | ||
import re | ||
import requests | ||
|
||
|
||
def url_to_base64(image_url): | ||
response = requests.get(image_url) | ||
image_data = response.content | ||
base64_data = base64.b64encode(image_data).decode("utf-8") | ||
return base64_data | ||
|
||
|
||
def process_message(original_message): | ||
# 使用正则表达式提取图片URL | ||
url_match = re.search( | ||
r"\[CQ:image,file=\w+\.image,url=([^\]]+)\]", original_message | ||
) | ||
|
||
if url_match: | ||
image_url = url_match.group(1) | ||
|
||
# 将图片URL转换为Base64 | ||
base64_image = url_to_base64(image_url) | ||
|
||
# 构建新的消息 | ||
new_message = re.sub( | ||
r"\[CQ:image,file=\w+\.image,url=([^\]]+)\]", | ||
f"[CQ:image,file=base64://{base64_image}]", | ||
original_message, | ||
) | ||
|
||
return new_message | ||
else: | ||
return original_message | ||
|
||
|
||
# 覆盖一个txt文件 | ||
with open("test.txt", "w", encoding="utf-8") as f: | ||
f.write(process_message(input())) |