def downloadLyric(self,keyword): """Download lyric when necessary.""" listUrl = baiduMusic.generateUrl(keyword) if listUrl: try: templst = baiduMusic.generateList(listUrl)[1:4] suffix,title,artist = templst[0][0],templst[1][0],templst[2][0] mp3Url = 'http://music.baidu.com/' + suffix baiduMusic.downloadLrcFile(mp3Url,title,artist,self.lrcLocation) except: pass
def searchFunc(self): """Search keywords input by user and display search results.""" keywords = str(self.ui.keywordEdit.text()) # 若不做str转换,默认是Qstring对象,不可使用split()等方法 if keywords: self.ui.searchButton.setEnabled(True) if not keywords: title = '悟空' text = '你又顽皮了' self.popMessage(title,text) return listUrl = baiduMusic.generateUrl(keywords) if listUrl: result = baiduMusic.generateList(listUrl) try: # 有搜索结果 seq,suffix,title,artist,album = result except TypeError: # 无网络连接时,自动退出程序 if str(result).isdigit(): if result == 10: title = '网络错误' text = '无法访问互联网,请检查网络连接' res = self.popMessage(title,text) if res == 1: self.close() return # 网络通畅时,根据返回值做不同处理 # 没有搜到结果 elif result == 20: title = '没有搜索结果' text = '没有找到匹配结果,请修改关键词重试' res = self.popMessage(title,text) if res == 1: self.ui.keywordEdit.setFocus() self.ui.keywordEdit.selectAll() return self.ui.infoTreeWidget.clear() # 切换到显示搜索结果的tab self.ui.tabWidget.setCurrentIndex(1) i = 0 for i in xrange(0,15): a = QtGui.QTreeWidgetItem(self.ui.infoTreeWidget) try: a.setText(0,seq[i].decode('utf-8')) a.setText(1,title[i].decode('utf-8')) a.setText(2,artist[i].decode('utf-8')) a.setText(3,album[i].decode('utf-8')) # 设置checkbox #a.setFlags(a.flags() & ~QtCore.Qt.ItemIsUserCheckable) a.setCheckState(0,QtCore.Qt.Unchecked) except: a.setText(0,'') a.setText(1,'') a.setText(2,'') a.setText(3,'') a = QtGui.QTreeWidgetItem(self.ui.infoTreeWidget) a.setText(1,'全选'.decode('utf-8')) a.setCheckState(0,QtCore.Qt.Unchecked) self.suffix = suffix self.title = title self.artist = artist if self.suffix: self.ui.downloadButton.setEnabled(True)