def set_media_source_at_row(self, row, clickPlayFlag=False): if not self.playlist.length() or row < 0: return self.stop_music() self.clickPlayFlag = clickPlayFlag self.playlist.set_current_row(row) sourcePath = self.playlist.get_music_path_at(row) self.title = self.playlist.get_music_title_at(row) self.sourceTrace = "local" if self.playlist.get_music_id_at(row) == Configures.LocalMusicId else "online" self.artistName, self.musicName = get_artist_and_musicname_from_title(self.title) self.playlistName = self.playlist.get_name() self.totalTime = self.playlist.get_music_time_at(row) self.album = self.playlist.get_music_album_at(row) self.errorType = Configures.NoError isAnUrl = False if not os.path.exists(sourcePath): if self.playlist.get_name() == Configures.PlaylistOnline: if sourcePath == Configures.NoLink: musicId = self.playlist.get_music_id_at(row) sourcePath = SearchOnline.get_song_link(musicId) if sourcePath: self.playlist.set_music_path_at(row, sourcePath) else: self.errorType = Configures.UrlError isAnUrl = True else: self.errorType = Configures.PathError sourcePath = "/usr/share/sounds/error_happened.ogg" if self.errorType == Configures.NoError: self.sourcePath = sourcePath self.musicFileName = get_base_name_from_path(sourcePath) self.playedDate = get_time_of_now() self.songinfosManager.update_datas_of_item( self.musicFileName, self.playedDate, self.musicName, self.artistName, self.totalTime, self.album, self.playlistName, ) if not self.timerFlag: self.timerFlag = True self.timeSpan = 0 if isAnUrl: url = QUrl(sourcePath) else: url = QUrl.fromLocalFile(sourcePath) self.play_from_url(url) else: self.timerFlag = False self.dont_hide_main_window_signal.emit() if self.errorType == Configures.DisnetError: QMessageBox.critical( self, "错误", "联网出错!\n无法联网播放歌曲'%s'!\n您最好在网络畅通时下载该曲目!" % self.playlist.get_music_title_at(row) ) elif self.errorType == Configures.PathError: QMessageBox.information(self, "提示", "路径'%s'无效,请尝试重新下载并添加对应歌曲!" % self.playlist.get_music_path_at(row))
def set_media_source_at_row(self, row, clickPlayFlag=False): if not self.playlist.length() or row < 0: return self.stop_music() self.clickPlayFlag = clickPlayFlag self.playlist.set_current_row(row) sourcePath = self.playlist.get_music_path_at(row) self.title = self.playlist.get_music_title_at(row) self.sourceTrace = 'local' if self.playlist.get_music_id_at( row) == Configures.LocalMusicId else 'online' self.artistName, self.musicName = get_artist_and_musicname_from_title( self.title) self.playlistName = self.playlist.get_name() self.totalTime = self.playlist.get_music_time_at(row) self.album = self.playlist.get_music_album_at(row) self.errorType = Configures.NoError isAnUrl = False if not os.path.exists(sourcePath): if self.playlist.get_name() == Configures.PlaylistOnline: if sourcePath == Configures.NoLink: musicId = self.playlist.get_music_id_at(row) sourcePath = SearchOnline.get_song_link(musicId) if sourcePath: self.playlist.set_music_path_at(row, sourcePath) else: self.errorType = Configures.UrlError isAnUrl = True else: self.errorType = Configures.PathError sourcePath = "/usr/share/sounds/error_happened.ogg" if self.errorType == Configures.NoError: self.sourcePath = sourcePath self.musicFileName = get_base_name_from_path(sourcePath) self.playedDate = get_time_of_now() self.songinfosManager.update_datas_of_item( self.musicFileName, self.playedDate, self.musicName, self.artistName, self.totalTime, self.album, self.playlistName) if not self.timerFlag: self.timerFlag = True self.timeSpan = 0 if isAnUrl: url = QUrl(sourcePath) else: url = QUrl.fromLocalFile(sourcePath) self.play_from_url(url) else: self.timerFlag = False self.dont_hide_main_window_signal.emit() if self.errorType == Configures.DisnetError: QMessageBox.critical( self, "错误", "联网出错!\n无法联网播放歌曲'%s'!\n您最好在网络畅通时下载该曲目!" % self.playlist.get_music_title_at(row)) elif self.errorType == Configures.PathError: QMessageBox.information( self, "提示", "路径'%s'无效,请尝试重新下载并添加对应歌曲!" % self.playlist.get_music_path_at(row))
def searchtable_clicked(self, row): 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() musicId = self.searchTable.item(row, 4).text() songLink = SearchOnline.get_song_link(musicId) if not songLink: return thread = DownloadLrcThread([(title, musicId)]) thread.setDaemon(True) thread.setName('downloadLrc') thread.start() self.listen_online_signal.emit(title, album, songLink, musicId)
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()