From 89b1acdfb341f0cee1390c0d630592cb03d620a0 Mon Sep 17 00:00:00 2001 From: LittleCoder Date: Mon, 5 Dec 2016 10:46:31 +0800 Subject: [PATCH] Fix no qr bug in qrCallback & add no internet warning --- itchat/components/login.py | 3 ++- itchat/components/register.py | 6 +++++- itchat/config.py | 2 +- itchat/utils.py | 10 ++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/itchat/components/login.py b/itchat/components/login.py index 21b14a87..5663c7fa 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -64,7 +64,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None, r = loginCallback() else: utils.clear_screen() - os.remove(picDir or config.DEFAULT_QR) + 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) self.start_receiving(exitCallback) diff --git a/itchat/components/register.py b/itchat/components/register.py index a2642b5b..3afe8c3c 100644 --- a/itchat/components/register.py +++ b/itchat/components/register.py @@ -1,10 +1,11 @@ -import logging, traceback +import logging, traceback, sys try: import Queue except ImportError: import queue as Queue from ..log import set_logging +from ..utils import test_connect logger = logging.getLogger('itchat') @@ -17,6 +18,9 @@ def load_register(core): def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl', enableCmdQR=False, picDir=None, qrCallback=None, loginCallback=None, exitCallback=None): + if not test_connect(): + logger.info("You don't have access to internet or wechat domain, so exit.") + sys.exit() self.useHotReload = hotReload if hotReload: if self.load_login_status(statusStorageDir, diff --git a/itchat/config.py b/itchat/config.py index c3ef5b63..2736cd0a 100644 --- a/itchat/config.py +++ b/itchat/config.py @@ -1,6 +1,6 @@ import os, platform -VERSION = '1.2.10' +VERSION = '1.2.11' BASE_URL = 'https://login.weixin.qq.com' OS = platform.system() #Windows, Linux, Darwin DIR = os.getcwd() diff --git a/itchat/utils.py b/itchat/utils.py index d4c48a6b..8ae55abf 100644 --- a/itchat/utils.py +++ b/itchat/utils.py @@ -5,6 +5,8 @@ except ImportError: from html.parser import HTMLParser +import requests + from . import config emojiRegex = re.compile(r'') @@ -123,3 +125,11 @@ def print_line(msg, oneLine = False): sys.stdout.write(msg.encode(sys.stdin.encoding or 'utf8', 'replace' ).decode(sys.stdin.encoding or 'utf8', 'replace')) sys.stdout.flush() + +def test_connect(retryTime=5): + for i in range(retryTime): + try: + r = requests.get(config.BASE_URL) + except: + if i == retryTime-1: return False + return True