Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
 def add_item_from_path(self, path):
     title, album, totalTime = read_music_info(path)
     size = os.path.getsize(path)
     self.add_record(path, title, totalTime, album, path, size,
                     Configures.LocalMusicId)
     return title, totalTime
Ejemplo n.º 4
0
 def add_item_from_path(self, path):
     title, album, totalTime =  read_music_info(path)
     size = os.path.getsize(path)
     self.add_record(path, title, totalTime, album, path, size, Configures.LocalMusicId)   
     return title, totalTime