WeChatMsg/app/util/compress_content.py

84 lines
2.3 KiB
Python
Raw Normal View History

2023-12-11 22:49:17 +08:00
import html
import xml.etree.ElementTree as ET
import lz4
def decompress_CompressContent(data):
"""
解压缩MsgCompressContent内容
:param data:
:return:
"""
if data is None or not isinstance(data, bytes):
return ''
try:
dst = lz4.block.decompress(data, uncompressed_size=len(data) << 10)
decoded_string = dst.decode().replace('\x00', '') # Remove any null characters
2023-12-26 23:49:53 +08:00
except :
2023-12-11 22:49:17 +08:00
print("Decompression failed: potentially corrupt input or insufficient buffer size.")
return ''
return decoded_string
2023-12-26 23:49:53 +08:00
2023-12-11 22:49:17 +08:00
def escape_js_and_html(input_str):
# 转义HTML特殊字符
html_escaped = html.escape(input_str, quote=False)
# 手动处理JavaScript转义字符
js_escaped = (
html_escaped
.replace("\\", "\\\\")
.replace("'", r"\'")
.replace('"', r'\"')
.replace("\n", r'\n')
.replace("\r", r'\r')
.replace("\t", r'\t')
)
return js_escaped
2023-12-26 23:49:53 +08:00
2023-12-11 22:49:17 +08:00
def parser_reply(data: bytes):
xml_content = decompress_CompressContent(data)
if not xml_content:
return {
'type': 57,
'title': "发生错误",
'refer': {
'type': '1',
'content': '引用错误',
'displayname': '用户名',
2023-12-20 18:19:17 +08:00
},
"is_error": True
2023-12-11 22:49:17 +08:00
}
2023-12-17 18:43:37 +08:00
try:
root = ET.XML(xml_content)
appmsg = root.find('appmsg')
msg_type = int(appmsg.find('type').text)
title = appmsg.find('title').text
refermsg_content = appmsg.find('refermsg').find('content').text
refermsg_type = int(appmsg.find('refermsg').find('type').text)
refermsg_displayname = appmsg.find('refermsg').find('displayname').text
return {
'type': msg_type,
2023-12-20 18:19:17 +08:00
'title': title,
'refer': None if refermsg_type != 1 else {
'type': refermsg_type,
2023-12-20 18:19:17 +08:00
'content': refermsg_content.lstrip("\n"),
'displayname': refermsg_displayname,
},
"is_error": False
}
except:
return {
'type': 57,
'title': "发生错误",
'refer': {
'type': '1',
'content': '引用错误',
'displayname': '用户名',
2023-12-20 18:19:17 +08:00
},
"is_error": True
2023-12-11 22:49:17 +08:00
}