async def getFriendList(self): _user = Users('http://{}:{}'.format(Config._host, Config._restPort), Config._restPort_secret) try: ''' friendList的数据格式: {'rosterItem': [{'jid': '[email protected]', 'nickname': 'chinsing00', 'subscriptionType': 3, 'groups': ['Friends']},{...}]} ''' friend_List = _user.get_user_roster( self.core.jid.localpart) #只需要用户名就行,不需要加域名 del _user #清除user,避免服务器生成过多的连接导致服务器连接池溢出 metadata = await self.avatar_server.get_avatar_metadata( self.core.jid) if metadata: picture = await asyncio.ensure_future( metadata[0].get_image_bytes()) picPath = os.path.join( app.getAvatarRootPath(self.core.jid.localpart), '{}.jpg'.format(str(self.core.jid))) if not os.path.exists(picPath): FileUtils.savaToPng(picPath, picture) usericon = QPixmap.fromImage(QImage(picPath)) if os.path.exists( picPath) else QPixmap(":src\images\CustomerService.png") self.mWin.avatar.setBaseSize(QSize(40, 40)) temp = utils.PixmapToRound(self.mWin.avatar, usericon) self.mWin.avatar.setScaledContents(True) self.mWin.avatar.setPixmap(temp) for friend in friend_List['rosterItem']: # task执行后返回 <AbstractAvatarDescriptor> 对象 task = await get_event_loop().create_task( self.avatar_server.get_avatar_metadata(JID.fromstr( friend['jid']), require_fresh=True, disable_pep=False)) #得到<AbstractAvatarDescriptor> 对象后进行头像保存本地及加载好友列表 avatar_path = None if task: bin_data = await ensure_future( task[0].get_image_bytes() ) # get_image_bytes()为协程,result[0]为最新的图片 avatar_path = os.path.join( app.getAvatarRootPath(self.core.jid.localpart), '{}.jpg'.format(friend['jid'])) FileUtils.savaToPng(avatar_path, bin_data) friend['avatar_path'] = avatar_path self.mWin.loadData(friend) except InvalidResponseException: Log.info("RestAPi", "获取好友列表失败")
async def getMembersAvatar(self, memberList): rootPath = os.path.join(app.getAvatarRootPath(self.core.jid.localpart), self.room_jid) if not os.path.exists(rootPath): os.mkdir(rootPath) self.room_path = rootPath for member in memberList: metadata = await asyncio.ensure_future( self._muc_avatarService.get_avatar_metadata( JID.fromstr(member))) if metadata: picture = await asyncio.ensure_future( metadata[0].get_image_bytes()) picPath = os.path.join(rootPath, '{}.jpg'.format(member)) FileUtils.savaToPng(picPath, picture) self.loadMember(memberList)