예제 #1
0
 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()
예제 #2
0
 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()
예제 #3
0
 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()
예제 #4
0
 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()