From 643bdd8984832a12b29e5d2f796c8aea2e985eec Mon Sep 17 00:00:00 2001 From: "Chang Qian (Fred)" <8153358+slackingfred@users.noreply.github.com> Date: Sat, 5 Jul 2025 19:02:00 -0700 Subject: [PATCH] Improve stability of WS connection with WXGF decoder app (#101) * In case TYPE_LINK is missing a title, use url as title to avoid aborting * Retry WS connection on broken pipe * Update render.py * Update wxgf.py --------- Co-authored-by: Yuxin Wu --- wechat/render.py | 6 +++++- wechat/wxgf.py | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/wechat/render.py b/wechat/render.py index 013437e..9b8a2b2 100644 --- a/wechat/render.py +++ b/wechat/render.py @@ -180,7 +180,11 @@ class HTMLRender(object): pq = PyQuery(msg.content_xml_ready) url = pq('url').text() if url: - title = pq('title')[0].text + try: + title = pq('title')[0].text + except Exception as e: + logger.warning('No title found in LINK message: ' + str(e)) + title = url content = '{1}'.format(url, title) format_dict['content'] = content return template.format(**format_dict) diff --git a/wechat/wxgf.py b/wechat/wxgf.py index 682e6e8..a08bfc2 100644 --- a/wechat/wxgf.py +++ b/wechat/wxgf.py @@ -17,6 +17,7 @@ class WxgfAndroidDecoder: if "://" not in server: server = "ws://" + server logger.info(f"Connecting to {server} ...") + self.server = server self.ws = create_connection(server) def __del__(self): @@ -28,8 +29,19 @@ class WxgfAndroidDecoder: def decode(self, data: bytes) -> bytes | None: assert data[:4] == WXGF_HEADER, data[:20] - self.ws.send(data, opcode=0x2) - res = self.ws.recv() + try: + self.ws.send(data, opcode=0x2) + except BrokenPipeError as e: + logger.warning(f'Failed to send data to wxgf service. {e}. Reconnecting ..') + self.ws = create_connection(self.server) + self.ws.send(data, opcode=0x2) + try: + res = self.ws.recv() + except Exception as e: + logger.warning(f'Failed to recv data to wxgf service. {e}. Reconnecting ..') + self.ws = create_connection(self.server) + self.ws.send(data, opcode=0x2) + res = self.ws.recv() if res == FAILURE_MESSAGE: return None return res