From 6acdb3b47a5129f3d6a67658db7ae0d4caef996f Mon Sep 17 00:00:00 2001 From: Excelle Su Date: Tue, 10 Oct 2017 19:52:08 +0800 Subject: [PATCH 1/4] Add timeout --- itchat/components/login.py | 9 ++++++++- itchat/components/register.py | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/itchat/components/login.py b/itchat/components/login.py index 9aa963a4..2d792858 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -31,7 +31,8 @@ def load_login(core): core.logout = logout def login(self, enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None): + loginCallback=None, exitCallback=None, timeout=None): + start_time = time.time() if self.alive or self.isLogging: logger.warning('itchat has already logged in.') return @@ -50,6 +51,12 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, logger.info('Please scan the QR code to log in.') isLoggedIn = False while not isLoggedIn: + now = time.time() + if timeout and (now - start_time) >= timeout: + logger.warning('Login timeout, exit.') + print('Timeout as user setting.') + return + status = self.check_login() if hasattr(qrCallback, '__call__'): qrCallback(uuid=self.uuid, status=status, qrcode=qrStorage.getvalue()) diff --git a/itchat/components/register.py b/itchat/components/register.py index 079d3191..4aac0d55 100644 --- a/itchat/components/register.py +++ b/itchat/components/register.py @@ -18,7 +18,7 @@ def load_register(core): def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None): + loginCallback=None, exitCallback=None, timeout=None): if not test_connect(): logger.info("You can't get access to internet or wechat domain, so exit.") sys.exit() @@ -29,11 +29,11 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', loginCallback=loginCallback, exitCallback=exitCallback): return self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, - loginCallback=loginCallback, exitCallback=exitCallback) + loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout) self.dump_login_status(statusStorageDir) else: self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, - loginCallback=loginCallback, exitCallback=exitCallback) + loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout) def configured_reply(self): ''' determine the type of message and reply if its method is defined From 974f0deecc604cc9bd255f5d204f0ba4047993e6 Mon Sep 17 00:00:00 2001 From: Excelle Su Date: Tue, 10 Oct 2017 20:15:28 +0800 Subject: [PATCH 2/4] Add timeout callback --- itchat/components/login.py | 4 +++- itchat/components/register.py | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/itchat/components/login.py b/itchat/components/login.py index 2d792858..e5dee1f0 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -31,7 +31,7 @@ def load_login(core): core.logout = logout def login(self, enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None, timeout=None): + loginCallback=None, exitCallback=None, timeout=None, timeoutCallback=None): start_time = time.time() if self.alive or self.isLogging: logger.warning('itchat has already logged in.') @@ -54,6 +54,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, now = time.time() if timeout and (now - start_time) >= timeout: logger.warning('Login timeout, exit.') + if hasattr(timeoutCallback, '__call__'): + timeoutCallback() print('Timeout as user setting.') return diff --git a/itchat/components/register.py b/itchat/components/register.py index 4aac0d55..cb51f29a 100644 --- a/itchat/components/register.py +++ b/itchat/components/register.py @@ -18,7 +18,7 @@ def load_register(core): def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None, timeout=None): + loginCallback=None, exitCallback=None, timeout=None, timeoutCallback=None): if not test_connect(): logger.info("You can't get access to internet or wechat domain, so exit.") sys.exit() @@ -29,11 +29,13 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', loginCallback=loginCallback, exitCallback=exitCallback): return self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, - loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout) + loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout, + timeoutCallback=timeoutCallback) self.dump_login_status(statusStorageDir) else: self.login(enableCmdQR=enableCmdQR, picDir=picDir, qrCallback=qrCallback, - loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout) + loginCallback=loginCallback, exitCallback=exitCallback, timeout=timeout, + timeoutCallback=timeoutCallback) def configured_reply(self): ''' determine the type of message and reply if its method is defined From 0166af75e0c5802a842d181751eb77f00f2797d2 Mon Sep 17 00:00:00 2001 From: Excelle Su Date: Tue, 10 Oct 2017 20:18:31 +0800 Subject: [PATCH 3/4] Update doc --- itchat/core.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/itchat/core.py b/itchat/core.py index 2b39d933..3d8dbe3d 100644 --- a/itchat/core.py +++ b/itchat/core.py @@ -30,7 +30,8 @@ def __init__(self): self.useHotReload, self.hotReloadDir = False, 'itchat.pkl' self.receivingRetryCount = 5 def login(self, enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None): + loginCallback=None, exitCallback=None, timeout=None, + timeoutCallback=None): ''' log in like web wechat does for log in - a QR code will be downloaded and opened @@ -45,6 +46,11 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, - if not set, screen is cleared and qrcode is deleted - exitCallback: callback after logged out - it contains calling of logout + - timeout: the login process will forcibly end after dedicated + timeout seconds + - if not set, login process will always wait until user + scan the QRCode to proceed. + - timeoutCallback: callback after timeout for usage ..code::python @@ -399,7 +405,8 @@ def load_login_status(self, fileDir, raise NotImplementedError() def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', enableCmdQR=False, picDir=None, qrCallback=None, - loginCallback=None, exitCallback=None): + loginCallback=None, exitCallback=None, timeout=None, + timeoutCallback=None): ''' log in like web wechat does for log in - a QR code will be downloaded and opened @@ -416,6 +423,11 @@ def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', - exitCallback: callback after logged out - it contains calling of logout - qrCallback: method that should accept uuid, status, qrcode + - timeout: the login process will forcibly end after dedicated + timeout seconds + - if not set, login process will always wait until user + scan the QRCode to proceed. + - timeoutCallback: callback after timeout for usage ..code::python From c5a076789315f669884090878572d61ed2aa08ad Mon Sep 17 00:00:00 2001 From: suwei Date: Sun, 23 Dec 2018 13:08:32 -0500 Subject: [PATCH 4/4] get current timestamp only when timeout=true --- itchat/components/login.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/itchat/components/login.py b/itchat/components/login.py index e5dee1f0..b891c4b6 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -51,7 +51,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, logger.info('Please scan the QR code to log in.') isLoggedIn = False while not isLoggedIn: - now = time.time() + if timeout: + now = time.time() if timeout and (now - start_time) >= timeout: logger.warning('Login timeout, exit.') if hasattr(timeoutCallback, '__call__'):