Skip to content

Commit 0fa43ed

Browse files
committed
Add pushlogin to login without scan qrcode
1 parent 9dc75fc commit 0fa43ed

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

itchat/components/contact.py

+2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ def _get_contact(seq=0):
274274
r = self.s.get(url, headers=headers)
275275
except:
276276
logger.info('Failed to fetch contact, that may because of the amount of your chatrooms')
277+
for chatroom in self.get_chatrooms():
278+
self.update_chatroom(chatroom['UserName'], detailedMember=True)
277279
return 0, []
278280
j = json.loads(r.content.decode('utf-8', 'replace'))
279281
return j.get('Seq', 0), j.get('MemberList')

itchat/components/hotreload.py

+18
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def load_login_status(self, fileDir,
5555
msgList, contactList = self.get_msg()
5656
if (msgList or contactList) is None:
5757
self.logout()
58+
load_last_login_status(self.s, j['cookies'])
5859
logger.debug('server refused, loading login status failed.')
5960
return ReturnValue({'BaseResponse': {
6061
'ErrMsg': 'server refused, loading login status failed.',
@@ -76,3 +77,20 @@ def load_login_status(self, fileDir,
7677
return ReturnValue({'BaseResponse': {
7778
'ErrMsg': 'loading login status succeeded.',
7879
'Ret': 0, }})
80+
81+
def load_last_login_status(session, cookiesDict):
82+
try:
83+
session.cookies = requests.utils.cookiejar_from_dict({
84+
'webwxuvid': cookiesDict['webwxuvid'],
85+
'webwx_auth_ticket': cookiesDict['webwx_auth_ticket'],
86+
'login_frequency': '2',
87+
'last_wxuin': cookiesDict['wxuin'],
88+
'wxloadtime': cookiesDict['wxloadtime'] + '_expired',
89+
'wxpluginkey': cookiesDict['wxloadtime'],
90+
'wxuin': cookiesDict['wxuin'],
91+
'mm_lang': 'zh_CN',
92+
'MM_WX_NOTIFY_STATE': '1',
93+
'MM_WX_SOUND_STATE': '1', })
94+
except:
95+
logger.info('Load status for push login failed, we may have experienced a cookies change.')
96+
logger.info('If you are using the newest version of itchat, you may report a bug.')

itchat/components/login.py

+42-10
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
3131
logger.warning('itchat has already logged in.')
3232
return
3333
while 1:
34-
logger.info('Getting uuid of QR code.')
35-
while not self.get_QRuuid():
36-
time.sleep(1)
37-
logger.info('Downloading QR code.')
38-
qrStorage = self.get_QR(enableCmdQR=enableCmdQR,
39-
picDir=picDir, qrCallback=qrCallback)
40-
logger.info('Please scan the QR code to log in.')
34+
uuid = push_login(self)
35+
if not uuid:
36+
logger.info('Getting uuid of QR code.')
37+
while not self.get_QRuuid():
38+
time.sleep(1)
39+
logger.info('Downloading QR code.')
40+
qrStorage = self.get_QR(enableCmdQR=enableCmdQR,
41+
picDir=picDir, qrCallback=qrCallback)
42+
logger.info('Please scan the QR code to log in.')
4143
isLoggedIn = False
4244
while not isLoggedIn:
4345
status = self.check_login()
@@ -53,7 +55,8 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
5355
break
5456
if isLoggedIn:
5557
break
56-
logger.info('Log in time out, reloading QR code')
58+
logger.info('Log in time out, reloading QR code.')
59+
logger.info('Loading the contact, this may take a little while.')
5760
self.web_init()
5861
self.show_mobile_login()
5962
self.get_contact(True)
@@ -66,6 +69,18 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
6669
logger.info('Login successfully as %s' % self.storageClass.nickName)
6770
self.start_receiving(exitCallback)
6871

72+
def push_login(core):
73+
cookiesDict = core.s.cookies.get_dict()
74+
if 'wxuin' in cookiesDict:
75+
url = '%s/cgi-bin/mmwebwx-bin/webwxpushloginurl?uin=%s' % (
76+
config.BASE_URL, cookiesDict['wxuin'])
77+
headers = { 'User-Agent' : config.USER_AGENT }
78+
r = core.s.get(url, headers=headers).json()
79+
if 'uuid' in r and r.get('ret') in (0, '0'):
80+
core.uuid = r['uuid']
81+
return r['uuid']
82+
return False
83+
6984
def get_QRuuid(self):
7085
url = '%s/jslogin' % config.BASE_URL
7186
params = {
@@ -168,6 +183,22 @@ def web_init(self):
168183
for item in dic['SyncKey']['List']])
169184
self.storageClass.userName = dic['User']['UserName']
170185
self.storageClass.nickName = dic['User']['NickName']
186+
# deal with contact list returned when init
187+
contactList = dic.get('ContactList', [])
188+
chatroomList, otherList = [], []
189+
for m in contactList:
190+
if m['Sex'] != 0:
191+
otherList.append(m)
192+
elif '@@' in m['UserName']:
193+
m['MemberList'] = [] # don't let dirty info pollute the list
194+
chatroomList.append(m)
195+
elif '@' in m['UserName']:
196+
# mp will be dealt in update_local_friends as well
197+
otherList.append(m)
198+
if chatroomList:
199+
update_local_chatrooms(self, chatroomList)
200+
if otherList:
201+
update_local_friends(self, otherList)
171202
return dic
172203

173204
def show_mobile_login(self):
@@ -195,12 +226,13 @@ def maintain_loop():
195226
if i is None:
196227
self.alive = False
197228
elif i == '0':
198-
continue
229+
pass
199230
else:
200231
msgList, contactList = self.get_msg()
201232
if msgList:
202233
msgList = produce_msg(self, msgList)
203-
for msg in msgList: self.msgList.put(msg)
234+
for msg in msgList:
235+
self.msgList.put(msg)
204236
if contactList:
205237
chatroomList, otherList = [], []
206238
for contact in contactList:

itchat/components/messages.py

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ def produce_group_chat(core, msg):
198198
content = msg['Content']
199199
chatroomUserName = msg['ToUserName']
200200
else:
201+
msg['ActualUserName'] = core.storageClass.userName
202+
msg['ActualNickName'] = core.storageClass.nickName
203+
msg['isAt'] = False
201204
return
202205
chatroom = core.storageClass.search_chatrooms(userName=chatroomUserName)
203206
member = utils.search_dict_list((chatroom or {}).get(

itchat/config.py

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

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

99
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'

itchat/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
5454
it is defined in components/login.py
5555
and of course every single move in login can be called outside
5656
- you may scan source code to see how
57-
- and modified according to your own demond
57+
- and modified according to your own demand
5858
'''
5959
raise NotImplementedError()
6060
def get_QRuuid(self):

0 commit comments

Comments
 (0)