mirror of
https://github.com/LC044/WeChatMsg
synced 2024-11-15 06:11:19 +08:00
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
import html
|
||
import xml.etree.ElementTree as ET
|
||
|
||
import lz4
|
||
|
||
|
||
def decompress_CompressContent(data):
|
||
"""
|
||
解压缩Msg:CompressContent内容
|
||
: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
|
||
except lz4.block.LZ4BlockError:
|
||
print("Decompression failed: potentially corrupt input or insufficient buffer size.")
|
||
return ''
|
||
return decoded_string
|
||
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
|
||
|
||
def parser_reply(data: bytes):
|
||
xml_content = decompress_CompressContent(data)
|
||
if not xml_content:
|
||
return {
|
||
'type': 57,
|
||
'title': "发生错误",
|
||
'refer': {
|
||
'type': '1',
|
||
'content': '引用错误',
|
||
'displayname': '用户名',
|
||
},
|
||
"is_error": True
|
||
}
|
||
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,
|
||
'title': title,
|
||
'refer': None if refermsg_type != 1 else {
|
||
'type': refermsg_type,
|
||
'content': refermsg_content.lstrip("\n"),
|
||
'displayname': refermsg_displayname,
|
||
},
|
||
"is_error": False
|
||
}
|
||
except:
|
||
return {
|
||
'type': 57,
|
||
'title': "发生错误",
|
||
'refer': {
|
||
'type': '1',
|
||
'content': '引用错误',
|
||
'displayname': '用户名',
|
||
},
|
||
"is_error": True
|
||
}
|