From 9b47e2d6f922cb1caa58a40bde058d33763bfab3 Mon Sep 17 00:00:00 2001 From: lanvent Date: Wed, 26 Apr 2023 22:54:53 +0800 Subject: [PATCH 1/5] fix: output itchat error msg rightly --- lib/itchat/components/login.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/itchat/components/login.py b/lib/itchat/components/login.py index 60ca395f7..3a359ee40 100644 --- a/lib/itchat/components/login.py +++ b/lib/itchat/components/login.py @@ -195,13 +195,17 @@ def process_login_info(core, loginContent): core.loginInfo['logintime'] = int(time.time() * 1e3) core.loginInfo['BaseRequest'] = {} cookies = core.s.cookies.get_dict() - skey = re.findall('(.*?)', r.text, re.S)[0] - pass_ticket = re.findall( - '(.*?)', r.text, re.S)[0] - core.loginInfo['skey'] = core.loginInfo['BaseRequest']['Skey'] = skey + res = re.findall('(.*?)', r.text, re.S) + skey = res[0] if res else None + res = re.findall( + '(.*?)', r.text, re.S) + pass_ticket = res[0] if res else None + if skey is not None: + core.loginInfo['skey'] = core.loginInfo['BaseRequest']['Skey'] = skey core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"] core.loginInfo['wxuin'] = core.loginInfo['BaseRequest']['Uin'] = cookies["wxuin"] - core.loginInfo['pass_ticket'] = pass_ticket + if pass_ticket is not None: + core.loginInfo['pass_ticket'] = pass_ticket # A question : why pass_ticket == DeviceID ? # deviceID is only a randomly generated number From 527d5e1dbc6b554cb6fb12abde89b64000ce73c4 Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 27 Apr 2023 02:46:53 +0800 Subject: [PATCH 2/5] fix(itchat): add error log when hot reload fails and log out before logging in normally --- channel/wechat/wechat_channel.py | 21 ++++++--------------- lib/itchat/components/login.py | 2 +- lib/itchat/components/register.py | 4 +++- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index d8249a1d4..c888157f3 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -113,21 +113,12 @@ def startup(self): # login by scan QRCode hotReload = conf().get("hot_reload", False) status_path = os.path.join(get_appdata_dir(), "itchat.pkl") - try: - itchat.auto_login( - enableCmdQR=2, - hotReload=hotReload, - statusStorageDir=status_path, - qrCallback=qrCallback, - ) - except Exception as e: - if hotReload: - logger.error("Hot reload failed, try to login without hot reload") - itchat.logout() - os.remove(status_path) - itchat.auto_login(enableCmdQR=2, hotReload=hotReload, qrCallback=qrCallback) - else: - raise e + itchat.auto_login( + enableCmdQR=2, + hotReload=hotReload, + statusStorageDir=status_path, + qrCallback=qrCallback, + ) self.user_id = itchat.instance.storageClass.userName self.name = itchat.instance.storageClass.nickName logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name)) diff --git a/lib/itchat/components/login.py b/lib/itchat/components/login.py index 3a359ee40..c4d8c1ce6 100644 --- a/lib/itchat/components/login.py +++ b/lib/itchat/components/login.py @@ -367,7 +367,7 @@ def sync_check(self): regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}' pm = re.search(regx, r.text) if pm is None or pm.group(1) != '0': - logger.debug('Unexpected sync check result: %s' % r.text) + logger.error('Unexpected sync check result: %s' % r.text) return None return pm.group(2) diff --git a/lib/itchat/components/register.py b/lib/itchat/components/register.py index 78a3f0b7c..368ac3154 100644 --- a/lib/itchat/components/register.py +++ b/lib/itchat/components/register.py @@ -25,9 +25,11 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', self.useHotReload = hotReload self.hotReloadDir = statusStorageDir if hotReload: - if self.load_login_status(statusStorageDir, + if rval:=self.load_login_status(statusStorageDir, loginCallback=loginCallback, exitCallback=exitCallback): return + logger.error('hot reload failed, logging in normally, {}'.format(rval)) + self.logout() self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, loginCallback=loginCallback, exitCallback=exitCallback) self.dump_login_status(statusStorageDir) From 008178d737997194502f0ee58e4e62691ce0cbf2 Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 27 Apr 2023 11:03:08 +0800 Subject: [PATCH 3/5] fix(login.py): add error message when retry count is exceeded --- lib/itchat/components/login.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/itchat/components/login.py b/lib/itchat/components/login.py index c4d8c1ce6..e4ec9ca51 100644 --- a/lib/itchat/components/login.py +++ b/lib/itchat/components/login.py @@ -321,6 +321,8 @@ def maintain_loop(): retryCount += 1 logger.error(traceback.format_exc()) if self.receivingRetryCount < retryCount: + logger.error("Having tried %s times, but still failed. " % ( + retryCount) + "Stop trying...") self.alive = False else: time.sleep(1) From be258e5b0594d894268d7d32743a6b64546b1cf9 Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 27 Apr 2023 11:23:28 +0800 Subject: [PATCH 4/5] fix: add more log in itchat --- lib/itchat/components/login.py | 3 ++- lib/itchat/components/register.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/itchat/components/login.py b/lib/itchat/components/login.py index e4ec9ca51..a2dd17c88 100644 --- a/lib/itchat/components/login.py +++ b/lib/itchat/components/login.py @@ -43,6 +43,7 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, logger.warning('itchat has already logged in.') return self.isLogging = True + logger.info('Ready to login.') while self.isLogging: uuid = push_login(self) if uuid: @@ -84,7 +85,7 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, if hasattr(loginCallback, '__call__'): r = loginCallback() else: - utils.clear_screen() + # utils.clear_screen() if os.path.exists(picDir or config.DEFAULT_QR): os.remove(picDir or config.DEFAULT_QR) logger.info('Login successfully as %s' % self.storageClass.nickName) diff --git a/lib/itchat/components/register.py b/lib/itchat/components/register.py index 368ac3154..b8bbc2940 100644 --- a/lib/itchat/components/register.py +++ b/lib/itchat/components/register.py @@ -28,7 +28,7 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', if rval:=self.load_login_status(statusStorageDir, loginCallback=loginCallback, exitCallback=exitCallback): return - logger.error('hot reload failed, logging in normally, {}'.format(rval)) + logger.error('Hot reload failed, logging in normally, error={}'.format(rval)) self.logout() self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, loginCallback=loginCallback, exitCallback=exitCallback) From 9fea949b256a0b45c0def0f3c6fe4d24b35d8dec Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 27 Apr 2023 11:42:19 +0800 Subject: [PATCH 5/5] fix(azure_voice.py): log error details instead of cancellation details --- voice/azure/azure_voice.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/voice/azure/azure_voice.py b/voice/azure/azure_voice.py index 1a0a8ed3f..b5884ed4f 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -64,7 +64,8 @@ def voiceToText(self, voice_file): logger.info("[Azure] voiceToText voice file name={} text={}".format(voice_file, result.text)) reply = Reply(ReplyType.TEXT, result.text) else: - logger.error("[Azure] voiceToText error, result={}, canceldetails={}".format(result, result.cancellation_details)) + cancel_details = result.cancellation_details + logger.error("[Azure] voiceToText error, result={}, errordetails={}".format(result, cancel_details.error_details)) reply = Reply(ReplyType.ERROR, "抱歉,语音识别失败") return reply @@ -88,6 +89,7 @@ def textToVoice(self, text): logger.info("[Azure] textToVoice text={} voice file name={}".format(text, fileName)) reply = Reply(ReplyType.VOICE, fileName) else: - logger.error("[Azure] textToVoice error, result={}, canceldetails={}".format(result, result.cancellation_details)) + cancel_details = result.cancellation_details + logger.error("[Azure] textToVoice error, result={}, errordetails={}".format(result, cancel_details.error_details)) reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败") return reply