Skip to content

Commit

Permalink
Fix no qr bug in qrCallback & add no internet warning
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Dec 5, 2016
1 parent c30ae9a commit 89b1acd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion itchat/components/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion itchat/components/register.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion itchat/config.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 10 additions & 0 deletions itchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
except ImportError:
from html.parser import HTMLParser

import requests

from . import config

emojiRegex = re.compile(r'<span class="emoji emoji(.{1,10})"></span>')
Expand Down Expand Up @@ -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

0 comments on commit 89b1acd

Please sign in to comment.