Skip to content

Commit

Permalink
Add logging of sending messages & fix bug of chatroom returned by sync
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Dec 2, 2016
1 parent b9a152f commit 2bc6e0b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
14 changes: 11 additions & 3 deletions itchat/components/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def update_friend(self, userName):
return r if 1 < len(r) else r[0]

def update_local_chatrooms(core, l):
'''
get a list of chatrooms for updating local chatrooms
return a list of given chatrooms with updated info
'''
oldUsernameList = []
for chatroom in l:
# format NickName & DisplayName & self keys
Expand All @@ -119,6 +123,7 @@ def update_local_chatrooms(core, l):
oldMember = utils.search_dict_list(
oldMemberList, 'UserName', member['UserName'])
if oldMember is not None:
#TODO
for k in oldMember:
member[k] = member.get(k) or oldMember[k]
else:
Expand Down Expand Up @@ -158,12 +163,12 @@ def update_local_chatrooms(core, l):
def update_local_friends(core, l):
fullList = core.memberList + core.mpList
for friend in l:
utils.emoji_formatter(friend, 'NickName')
utils.emoji_formatter(friend, 'DisplayName')
oldInfoDict = utils.search_dict_list(
fullList, 'UserName', friend['UserName'])
if oldInfoDict is None:
oldInfoDict = copy.deepcopy(friend)
utils.emoji_formatter(oldInfoDict, 'NickName')
utils.emoji_formatter(oldInfoDict, 'DisplayName')
if oldInfoDict['VerifyFlag'] & 8 == 0:
core.memberList.append(oldInfoDict)
else:
Expand All @@ -176,6 +181,10 @@ def update_local_friends(core, l):
oldInfoDict[k] = v

def update_local_uin(core, msg):
'''
content contains uins and StatusNotifyUserName contains username
they are in same order, so what I do is to pair them together
'''
uins = re.search('<username>([^<]*?)<', msg['Content'])
usernameChangedList = []
r = {
Expand Down Expand Up @@ -232,7 +241,6 @@ def get_contact(self, update=False):
tempList = json.loads(r.content.decode('utf-8', 'replace'))['MemberList']
chatroomList, otherList = [], []
for m in tempList:
utils.emoji_formatter(m, 'NickName')
if m['Sex'] != 0:
otherList.append(m)
elif '@@' in m['UserName']:
Expand Down
6 changes: 5 additions & 1 deletion itchat/components/hotreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def load_login_status(self, fileDir,
'Ret': -1003, }})
else:
if contactList:
update_local_chatrooms(self, contactList)
for contact in contactList:
if '@@' in contact['UserName']:
update_local_chatrooms(self, [contact])
else:
update_local_chatrooms(self, [contact])
if msgList:
msgList = produce_msg(self, msgList)
for msg in msgList: self.msgList.put(msg)
Expand Down
9 changes: 8 additions & 1 deletion itchat/components/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def web_init(self):
for item in dic['SyncKey']['List']])
self.storageClass.userName = dic['User']['UserName']
self.storageClass.nickName = dic['User']['NickName']
self.memberList.append(dic['User'])
return dic

def show_mobile_login(self):
Expand Down Expand Up @@ -190,7 +191,13 @@ def maintain_loop():
else:
msgList, contactList = self.get_msg()
if contactList:
chatroomMsg = update_local_chatrooms(self, contactList)
chatroomList, otherList = [], []
for contact in contactList:
if '@@' in contact['UserName']:
chatroomList.append(contact)
else:
otherList.append(contact)
chatroomMsg = update_local_chatrooms(self, chatroomList)
self.msgList.put(chatroomMsg)
if msgList:
msgList = produce_msg(self, msgList)
Expand Down
9 changes: 9 additions & 0 deletions itchat/components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,13 @@ def send_raw_msg(self, msgType, content, toUserName):
return ReturnValue(rawResponse=r)

def send_msg(self, msg='Test Message', toUserName=None):
logger.debug('Request to send a text message to %s: %s' % (toUserName, msg))
r = self.send_raw_msg(1, msg, toUserName)
return r

def upload_file(self, fileDir, isPicture=False, isVideo=False):
logger.debug('Request to upload a %s: %s' % (
'picture' if isPicture else 'video' if isVideo else 'file', fileDir))
if not utils.check_file(fileDir):
return ReturnValue({'BaseResponse': {
'ErrMsg': 'No file found in specific dir',
Expand Down Expand Up @@ -267,6 +270,8 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False):
return ReturnValue(rawResponse=r)

def send_file(self, fileDir, toUserName=None, mediaId=None):
logger.debug('Request to send a file(mediaId: %s) to %s: %s' % (
mediaId, toUserName, fileDir))
if toUserName is None: toUserName = self.storageClass.userName
if mediaId is None:
r = self.upload_file(fileDir)
Expand Down Expand Up @@ -296,6 +301,8 @@ def send_file(self, fileDir, toUserName=None, mediaId=None):
return ReturnValue(rawResponse=r)

def send_image(self, fileDir, toUserName=None, mediaId=None):
logger.debug('Request to send a image(mediaId: %s) to %s: %s' % (
mediaId, toUserName, fileDir))
if toUserName is None: toUserName = self.storageClass.userName
if mediaId is None:
r = self.upload_file(fileDir, isPicture=not fileDir[-4:] == '.gif')
Expand Down Expand Up @@ -326,6 +333,8 @@ def send_image(self, fileDir, toUserName=None, mediaId=None):
return ReturnValue(rawResponse=r)

def send_video(self, fileDir=None, toUserName=None, mediaId=None):
logger.debug('Request to send a video(mediaId: %s) to %s: %s' % (
mediaId, toUserName, fileDir))
if toUserName is None: toUserName = self.storageClass.userName
if mediaId is None:
r = self.upload_file(fileDir, isVideo=True)
Expand Down

0 comments on commit 2bc6e0b

Please sign in to comment.