Skip to content

Commit 54eef9b

Browse files
committed
Make login stoppable in qrCallback [BR#267: githubwing]
1 parent 34d3a4e commit 54eef9b

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

itchat/components/login.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ def load_login(core):
2727

2828
def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
2929
loginCallback=None, exitCallback=None):
30-
if self.alive:
30+
if self.alive or self.isLogging:
3131
logger.warning('itchat has already logged in.')
3232
return
33-
while 1:
33+
self.isLogging = True
34+
while self.isLogging:
3435
uuid = push_login(self)
35-
if not uuid:
36+
if uuid:
37+
qrStorage = io.BytesIO()
38+
else:
3639
logger.info('Getting uuid of QR code.')
3740
while not self.get_QRuuid():
3841
time.sleep(1)
@@ -56,6 +59,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
5659
if isLoggedIn:
5760
break
5861
logger.info('Log in time out, reloading QR code.')
62+
else:
63+
return # log in process is stopped by user
5964
logger.info('Loading the contact, this may take a little while.')
6065
self.web_init()
6166
self.show_mobile_login()
@@ -68,6 +73,7 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
6873
os.remove(picDir or config.DEFAULT_QR)
6974
logger.info('Login successfully as %s' % self.storageClass.nickName)
7075
self.start_receiving(exitCallback)
76+
self.isLogging = False
7177

7278
def push_login(core):
7379
cookiesDict = core.s.cookies.get_dict()
@@ -311,6 +317,7 @@ def logout(self):
311317
headers = { 'User-Agent' : config.USER_AGENT }
312318
self.s.get(url, params=params, headers=headers)
313319
self.alive = False
320+
self.isLogging = False
314321
self.s.cookies.clear()
315322
del self.chatroomList[:]
316323
del self.memberList[:]

itchat/components/messages.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False,
256256
fileSize = os.path.getsize(fileDir)
257257
fileSymbol = 'pic' if isPicture else 'video' if isVideo else'doc'
258258
with open(fileDir, 'rb') as f: fileMd5 = hashlib.md5(f.read()).hexdigest()
259-
file = open(fileDir, 'rb')
259+
file_ = open(fileDir, 'rb')
260260
chunks = int((fileSize - 1) / 524288) + 1
261261
clientMediaId = int(time.time() * 1e4)
262262
uploadMediaRequest = json.dumps(OrderedDict([
@@ -271,10 +271,13 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False,
271271
('ToUserName', toUserName),
272272
('FileMd5', fileMd5)]
273273
), separators = (',', ':'))
274+
r = {'BaseResponse': {'Ret': -1005, 'ErrMsg': 'Empty file detected'}}
274275
for chunk in range(chunks):
275276
r = upload_chunk_file(self, fileDir, fileSymbol, fileSize,
276-
file, chunk, chunks, uploadMediaRequest)
277-
file.close()
277+
file_, chunk, chunks, uploadMediaRequest)
278+
file_.close()
279+
if isinstance(r, dict):
280+
return ReturnValue(r)
278281
return ReturnValue(rawResponse=r)
279282

280283
def upload_chunk_file(core, fileDir, fileSymbol, fileSize,

itchat/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os, platform
22

3-
VERSION = '1.2.28'
3+
VERSION = '1.2.29'
44
BASE_URL = 'https://login.weixin.qq.com'
55
OS = platform.system() #Windows, Linux, Darwin
66
DIR = os.getcwd()

itchat/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self):
1717
- it's 5 now, but actually even 1 is enough
1818
- failing is failing
1919
'''
20-
self.alive = False
20+
self.alive, self.isLogging = False, False
2121
self.storageClass = storage.Storage()
2222
self.memberList = self.storageClass.memberList
2323
self.mpList = self.storageClass.mpList

0 commit comments

Comments
 (0)