def lc(self): """ 登录时的回调函数, :param self: :return: """ cmd = 'lc' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) print(datetime.datetime.now(), self.nickName, '成功登陆!') self.online = True self.receiveMsg = True self.frdInfoList = self.getFriends() self.userName = self.frdInfoList[0]['UserName'] self.schemeTimer.start() try: self.instance.send('登录成功') except Exception as e: self.instance.send('Error in users line 698, %s' % str(e)) print('Error in users line 698', e) self.msgProcess() try: os.remove(os.getcwd() + r'/users/%s/QR.png' % self.nickName) except Exception as reason: pass myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def getMps(self): cmd = 'getMps' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if self.online: myLogging(self.cmdFile, self.nickName, infoType='cmd', status='succes', other=cmd) return self.instance.get_mps(update=True)
def ec(self): cmd = 'ec' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) self.logout() del onlineDict[self.nickName] print(datetime.datetime.now(), self.nickName, '成功退出!') myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def userOnline(self): # 获取当前在线用户 cmd = 'userOnline' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) usersInfo = [str(each) for each in onlineDict.keys()] reply = '当前在线用户:\n' + '\n'.join(usersInfo) self.instance.send(reply, toUserName=self.userName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def fileHelperOff(self): cmd = 'fileHelperOff' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) self.filehelper = 0 self.mysqlHelper.update('users', 'filehelper', self.filehelper, 'nickName', self.nickName) self.instance.send('提示:文件传输助手已关闭') myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def getChatrooms(self): cmd = 'getChatrooms' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if self.online: chatroomList = self.instance.get_chatrooms(update=True) self.chatroomList = [each['NickName'] for each in chatroomList] myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) return chatroomList
def getFriends(self): cmd = 'getFriends' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if self.online: frdInfoList = self.instance.get_friends(update=True) self.frdList = [each['NickName'] for each in frdInfoList] myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) return frdInfoList
def autoReplyOff(self): # 关闭自动回复 cmd = 'autoReplyOff' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) self.autoReply = 0 self.mysqlHelper.update('users', 'autoReply', self.autoReply, 'nickName', self.nickName) self.instance.send('提示:已关闭自动回复', toUserName=self.userName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def getUserDict(self): cmd = 'getUserDict' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) usersInfo = [ str(each) for each in self.mysqlHelper.getAll('users', 'nickName', 'userType') ] reply = '当前注册用户:\n' + '\n'.join(usersInfo) self.instance.send(reply, toUserName=self.userName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def getChatroomMembers(self, nickName, detailedMember=True): cmd = 'getChatroomMembers' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if self.online: chatroomUserName = self.instance.search_chatrooms( name=nickName)[0]['UserName'] chatroomMembers = self.instance.update_chatroom( chatroomUserName, detailedMember=detailedMember) self.chatroomMembers = [ each['NickName'] for each in chatroomMembers['MemberList'] ] myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) return chatroomMembers
def replyFriendChat(msg): # TODO (1)处理自己给自己的消息 if msg['FromUserName'] == msg['ToUserName'] == self.userName: # 来自自己的消息 if (msg['Type'] == TEXT) and (msg['Text'][0] == '*'): # 处理命令消息,以*开头 replyCmd(msg['Text'][1:], self.cmds) else: # TODO 处理其他消息 pass # TODO (2)处理好友发来的消息 elif msg['FromUserName'] in [ each['UserName'] for each in self.frdInfoList[1:] ]: # 来自好友的消息 myLogging(self.chatDir + '%s.txt' % msg['User']['NickName'], self.nickName, infoType='recv', other=msg['User']['NickName'], info=msg['Text']) if (msg['Type'] == TEXT) and (msg['Text'][0] == '*'): # 处理远程指令消息,以*开头 replyCmd(msg['Text'][1:], self.remoteCmds) else: if self.autoReply: # 自动回复 if msg['User']['NickName'] in self.autoReplyGroup: forward(msg, msg['User']['NickName'], self.robotNickName, self.robotUserName) self.FTUserName = msg['FromUserName'] self.FTNickName = msg['User']['NickName'] else: pass # TODO (3)处理自己向其他好友发送的消息 else: try: toNickName = self.instance.search_friends( userName=msg['ToUserName'])[0]['NickName'] print(toNickName) myLogging(self.chatDir + '%s.txt' % toNickName, self.nickName, infoType='send', other=toNickName, info=msg['Text']) except Exception as reason: print('Error: replyFriendChat, reason:', reason)
def geoData(self, chatroom=None): path = self.savedir + 'geoData.html' if chatroom: cmd = 'geoData %s' % chatroom myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: chatroom == self.instance.search_chatrooms( name=chatroom)[0]['NickName'] except Exception as reason: self.instance.send('在您的通讯录中没有找到该群[%s]' % chatroom) myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) return memberList = self.getChatroomMembers(chatroom)['MemberList'] subtitle = self.nickName + ':' + chatroom else: cmd = 'geoData' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) memberList = [each['Signature'] for each in self.frdInfoList] subtitle = self.nickName cities = [each['City'] for each in memberList] with open( os.path.dirname(os.path.abspath(__file__)) + '/city_coordinates.json', 'rb') as f: cityDict = json.load(f) data = [(each, cities.count(each)) for each in set(cities) if each in cityDict.keys()] geo = pyecharts.Geo('微信好友地理分布', subtitle, title_color="#fff", title_pos="center", background_color='#404a59') attr, value = geo.cast(data) geo.add("", attr, value, visual_range=[0, 100], visual_text_color="#fff", symbol_size=10, is_visualmap=True) geo.render(path) self.instance.send('@fil@%s' % path) os.remove(path) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def friendInChatrooms(self, chatroom): cmd = 'friendInChatrooms' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) chatroomList = self.getChatrooms() chatroomList = [each['NickName'] for each in chatroomList] if chatroom in chatroomList: chatroomMembers = self.getChatroomMembers(chatroom) chatroomMembers = [ each['NickName'] for each in chatroomMembers['MemberList'] ] if not self.frdInfoList: self.frdInfoList = self.getFriends() frdList = [each['NickName'] for each in self.frdInfoList] intersection = set(chatroomMembers) & set(frdList) reply = '与您在同在群[%s]的好友有%d个:\n%s' % (chatroom, len(intersection), str(intersection)) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) else: reply = '失败!群[%s]不在您的群列表中!' % chatroom myLogging(self.cmdFile, self.nickName, infoType='cmd', status='fail', other=cmd, info='群[%s]不在您的群列表中!' % chatroom) self.instance.send(reply)
def signatureCloud(self, chatroom=None): # 签名词云 path = self.savedir + 'signatureCloud.html' if chatroom: cmd = 'signaturCloud %s' % chatroom myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: chatroom == self.instance.search_chatrooms( name=chatroom)[0]['NickName'] except Exception as reason: self.instance.send('在您的通讯录中没有找到该群[%s]' % chatroom) myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) return memberList = self.getChatroomMembers(chatroom)['MemberList'] sigList = [each['Signature'] for each in memberList] subtitle = self.nickName + ':' + chatroom else: cmd = 'signaturCloud' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) sigList = [each['Signature'] for each in self.frdInfoList] subtitle = self.nickName primaryList = re.findall('\w+', ' '.join(sigList)) wordString = re.sub(r'emoji[\d\w]*|span|class', '', ' '.join(primaryList)) finalList = [ each for each in list(jieba.cut(wordString)) if len(each) >= 2 ] words = list(set(finalList)) amount = [finalList.count(each) for each in words] wordCloud = pyecharts.WordCloud('微信好友个性签名词云图', subtitle) shape = random.choice([ 'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star' ]) wordCloud.add('', words, amount, word_size_range=[12 - 60], shape=shape) wordCloud.render(path) self.instance.send('@fil@%s' % path) os.remove(path) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def mfRatio(self, chatroom=None): # 男女比例 male = female = othor = 0 lables = ['男', '女', '其他'] path = self.savedir + 'mfRatio.html' if chatroom: cmd = 'mfRatio %s' % chatroom myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: chatroom == self.instance.search_chatrooms( name=chatroom)[0]['NickName'] except Exception as reason: self.instance.send('在您的通讯录中没有找到该群[%s]' % chatroom) myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) return memberList = self.getChatroomMembers(chatroom)['MemberList'] subtitle = self.nickName + ':' + chatroom else: cmd = 'mfRatio' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) memberList = self.frdInfoList[1:] subtitle = self.nickName male = len([1 for each in memberList if each['Sex'] == 1]) female = len([2 for each in memberList if each['Sex'] == 2]) other = len([0 for each in memberList if each['Sex'] == 0]) values = [male, female, other] pie = pyecharts.Pie('微信好友男女比例', subtitle) pie.add('男女比例', lables, values, is_label_show=True) pie.render(path) self.instance.send('@fil@%s' % path) os.remove(path) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd)
def tips(self): # 提示命令 cmd = 'tips' myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: self.instance.send('操作指令:\n' + str(self.cmds)) self.instance.send('远程操作指令:\n' + str(self.remoteCmds)) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason)
def replyGroupChat(msg): if msg['FromUserName'] == self.userName: # 自己发送的群消息 myLogging(self.chatroomDir + '%s.txt' % msg['User']['NickName'], self.nickName, infoType='send', other=msg['ActualNickName'], info='[%s]' % msg['User']['NickName'] + msg['Text']) else: # 接收的群消息 if msg['isAt']: # 当有人@我的 self.instance.send('谁在@我吖,我等会就来...', toUserName=msg['FromUserName']) try: myLogging(self.chatroomDir + '%s.txt' % msg['User']['NickName'], self.nickName, infoType='recv', other=msg['ActualNickName'], info='[%s]' % msg['User']['NickName'] + msg['Text']) except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', info=reason)
def delUser(self, nickName): # 删除用户 cmd = 'delUser-%s' % nickName myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: if self.mysqlHelper.getOne('users', 'nickName', nickName): if nickName in onlineDict: del onlineDict[nickName] self.mysqlHelper.delete('users', 'nickName', nickName) myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd, status='success', info='成功删除用户:%s' % nickName) self.instance.send('成功删除用户:%s' % (nickName)) return except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) self.instance.send('删除用户失败:%s' % (nickName), reason)
def remoteLogin(self, frdNickName): # 远程登录 cmd = 'remoteLogin-%s' % frdNickName myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) user = self.mysqlHelper.getOne('users', 'nickName', frdNickName) frdUserName = self.instance.search_friends(name=frdNickName)[0]['UserName'] if user: if (frdNickName in onlineDict.keys() ) and onlineDict[frdNickName].instance.alive: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='该用户已成功登录') self.instance.send('您已成功登录!', toUserName=frdUserName) else: try: del onlineDict[frdNickName] except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) onlineDict[frdNickName] = User(frdNickName) qrPath = os.path.dirname( os.path.abspath(__file__)) + '/users/%s/QR.png' % frdNickName if os.path.exists(qrPath): os.remove(qrPath) onlineDict[frdNickName].loading = True for i in range(30): if onlineDict[frdNickName].instance.alive: myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) self.instance.send('登录成功', toUserName=frdUserName) return if os.path.exists(qrPath): self.instance.send_image(qrPath, toUserName=frdUserName) self.instance.send('请扫描二维码登录,或在手机上确认登录', toUserName=frdUserName) myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd, status='success', info='二维码发送成功,收件人:%s' % frdNickName) return time.sleep(1) myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='连接失败:%s' % frdNickName) self.instance.send('连接超时,请稍后重试', toUserName=frdUserName) return else: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='该用户还未注册:%s' % frdNickName) self.instance.send('该用户还未注册:%s' % frdNickName, toUserName=frdUserName) return
def getAttr(self, nickName, attr=None): # 获取属性值 cmd = 'getAttr-%s-%s' % (nickName, attr) myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if attr: try: if nickName in onlineDict.keys(): value = getattr(onlineDict[nickName], attr) else: value = self.mysqlHelper.getOne('users', 'nickName', nickName)[attr] myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) self.instance.send('%s.%s = %s' % (nickName, attr, value)) except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) self.instance.send('%s.%s属性不存在' % (nickName, attr)) else: try: if nickName in onlineDict.keys(): attrs = self.__dict__.keys() info = ''.join([ '%s = %s\n' % (attr, getattr(onlineDict[nickName], attr)) for attr in attrs ]) # info = 'nickName = %s\n' \ # 'userType = %s\n' \ # 'autoLogin = %s\n' \ # 'autoReply = %s\n' \ # 'loading = %s\n' \ # 'online = %s\n' \ # 'receiveMsg = %s\n' \ # 'uin = %s\n' \ # 'robotNickName = %s\n' \ # 'autoReplyGroup = %s\n' \ # 'cmds = %s' % (onlineDict[nickName].nickName, onlineDict[nickName].userType, # onlineDict[nickName].autoLogin, onlineDict[nickName].autoReply, # onlineDict[nickName].loading, onlineDict[nickName].online, # onlineDict[nickName].receiveMsg, onlineDict[nickName].uin, # onlineDict[nickName].robotNickName, onlineDict[nickName].autoReplyGroup, # onlineDict[nickName].cmds) print('users line 422 info =', info) else: userInfo = self.mysqlHelper.getOne('users', 'nickName', nickName) userType = userInfo['userType'] autoLogin = userInfo['autoLogin'] autoReply = userInfo['autoReply'] autoReplyGroup = userInfo['autoReplyGroup'] info = 'nickName = %s\n' \ 'userType = %s\n' \ 'autoLogin = %s\n' \ 'autoReply = %s\n' \ 'autoReplyGroup = %s' % (nickName, userType, autoLogin, autoReply, autoReplyGroup) self.instance.send(info, toUserName=self.userName) except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason)
def addUser(self, nickName, userType): # 添加用户 cmd = 'addUser-%s-%s' % (nickName, userType) myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) try: userType = int(userType) except TypeError as reason: self.instance.send('用户类型输入错误') myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) return if self.userType < userType: if self.mysqlHelper.getOne('users', column='nickName', value=nickName): myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='当前用户已存在') self.instance.send('当前用户已存在') return else: if nickName not in [each['NickName'] for each in self.frdInfoList]: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='当前用户不在您的好友名单中') self.instance.send('当前用户不在您的好友名单中') return if userType not in CONFIG.userTypes: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='用户类型输入错误') self.instance.send('用户类型输入错误') return try: self.mysqlHelper.insertOne('users', nickName=nickName, userType=userType) self.setDefault(nickName, userType) myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd, status='success', info='成功添加新用户:%s' % nickName) self.instance.send('成功添加新用户:%s' % nickName) return except Exception as reason: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info=reason) self.instance.send('%s' % str(reason)) return else: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='您的权限不足') self.instance.send('您的权限不足')
def delReply(self, nickName, chatroom=None): # 删除自动回复成员 if chatroom: cmd = 'delReply %s chatroom' % nickName myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) chatroomList = self.getChatrooms() if nickName in [each['NickName'] for each in chatroomList]: memberList = self.getChatroomMembers(nickName)['MemberList'] friendsInChatroom = [ each['NickName'] for each in memberList if each['NickName'] in [each['NickName'] for each in self.frdInfoList[1:]] ] friendsInChatroom = [ nickName for nickName in friendsInChatroom if nickName in self.autoReplyGroup ] if friendsInChatroom: self.autoReplyGroup = list( set(friendsInChatroom) ^ set(self.autoReplyGroup)) self.mysqlHelper.update( 'users', 'autoReplyGroup', self.separator.join(self.autoReplyGroup), 'nickName', self.nickName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) self.instance.send('成功!群[%s]中的好友成功加入自动回复名单' % nickName, toUserName=self.userName) else: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='群[%s]中的好友不在自动回复名单中' % nickName) self.instance.send('失败!群[%s]中的好友不在自动回复名单中' % nickName, toUserName=self.userName) else: myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='[%s]不在您的群名单中' % nickName) self.instance.send('失败![%s]不在您的群名单中' % nickName, toUserName=self.userName) else: cmd = 'delReply %s' % nickName myLogging(self.cmdFile, self.nickName, infoType='cmd', other=cmd) if nickName in self.autoReplyGroup: # 删除指定成员 self.autoReplyGroup.remove(nickName) self.mysqlHelper.update('users', 'autoReplyGroup', self.separator.join(self.autoReplyGroup), 'nickName', self.nickName) self.instance.send('成功!好友[%s]从自动回复名单中删除' % nickName, toUserName=self.userName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) elif nickName == 'all': # 删除所有成员 self.autoReplyGroup = [] self.mysqlHelper.update('users', 'autoReplyGroup', self.separator.join(self.autoReplyGroup), 'nickName', self.nickName) self.instance.send('成功![所有好友]从自动回复名单中删除', toUserName=self.userName) myLogging(self.cmdFile, self.nickName, infoType='cmd', status='success', other=cmd) else: # 错误指令 myLogging(self.errFile, self.nickName, infoType='err', other=cmd, info='好友[%s]不在自动回复名单中' % nickName) self.instance.send('失败!好友[%s]不在自动回复名单中' % nickName, toUserName=self.userName)