def add_bunch_to_list(self): """向“在线试听”列表批量添加歌曲。""" selecteds = self.searchTable.selectedIndexes() if not len(selecteds): return self.added_items = [] playlistTemp = Playlist() playlistTemp.fill_list(Configures.PlaylistOnline) musicIdsInOnlineList = playlistTemp.get_items_queue() for index in selecteds: if index.column() == 0: row = index.row() musicId = self.searchTable.item(row, 4).text() if musicId not in musicIdsInOnlineList: musicName = self.searchTable.item(row, 1).text() artist = self.searchTable.item(row, 2).text() title = connect_as_title(artist, musicName) album = self.searchTable.item(row, 3).text() lrcPath = composite_lyric_path_use_title(title) if os.path.exists(lrcPath): os.remove(lrcPath) self.added_items.append([title, musicId]) playlistTemp.add_record(musicId, title, Configures.ZeroTime, album, Configures.NoLink, 0, musicId) if len(self.added_items): playlistTemp.commit_records() thread = DownloadLrcThread(self.added_items) thread.setDaemon(True) thread.setName("downloadLrc") thread.start() self.add_bunch_to_list_succeed.emit()
class PlaylistWidgetBasic(QTableWidget): def __init__(self, listName, parent=None): super(PlaylistWidgetBasic, self).__init__(parent) self.listName = '' self.playlist = Playlist() def get_playlist(self): return self.playlist.copy() def get_operating_playlist_name(self): """获取正在被操作的列表名""" return self.listName def fill_playlist_widget(self, playlist): self.clear_all_items() itemsList = playlist.get_items_queue() group = playlist.get_infos_group() for item in itemsList: title = group[item][0] time = group[item][1] self.add_record(title, time) def clear_all_items(self): while self.rowCount(): self.removeRow(0) def add_record(self, title, time): countRow = self.rowCount() titleItem = QTableWidgetItem(title) self.insertRow(countRow) self.setItem(countRow, 0, titleItem) self.setRowHeight(countRow, 31) def add_record_with_full_info(self, id, title, totalTime, album, path, size, musicId=Configures.LocalMusicId): self.playlist.add_record(id, title, totalTime, album, path, size, musicId) self.add_record(title, totalTime) def add_a_music(self, path): title, totalTime = self.playlist.add_item_from_path(path) self.add_record(title, totalTime)
def run(self): if self.songLink == Configures.NoLink: self.songLink = SearchOnline.get_song_link(self.musicId) if not self.songLink: self.errorHappend('歌曲的网络链接为空') return self.downloadStatus = Configures.Downloading self.print_info('开始下载') self.download_lrc_and_artistinfo(self.title, self.musicId) if self.length == 0: res = self.try_to_open_url(self.songLink) if not res: self.errorHappend('无法打开歌曲链接') else: if res.status == 200 and res.reason == 'OK' and res.getheader( 'Content-Type') == 'audio/mpeg': try: self.length = int(res.getheader('Content-Length')) except ValueError: self.errorHappend('无法获取歌曲的大小') else: self.errorHappend('不是音乐资源类型') if not self.length or self.downloadStatus == Configures.DownloadError: return self.currentLength = self.check_temp_downloaded(self.tempfileName) req = request.Request(self.songLink) req.headers['Range'] = 'bytes= %s-%s' % (self.currentLength, self.length) res = self.try_to_open_url(req) if not res or res.getheader('Content-Type') != 'audio/mpeg': self.errorHappend('无法定位到开始下载处的节点位置') return contentsList = [] while self.currentLength < self.length and self.runPermit and self.noPause: trytimes = 5 while (trytimes): try: contentCache = res.read(BufferBlock) contentsList.append(contentCache) self.currentLength += BufferBlock if self.currentLength > self.length: self.currentLength = self.length break except: time.sleep(0.05) trytimes -= 1 continue if trytimes == 0: self.print_info('下载超时') self.pause() res.close() if self.currentLength == self.length: self.downloadStatus = Configures.DownloadCompleted if self.downloadStatus != Configures.DownloadCancelled: contentsStr = b''.join(contentsList) with open(self.tempfileName, 'ab+') as f: f.write(contentsStr) if self.downloadStatus == Configures.DownloadCompleted: if os.path.exists(self.musicPath): os.remove(self.musicPath) os.rename(self.tempfileName, self.musicPath) write_tags(self.musicPath, self.title, self.album) self.print_info('准备添加到“%s”' % Configures.PlaylistDownloaded) playlistTemp = Playlist() playlistTemp.fill_list(Configures.PlaylistDownloaded) title, album, totalTime = read_music_info(self.musicPath) if self.lock.acquire(): if self.musicPath not in playlistTemp.get_items_queue(): playlistTemp.add_record(self.musicPath, title, totalTime, album, self.musicPath, self.length, self.musicId) playlistTemp.commit_records() self.print_info("已完成下载") self.lock.release()
def run(self): if self.songLink == Configures.NoLink: self.songLink = SearchOnline.get_song_link(self.musicId) if not self.songLink: self.errorHappend('歌曲的网络链接为空') return self.downloadStatus = Configures.Downloading self.print_info('开始下载') self.download_lrc_and_artistinfo(self.title, self.musicId) if self.length == 0: res = self.try_to_open_url(self.songLink) if not res: self.errorHappend('无法打开歌曲链接') else: if res.status == 200 and res.reason == 'OK' and res.getheader('Content-Type') == 'audio/mpeg': try: self.length = int(res.getheader('Content-Length')) except ValueError: self.errorHappend('无法获取歌曲的大小') else: self.errorHappend('不是音乐资源类型') if not self.length or self.downloadStatus == Configures.DownloadError: return self.currentLength = self.check_temp_downloaded(self.tempfileName) req = request.Request(self.songLink) req.headers['Range'] = 'bytes= %s-%s'%(self.currentLength, self.length) res = self.try_to_open_url(req) if not res or res.getheader('Content-Type') != 'audio/mpeg': self.errorHappend('无法定位到开始下载处的节点位置') return contentsList = [] while self.currentLength<self.length and self.runPermit and self.noPause: trytimes = 5 while(trytimes): try: contentCache = res.read(BufferBlock) contentsList.append(contentCache) self.currentLength += BufferBlock if self.currentLength>self.length: self.currentLength = self.length break except: time.sleep(0.05) trytimes -= 1 continue if trytimes == 0: self.print_info('下载超时') self.pause() res.close() if self.currentLength == self.length: self.downloadStatus = Configures.DownloadCompleted if self.downloadStatus != Configures.DownloadCancelled: contentsStr = b''.join(contentsList) with open(self.tempfileName, 'ab+') as f: f.write(contentsStr) if self.downloadStatus == Configures.DownloadCompleted: if os.path.exists(self.musicPath): os.remove(self.musicPath) os.rename(self.tempfileName, self.musicPath) write_tags(self.musicPath, self.title, self.album) self.print_info('准备添加到“%s”'%Configures.PlaylistDownloaded) playlistTemp = Playlist() playlistTemp.fill_list(Configures.PlaylistDownloaded) title, album, totalTime = read_music_info(self.musicPath) if self.lock.acquire(): if self.musicPath not in playlistTemp.get_items_queue(): playlistTemp.add_record(self.musicPath, title, totalTime, album, self.musicPath, self.length, self.musicId) playlistTemp.commit_records() self.print_info("已完成下载") self.lock.release()