Skip to content

Commit

Permalink
Make login stoppable in qrCallback [BR#267: githubwing]
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Mar 8, 2017
1 parent 34d3a4e commit 54eef9b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
13 changes: 10 additions & 3 deletions itchat/components/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ def load_login(core):

def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
loginCallback=None, exitCallback=None):
if self.alive:
if self.alive or self.isLogging:
logger.warning('itchat has already logged in.')
return
while 1:
self.isLogging = True
while self.isLogging:
uuid = push_login(self)
if not uuid:
if uuid:
qrStorage = io.BytesIO()
else:
logger.info('Getting uuid of QR code.')
while not self.get_QRuuid():
time.sleep(1)
Expand All @@ -56,6 +59,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
if isLoggedIn:
break
logger.info('Log in time out, reloading QR code.')
else:
return # log in process is stopped by user
logger.info('Loading the contact, this may take a little while.')
self.web_init()
self.show_mobile_login()
Expand All @@ -68,6 +73,7 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
os.remove(picDir or config.DEFAULT_QR)
logger.info('Login successfully as %s' % self.storageClass.nickName)
self.start_receiving(exitCallback)
self.isLogging = False

def push_login(core):
cookiesDict = core.s.cookies.get_dict()
Expand Down Expand Up @@ -311,6 +317,7 @@ def logout(self):
headers = { 'User-Agent' : config.USER_AGENT }
self.s.get(url, params=params, headers=headers)
self.alive = False
self.isLogging = False
self.s.cookies.clear()
del self.chatroomList[:]
del self.memberList[:]
Expand Down
9 changes: 6 additions & 3 deletions itchat/components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False,
fileSize = os.path.getsize(fileDir)
fileSymbol = 'pic' if isPicture else 'video' if isVideo else'doc'
with open(fileDir, 'rb') as f: fileMd5 = hashlib.md5(f.read()).hexdigest()
file = open(fileDir, 'rb')
file_ = open(fileDir, 'rb')
chunks = int((fileSize - 1) / 524288) + 1
clientMediaId = int(time.time() * 1e4)
uploadMediaRequest = json.dumps(OrderedDict([
Expand All @@ -271,10 +271,13 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False,
('ToUserName', toUserName),
('FileMd5', fileMd5)]
), separators = (',', ':'))
r = {'BaseResponse': {'Ret': -1005, 'ErrMsg': 'Empty file detected'}}
for chunk in range(chunks):
r = upload_chunk_file(self, fileDir, fileSymbol, fileSize,
file, chunk, chunks, uploadMediaRequest)
file.close()
file_, chunk, chunks, uploadMediaRequest)
file_.close()
if isinstance(r, dict):
return ReturnValue(r)
return ReturnValue(rawResponse=r)

def upload_chunk_file(core, fileDir, fileSymbol, fileSize,
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.28'
VERSION = '1.2.29'
BASE_URL = 'https://login.weixin.qq.com'
OS = platform.system() #Windows, Linux, Darwin
DIR = os.getcwd()
Expand Down
2 changes: 1 addition & 1 deletion itchat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self):
- it's 5 now, but actually even 1 is enough
- failing is failing
'''
self.alive = False
self.alive, self.isLogging = False, False
self.storageClass = storage.Storage()
self.memberList = self.storageClass.memberList
self.mpList = self.storageClass.mpList
Expand Down

0 comments on commit 54eef9b

Please sign in to comment.