Browse Source

fix(itchat): add error log when hot reload fails and log out before logging in normally

master
lanvent 1 year ago
parent
commit
527d5e1dbc
3 changed files with 10 additions and 17 deletions
  1. +6
    -15
      channel/wechat/wechat_channel.py
  2. +1
    -1
      lib/itchat/components/login.py
  3. +3
    -1
      lib/itchat/components/register.py

+ 6
- 15
channel/wechat/wechat_channel.py View File

@@ -113,21 +113,12 @@ class WechatChannel(ChatChannel):
# login by scan QRCode # login by scan QRCode
hotReload = conf().get("hot_reload", False) hotReload = conf().get("hot_reload", False)
status_path = os.path.join(get_appdata_dir(), "itchat.pkl") 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.user_id = itchat.instance.storageClass.userName
self.name = itchat.instance.storageClass.nickName self.name = itchat.instance.storageClass.nickName
logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name)) logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name))


+ 1
- 1
lib/itchat/components/login.py View File

@@ -367,7 +367,7 @@ def sync_check(self):
regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}' regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}'
pm = re.search(regx, r.text) pm = re.search(regx, r.text)
if pm is None or pm.group(1) != '0': 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 None
return pm.group(2) return pm.group(2)




+ 3
- 1
lib/itchat/components/register.py View File

@@ -25,9 +25,11 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl',
self.useHotReload = hotReload self.useHotReload = hotReload
self.hotReloadDir = statusStorageDir self.hotReloadDir = statusStorageDir
if hotReload: if hotReload:
if self.load_login_status(statusStorageDir,
if rval:=self.load_login_status(statusStorageDir,
loginCallback=loginCallback, exitCallback=exitCallback): loginCallback=loginCallback, exitCallback=exitCallback):
return return
logger.error('hot reload failed, logging in normally, {}'.format(rval))
self.logout()
self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback,
loginCallback=loginCallback, exitCallback=exitCallback) loginCallback=loginCallback, exitCallback=exitCallback)
self.dump_login_status(statusStorageDir) self.dump_login_status(statusStorageDir)


Loading…
Cancel
Save