Skip to content

Commit 9dc75fc

Browse files
committed
Fix long contact fetch bug & new protocol of adding friends
1 parent 9d67e06 commit 9dc75fc

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

itchat/components/contact.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,27 @@ def update_local_uin(core, msg):
264264
def get_contact(self, update=False):
265265
if not update:
266266
return utils.contact_deep_copy(self, self.chatroomList)
267-
url = '%s/webwxgetcontact?r=%s&seq=0&skey=%s' % (self.loginInfo['url'],
268-
int(time.time()), self.loginInfo['skey'])
269-
headers = {
270-
'ContentType': 'application/json; charset=UTF-8',
271-
'User-Agent' : config.USER_AGENT, }
272-
try:
273-
r = self.s.get(url, headers=headers)
274-
except:
275-
logger.info('Failed to fetch contact, that may because of the amount of your chatrooms')
276-
return []
277-
tempList = json.loads(r.content.decode('utf-8', 'replace'))['MemberList']
267+
def _get_contact(seq=0):
268+
url = '%s/webwxgetcontact?r=%s&seq=%s&skey=%s' % (self.loginInfo['url'],
269+
int(time.time()), seq, self.loginInfo['skey'])
270+
headers = {
271+
'ContentType': 'application/json; charset=UTF-8',
272+
'User-Agent' : config.USER_AGENT, }
273+
try:
274+
r = self.s.get(url, headers=headers)
275+
except:
276+
logger.info('Failed to fetch contact, that may because of the amount of your chatrooms')
277+
return 0, []
278+
j = json.loads(r.content.decode('utf-8', 'replace'))
279+
return j.get('Seq', 0), j.get('MemberList')
280+
seq, memberList = 0, []
281+
while 1:
282+
seq, batchMemberList = _get_contact(seq)
283+
memberList.extend(batchMemberList)
284+
if seq == 0:
285+
break
278286
chatroomList, otherList = [], []
279-
for m in tempList:
287+
for m in memberList:
280288
if m['Sex'] != 0:
281289
otherList.append(m)
282290
elif '@@' in m['UserName']:
@@ -356,14 +364,15 @@ def add_friend(self, userName, status=2, verifyContent='', autoUpdate=True):
356364
'VerifyUserTicket': '', }],
357365
'VerifyContent': verifyContent,
358366
'SceneListCount': 1,
359-
'SceneList': 33, # [33]
367+
'SceneList': [33],
360368
'skey': self.loginInfo['skey'], }
361369
headers = {
362370
'ContentType': 'application/json; charset=UTF-8',
363371
'User-Agent' : config.USER_AGENT }
364372
r = self.s.post(url, headers=headers,
365373
data=json.dumps(data, ensure_ascii=False).encode('utf8', 'replace'))
366-
if autoUpdate: self.update_friend(userName)
374+
if autoUpdate:
375+
self.update_friend(userName)
367376
return ReturnValue(rawResponse=r)
368377

369378
def get_head_img(self, userName=None, chatroomUserName=None, picDir=None):

0 commit comments

Comments
 (0)