[patch] 优化DY日志结构 降低冗余代码
This commit is contained in:
@@ -1022,20 +1022,22 @@ class DouYinMessageHandler:
|
|||||||
try:
|
try:
|
||||||
message = blackboxprotobuf.decode_message(content)[0]
|
message = blackboxprotobuf.decode_message(content)[0]
|
||||||
|
|
||||||
# 解析二进制数据
|
# 🔥 优化:只记录有效消息类型,过滤无用日志
|
||||||
decoded_message_readable = self.decode_binary_data(message)
|
# 首先检查是否是心跳响应(优先处理,减少不必要的解析)
|
||||||
print(f"\n📨 收到消息: {decoded_message_readable}")
|
if "5" in message:
|
||||||
|
kv_pairs = message["5"]
|
||||||
|
if isinstance(kv_pairs, list):
|
||||||
|
for pair in kv_pairs:
|
||||||
|
if isinstance(pair, dict) and pair.get("1") == b"code" and pair.get("2") == b"0":
|
||||||
|
self._log("💓 心跳响应正常", "DEBUG")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 解析用户Token(静默处理)
|
||||||
user, token = self.code_parse(message=message)
|
user, token = self.code_parse(message=message)
|
||||||
if user and token:
|
if user and token:
|
||||||
receiver_id = int(user.split(":")[0])
|
receiver_id = int(user.split(":")[0])
|
||||||
self._init_user_info(receiver_id, token)
|
self._init_user_info(receiver_id, token)
|
||||||
|
self._log(f"✅ 获取到用户Token: 用户ID={receiver_id}", "DEBUG")
|
||||||
print(f"✅ 获取到用户Token: 用户ID={receiver_id}")
|
|
||||||
|
|
||||||
# 获取消息类型
|
|
||||||
msg_type = message.get('1')
|
|
||||||
print(f"📋 消息类型: {msg_type}")
|
|
||||||
|
|
||||||
# 获取内部消息结构
|
# 获取内部消息结构
|
||||||
inner_msg = message.get('8', {})
|
inner_msg = message.get('8', {})
|
||||||
@@ -1044,48 +1046,37 @@ class DouYinMessageHandler:
|
|||||||
if isinstance(inner_msg, bytes):
|
if isinstance(inner_msg, bytes):
|
||||||
try:
|
try:
|
||||||
inner_msg = blackboxprotobuf.decode_message(inner_msg)[0]
|
inner_msg = blackboxprotobuf.decode_message(inner_msg)[0]
|
||||||
print(f"🔄 解析内部消息字节成功")
|
except Exception:
|
||||||
except Exception as e:
|
# 无法解析的消息静默忽略
|
||||||
print(f"⚠️ 无法解析内部消息字节: {e}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if not inner_msg or not isinstance(inner_msg, dict):
|
if not inner_msg or not isinstance(inner_msg, dict):
|
||||||
print("⚠️ 消息结构不完整")
|
# 消息结构不完整,静默忽略
|
||||||
return
|
return
|
||||||
|
|
||||||
# 获取内部消息类型
|
# 获取内部消息类型
|
||||||
inner_type = inner_msg.get('1')
|
inner_type = inner_msg.get('1')
|
||||||
inner_data = inner_msg.get('6', {})
|
inner_data = inner_msg.get('6', {})
|
||||||
|
|
||||||
# 首先检查是否是心跳响应
|
# 🔥 只处理和记录有意义的消息类型
|
||||||
if "5" in message:
|
|
||||||
kv_pairs = message["5"]
|
|
||||||
if isinstance(kv_pairs, list):
|
|
||||||
for pair in kv_pairs:
|
|
||||||
if isinstance(pair, dict) and pair.get("1") == b"code" and pair.get("2") == b"0":
|
|
||||||
self._log("💓 心跳响应正常", "DEBUG")
|
|
||||||
return
|
|
||||||
print(f"📋 内部消息类型: {inner_type}")
|
|
||||||
print(f"📋 内部消息数据键: {list(inner_data.keys()) if isinstance(inner_data, dict) else type(inner_data)}")
|
|
||||||
|
|
||||||
# 处理不同类型的消息
|
|
||||||
if inner_type == 610: # Token消息
|
if inner_type == 610: # Token消息
|
||||||
print("🔑 处理Token消息")
|
self._log("🔑 处理Token消息", "DEBUG")
|
||||||
self._handle_token_message(inner_data)
|
self._handle_token_message(inner_data)
|
||||||
|
|
||||||
elif inner_type == 500: # 聊天消息
|
elif inner_type == 500: # 聊天消息(真正的用户消息)
|
||||||
print("💬 处理聊天消息")
|
# 🔥 只有聊天消息才打印详细日志
|
||||||
|
decoded_message_readable = self.decode_binary_data(message)
|
||||||
|
self._log(f"💬 收到用户消息: {decoded_message_readable}", "INFO")
|
||||||
self._handle_chat_message(inner_data, message)
|
self._handle_chat_message(inner_data, message)
|
||||||
|
|
||||||
elif inner_type == 200: # 心跳消息
|
elif inner_type == 200: # 心跳消息
|
||||||
print("💓 收到心跳消息")
|
self._log("💓 收到心跳消息", "DEBUG")
|
||||||
|
|
||||||
else:
|
# 🔥 其他未知消息类型静默忽略,不打印日志
|
||||||
print(f"❓ 未知内部消息类型: {inner_type}")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"❌ 解析消息时出错: {e}")
|
# 只记录真正的错误,忽略解析失败
|
||||||
traceback.print_exc()
|
self._log(f"❌ 处理消息时出错: {e}", "ERROR")
|
||||||
|
|
||||||
async def _process_pending_message(self, sender_id, message_content):
|
async def _process_pending_message(self, sender_id, message_content):
|
||||||
"""处理待发送的消息"""
|
"""处理待发送的消息"""
|
||||||
@@ -1295,8 +1286,6 @@ class DouYinMessageHandler:
|
|||||||
def _extract_message_text(self, msg_content):
|
def _extract_message_text(self, msg_content):
|
||||||
"""从消息内容中提取文本和图片URL"""
|
"""从消息内容中提取文本和图片URL"""
|
||||||
try:
|
try:
|
||||||
print("🔍 开始提取消息内容...")
|
|
||||||
|
|
||||||
# 初始化返回结果
|
# 初始化返回结果
|
||||||
result = {
|
result = {
|
||||||
'text': None,
|
'text': None,
|
||||||
@@ -1311,27 +1300,22 @@ class DouYinMessageHandler:
|
|||||||
# 方法1: 处理文本消息(从'8'字段获取)
|
# 方法1: 处理文本消息(从'8'字段获取)
|
||||||
text_content = msg_content.get('8')
|
text_content = msg_content.get('8')
|
||||||
if text_content:
|
if text_content:
|
||||||
print(f"📄 检测到文本内容")
|
|
||||||
if isinstance(text_content, bytes):
|
if isinstance(text_content, bytes):
|
||||||
try:
|
try:
|
||||||
decoded = text_content.decode('utf-8')
|
decoded = text_content.decode('utf-8')
|
||||||
result['text'] = decoded
|
result['text'] = decoded
|
||||||
self._log(f"✅ UTF-8解码成功: {decoded}", "SUCCESS")
|
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
try:
|
try:
|
||||||
decoded = text_content.decode('gbk')
|
decoded = text_content.decode('gbk')
|
||||||
result['text'] = decoded
|
result['text'] = decoded
|
||||||
self._log(f"✅ GBK解码成功: {decoded}", "SUCCESS")
|
|
||||||
except:
|
except:
|
||||||
self._log("⚠️ 无法解码文本内容", "DEBUG")
|
pass
|
||||||
elif isinstance(text_content, str):
|
elif isinstance(text_content, str):
|
||||||
result['text'] = text_content
|
result['text'] = text_content
|
||||||
self._log(f"✅ 直接使用文本内容: {text_content}")
|
|
||||||
|
|
||||||
# 方法2: 统一处理'9'字段中的数据(包括文本和图片、消息、视频
|
# 方法2: 统一处理'9'字段中的数据(包括文本和图片、消息、视频
|
||||||
meta_data = msg_content.get('9', [])
|
meta_data = msg_content.get('9', [])
|
||||||
if meta_data:
|
if meta_data:
|
||||||
print(f"🔍 检测到消息元数据")
|
|
||||||
for item in meta_data:
|
for item in meta_data:
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
continue
|
continue
|
||||||
@@ -1344,17 +1328,13 @@ class DouYinMessageHandler:
|
|||||||
# 处理所有类型的URL
|
# 处理所有类型的URL
|
||||||
if key == 'avatar_uri':
|
if key == 'avatar_uri':
|
||||||
result['avatar'] = value
|
result['avatar'] = value
|
||||||
self._log(f"✅ 找到头像URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'imageUrl':
|
elif key == 'imageUrl':
|
||||||
result['image_url'] = value
|
result['image_url'] = value
|
||||||
self._log(f"✅ 找到图片URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'thumbnailUrl':
|
elif key == 'thumbnailUrl':
|
||||||
result['thumbnail_url'] = value
|
result['thumbnail_url'] = value
|
||||||
self._log(f"✅ 找到缩略图URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'goods_id':
|
elif key == 'goods_id':
|
||||||
goods_id = value
|
goods_id = value
|
||||||
result['goods_id'] = goods_id
|
result['goods_id'] = goods_id
|
||||||
self._log(f"✅ 找到商品ID: {goods_id}", "SUCCESS")
|
|
||||||
elif key == 'msg_render_model':
|
elif key == 'msg_render_model':
|
||||||
try:
|
try:
|
||||||
video_info = json.loads(value)
|
video_info = json.loads(value)
|
||||||
@@ -1364,22 +1344,17 @@ class DouYinMessageHandler:
|
|||||||
viedo_url = render_body.get('coverURL')
|
viedo_url = render_body.get('coverURL')
|
||||||
viedo_vid = render_body.get('vid')
|
viedo_vid = render_body.get('vid')
|
||||||
if viedo_vid and viedo_url:
|
if viedo_vid and viedo_url:
|
||||||
self._log(f"✅ 找到视频原始vid: {viedo_vid}", "SUCCESS")
|
|
||||||
|
|
||||||
# 解析获取视频播放地址
|
# 解析获取视频播放地址
|
||||||
play_url = self.parse_video(viedo_vid)
|
play_url = self.parse_video(viedo_vid)
|
||||||
if play_url:
|
if play_url:
|
||||||
result['video_url'] = play_url
|
result['video_url'] = play_url
|
||||||
else:
|
else:
|
||||||
# 如果解析失败 在打印对应的日志后 使用备用封面图片作为地址
|
# 如果解析失败,使用备用封面图片作为地址
|
||||||
if '\\u0026' in viedo_url:
|
if '\\u0026' in viedo_url:
|
||||||
viedo_url = viedo_url.replace('\\u0026', '&')
|
viedo_url = viedo_url.replace('\\u0026', '&')
|
||||||
result['video_url'] = viedo_url
|
result['video_url'] = viedo_url
|
||||||
self._log("⚠️ 使用视频封面作为备选", "WARNING")
|
except json.JSONDecodeError:
|
||||||
else:
|
pass
|
||||||
self._log("⚠️ 未找到视频核心id 可能出现新的数据存放地址", "DEBUG")
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
self._log(f"解析视频信息失败: {e}", "ERROR")
|
|
||||||
elif key == 'sku_order_id':
|
elif key == 'sku_order_id':
|
||||||
# 取到了对应的订单号 id
|
# 取到了对应的订单号 id
|
||||||
order_id = value
|
order_id = value
|
||||||
@@ -1392,7 +1367,6 @@ class DouYinMessageHandler:
|
|||||||
# 方法3: 兼容处理旧的'9-1'字段(如果存在)
|
# 方法3: 兼容处理旧的'9-1'字段(如果存在)
|
||||||
image_data = msg_content.get('9-1', []) # 如果没有拿到这个arrayList那么就不需要在对里面的数据进行解析了
|
image_data = msg_content.get('9-1', []) # 如果没有拿到这个arrayList那么就不需要在对里面的数据进行解析了
|
||||||
if image_data != []:
|
if image_data != []:
|
||||||
print(f"📸 检测到旧的图片数据格式", "DEBUG")
|
|
||||||
for item in image_data:
|
for item in image_data:
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
continue
|
continue
|
||||||
@@ -1404,17 +1378,13 @@ class DouYinMessageHandler:
|
|||||||
|
|
||||||
if key == 'avatar_uri' and not result['avatar']:
|
if key == 'avatar_uri' and not result['avatar']:
|
||||||
result['avatar'] = value
|
result['avatar'] = value
|
||||||
self._log(f"✅ 找到头像URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'imageUrl' and not result['image_url']:
|
elif key == 'imageUrl' and not result['image_url']:
|
||||||
result['image_url'] = value
|
result['image_url'] = value
|
||||||
self._log(f"✅ 找到图片URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'thumbnailUrl' and not result['thumbnail_url']:
|
elif key == 'thumbnailUrl' and not result['thumbnail_url']:
|
||||||
result['thumbnail_url'] = value
|
result['thumbnail_url'] = value
|
||||||
self._log(f"✅ 找到缩略图URL: {value}", "SUCCESS")
|
|
||||||
elif key == 'goods_id':
|
elif key == 'goods_id':
|
||||||
goods_id = value
|
goods_id = value
|
||||||
result['goods_id'] = goods_id
|
result['goods_id'] = goods_id
|
||||||
self._log(f"✅ 找到商品ID: {goods_id}", "SUCCESS")
|
|
||||||
elif key == 'msg_render_model':
|
elif key == 'msg_render_model':
|
||||||
try:
|
try:
|
||||||
video_info = json.loads(value)
|
video_info = json.loads(value)
|
||||||
@@ -1424,42 +1394,22 @@ class DouYinMessageHandler:
|
|||||||
viedo_url = render_body.get('coverURL')
|
viedo_url = render_body.get('coverURL')
|
||||||
viedo_vid = render_body.get('vid')
|
viedo_vid = render_body.get('vid')
|
||||||
if viedo_vid and viedo_url:
|
if viedo_vid and viedo_url:
|
||||||
self._log(f"✅ 找到视频原始vid: {viedo_vid}", "SUCCESS")
|
|
||||||
|
|
||||||
# 解析获取视频播放地址
|
# 解析获取视频播放地址
|
||||||
play_url = self.parse_video(viedo_vid)
|
play_url = self.parse_video(viedo_vid)
|
||||||
if play_url:
|
if play_url:
|
||||||
result['video_url'] = play_url
|
result['video_url'] = play_url
|
||||||
else:
|
else:
|
||||||
# 如果解析失败 在打印对应的日志后 使用备用封面图片作为地址
|
# 如果解析失败,使用备用封面图片作为地址
|
||||||
if '\\u0026' in viedo_url:
|
if '\\u0026' in viedo_url:
|
||||||
viedo_url = viedo_url.replace('\\u0026', '&')
|
viedo_url = viedo_url.replace('\\u0026', '&')
|
||||||
result['video_url'] = viedo_url
|
result['video_url'] = viedo_url
|
||||||
self._log("⚠️ 使用视频封面作为备选", "WARNING")
|
except json.JSONDecodeError:
|
||||||
else:
|
pass
|
||||||
self._log("⚠️ 未找到视频核心id 可能出现新的数据存放地址", "DEBUG")
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
self._log(f"解析视频信息失败: {e}", "ERROR")
|
|
||||||
elif key == 'sku_order_id':
|
elif key == 'sku_order_id':
|
||||||
# 取到了对应的订单号 id
|
# 取到了对应的订单号 id
|
||||||
order_id = value
|
order_id = value
|
||||||
result['order_id'] = order_id
|
result['order_id'] = order_id
|
||||||
|
|
||||||
# 如果都没找到内容,打印调试信息
|
|
||||||
if not any(result.values()):
|
|
||||||
print("🔍 未找到有效内容,打印所有字段:")
|
|
||||||
for key, value in msg_content.items():
|
|
||||||
if isinstance(value, bytes):
|
|
||||||
try:
|
|
||||||
decoded = value.decode('utf-8', errors='ignore')
|
|
||||||
print(f" {key}: {decoded[:100]}...")
|
|
||||||
except:
|
|
||||||
print(f" {key}: [无法解码的字节数据,长度: {len(value)}]")
|
|
||||||
elif isinstance(value, list):
|
|
||||||
print(f" {key}: [列表数据,长度: {len(value)}]")
|
|
||||||
else:
|
|
||||||
print(f" {key}: {value}")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -1478,8 +1428,6 @@ class DouYinMessageHandler:
|
|||||||
def _handle_chat_message(self, message, msg_type):
|
def _handle_chat_message(self, message, msg_type):
|
||||||
"""处理聊天消息 - 修改版本,支持多种消息类型"""
|
"""处理聊天消息 - 修改版本,支持多种消息类型"""
|
||||||
try:
|
try:
|
||||||
self._log(f"🔍 开始解析{message} 消息结构", "DEBUG")
|
|
||||||
|
|
||||||
msg_500 = message.get('500', {})
|
msg_500 = message.get('500', {})
|
||||||
if not msg_500:
|
if not msg_500:
|
||||||
return
|
return
|
||||||
@@ -1492,28 +1440,21 @@ class DouYinMessageHandler:
|
|||||||
inner_msg_type = msg_content.get("6", 0)
|
inner_msg_type = msg_content.get("6", 0)
|
||||||
sender_id = msg_content.get("7", 0)
|
sender_id = msg_content.get("7", 0)
|
||||||
|
|
||||||
self._log(f"📊 内部消息类型: {inner_msg_type}, 发送者ID: {sender_id}", "DEBUG")
|
|
||||||
|
|
||||||
print("会不会到这里来呢")
|
|
||||||
|
|
||||||
# 处理不同类型的消息
|
# 处理不同类型的消息
|
||||||
if inner_msg_type == 1000 and sender_id and sender_id != int(self.cookie['PIGEON_CID']):
|
if inner_msg_type == 1000 and sender_id and sender_id != int(self.cookie['PIGEON_CID']):
|
||||||
# 用户文本消息 - 处理AI回复
|
# 用户文本消息 - 处理AI回复
|
||||||
self._log("✅ 检测到1000状态的有效消息", "INFO")
|
self._log(f"✅ 检测到用户消息 (类型1000),发送者: {sender_id}", "INFO")
|
||||||
self._handle_user_text_message(msg_content, sender_id)
|
self._handle_user_text_message(msg_content, sender_id)
|
||||||
|
|
||||||
elif inner_msg_type == 50002 and sender_id and sender_id != int(self.cookie['PIGEON_CID']):
|
elif inner_msg_type == 50002 and sender_id and sender_id != int(self.cookie['PIGEON_CID']):
|
||||||
# 系统消息 - 触发token请求
|
# 系统消息 - 触发token请求
|
||||||
self._log("✅ 检测到50002类型的系统消息", "INFO")
|
self._log(f"✅ 检测到系统消息 (类型50002),发送者: {sender_id}", "DEBUG")
|
||||||
self._handle_system_message(msg_content, sender_id)
|
self._handle_system_message(msg_content, sender_id)
|
||||||
|
|
||||||
else:
|
# 🔥 其他消息类型静默忽略
|
||||||
# 忽略其他消息
|
|
||||||
self._log(f"🔍 忽略消息类型: {inner_msg_type}", "DEBUG")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._log(f"❌ 处理聊天消息失败: {e}", "ERROR")
|
self._log(f"❌ 处理聊天消息失败: {e}", "ERROR")
|
||||||
self._log(f"❌ 错误详情: {traceback.format_exc()}", "DEBUG")
|
|
||||||
|
|
||||||
def _handle_user_text_message(self, msg_content, sender_id):
|
def _handle_user_text_message(self, msg_content, sender_id):
|
||||||
"""处理用户文本消息"""
|
"""处理用户文本消息"""
|
||||||
|
|||||||
@@ -433,6 +433,12 @@ class BackendClient:
|
|||||||
self._send_to_backend(status_message),
|
self._send_to_backend(status_message),
|
||||||
self.loop
|
self.loop
|
||||||
)
|
)
|
||||||
|
#
|
||||||
|
# # 🔥 等待发送完成(可选,避免警告)
|
||||||
|
# try:
|
||||||
|
# future.result(timeout=2) # 最多等待2秒
|
||||||
|
# except Exception as send_error:
|
||||||
|
# self._log(f"发送状态通知异常: {send_error}", "DEBUG")
|
||||||
|
|
||||||
status_text = "连接" if connected else "断开"
|
status_text = "连接" if connected else "断开"
|
||||||
self._log(f"已通知后端GUI客户端{status_text}", "DEBUG")
|
self._log(f"已通知后端GUI客户端{status_text}", "DEBUG")
|
||||||
|
|||||||
12
config.py
12
config.py
@@ -10,13 +10,13 @@ import json # 用于将令牌保存为 JSON 格式
|
|||||||
# 后端服务器配置
|
# 后端服务器配置
|
||||||
# BACKEND_HOST = "192.168.5.233"
|
# BACKEND_HOST = "192.168.5.233"
|
||||||
# BACKEND_HOST = "192.168.5.106"
|
# BACKEND_HOST = "192.168.5.106"
|
||||||
# BACKEND_HOST = "192.168.5.12"
|
BACKEND_HOST = "192.168.5.12"
|
||||||
BACKEND_HOST = "shuidrop.com"
|
# BACKEND_HOST = "shuidrop.com"
|
||||||
# BACKEND_HOST = "test.shuidrop.com"
|
# BACKEND_HOST = "test.shuidrop.com"
|
||||||
# BACKEND_PORT = "8000"
|
BACKEND_PORT = "8000"
|
||||||
BACKEND_PORT = ""
|
# BACKEND_PORT = ""
|
||||||
# BACKEND_WS_URL = f"ws://{BACKEND_HOST}:{BACKEND_PORT}"
|
BACKEND_WS_URL = f"ws://{BACKEND_HOST}:{BACKEND_PORT}"
|
||||||
BACKEND_WS_URL = f"wss://{BACKEND_HOST}"
|
# BACKEND_WS_URL = f"wss://{BACKEND_HOST}"
|
||||||
|
|
||||||
# WebSocket配置
|
# WebSocket配置
|
||||||
WS_CONNECT_TIMEOUT = 16.0
|
WS_CONNECT_TIMEOUT = 16.0
|
||||||
|
|||||||
Reference in New Issue
Block a user