コード例 #1
0
    def downloadAlbum(self):
        while True:
            print("----------------ALBUM------------------")
            sID = printChoice("Enter AlbumID(Enter '0' go back):", True, 0)
            if sID == 0:
                return

            aAlbumInfo = self.tool.getAlbum(sID)
            if self.tool.errmsg != "":
                printErr(0, "Get AlbumInfo Err! " + self.tool.errmsg)
                continue

            print("[Title]       %s" % (aAlbumInfo['title']))
            print("[SongNum]     %s\n" % (aAlbumInfo['numberOfTracks']))

            # Get Tracks
            aAlbumTracks = self.tool.getAlbumTracks(sID)
            if self.tool.errmsg != "":
                printErr(0, "Get AlbumTracks Err!" + self.tool.errmsg)
                return
            # Creat OutputDir
            targetDir = self.__creatAlbumDir(aAlbumInfo)
            # write msg
            string = self.tool.convertAlbumInfoToString(
                aAlbumInfo, aAlbumTracks)
            with open(targetDir + "/AlbumInfo.txt", 'w',
                      encoding='utf-8') as fd:
                fd.write(string)
            # download cover
            coverPath = targetDir + '/' + pathHelper.replaceLimitChar(
                aAlbumInfo['title'], '-') + '.jpg'
            coverUrl = self.tool.getAlbumArtworkUrl(aAlbumInfo['cover'])
            netHelper.downloadFile(coverUrl, coverPath)
            # check exist files
            redownload = True
            existFiles = pathHelper.getDirFiles(targetDir)
            for item in existFiles:
                if '.txt' in item:
                    continue
                if '.jpg' in item:
                    continue
                check = printChoice(
                    "Some TrackFile Exist.Is Redownload?(y/n):")
                if check != 'y' and check != 'yes':
                    redownload = False
                break
            # download album tracks
            for item in aAlbumTracks['items']:
                streamInfo = self.tool.getStreamUrl(str(item['id']),
                                                    self.config.quality)
                if self.tool.errmsg != "":
                    printErr(
                        14, item['title'] + "(Get Stream Url Err!" +
                        self.tool.errmsg + ")")
                    continue

                fileType = self._getSongExtension(streamInfo['url'])
                filePath = self.__getAlbumSongSavePath(targetDir, aAlbumInfo,
                                                       item, fileType)
                paraList = {
                    'album': aAlbumInfo,
                    'redownload': redownload,
                    'title': item['title'],
                    'trackinfo': item,
                    'url': streamInfo['url'],
                    'path': filePath,
                    'retry': 3,
                    'key': streamInfo['encryptionKey']
                }
                self.thread.start(self.__thradfunc_dl, paraList)
            # wait all download thread
            self.thread.waitAll()
            self.tool.removeTmpFile(targetDir)
        return
コード例 #2
0
    def downloadAlbum(self, album_id=None, redl_flag=None):
        while_count = 9999
        while while_count > 0:
            while_count -= 1

            if album_id is not None:
                while_count = 0
                sID = album_id
            else:
                print("----------------ALBUM------------------")
                sID = printChoice("Enter AlbumID(Enter '0' go back):", True, 0)
                if sID == 0:
                    return

            aAlbumInfo = self.tool.getAlbum(sID)
            if self.tool.errmsg != "":
                printErr(0, "Get AlbumInfo Err! " + self.tool.errmsg)
                continue

            print("[Title]       %s" % (aAlbumInfo['title']))
            print("[SongNum]     %s\n" % (aAlbumInfo['numberOfTracks']))

            # Get Tracks
            aAlbumTracks = self.tool.getAlbumTracks(sID)
            if self.tool.errmsg != "":
                printErr(0, "Get AlbumTracks Err!" + self.tool.errmsg)
                continue
            aAlbumVideos = self.tool.getAlbumVideos(sID)

            # Creat OutputDir
            targetDir = self.__creatAlbumDir(aAlbumInfo)
            # write msg
            string = self.tool.convertAlbumInfoToString(
                aAlbumInfo, aAlbumTracks)
            with codecs.open(targetDir + "/AlbumInfo.txt", 'w', 'utf-8') as fd:
                fd.write(string)
            # download cover
            coverPath = targetDir + '/' + pathHelper.replaceLimitChar(
                aAlbumInfo['title'], '-') + '.jpg'
            if aAlbumInfo['cover'] is not None:
                coverUrl = self.tool.getAlbumArtworkUrl(aAlbumInfo['cover'])
                netHelper.downloadFile(coverUrl, coverPath)
            # check exist files
            redownload = True
            if redl_flag is None:
                existFiles = pathHelper.getDirFiles(targetDir)
                for item in existFiles:
                    if '.txt' in item:
                        continue
                    if '.jpg' in item:
                        continue
                    check = printChoice(
                        "Some tracks already exist. Redownload?(y/n):")
                    if not cmdHelper.isInputYes(check):
                        redownload = False
                    break
            else:
                redownload = redl_flag

            # download album tracks
            for item in aAlbumTracks['items']:
                streamInfo = self.tool.getStreamUrl(str(item['id']),
                                                    self.config.quality)
                if self.tool.errmsg != "" or not streamInfo:
                    printErr(
                        14, item['title'] + "(Get Stream Url Err!" +
                        self.tool.errmsg + ")")
                    continue

                fileType = self._getSongExtension(streamInfo['url'])
                filePath = self.__getAlbumSongSavePath(targetDir, aAlbumInfo,
                                                       item, fileType)
                paraList = {
                    'album': aAlbumInfo,
                    'redownload': redownload,
                    'title': item['title'],
                    'trackinfo': item,
                    'url': streamInfo['url'],
                    'path': filePath,
                    'retry': 3,
                    'key': streamInfo['encryptionKey'],
                    'coverpath': coverPath
                }
                self.thread.start(self.__thradfunc_dl, paraList)
            # wait all download thread
            self.thread.waitAll()
            self.tool.removeTmpFile(targetDir)

            # remove cover
            if self.config.savephoto != 'True':
                pathHelper.remove(coverPath)

            # download video

            for item in aAlbumVideos:
                item = item['item']
                filePath = targetDir + '/' + pathHelper.replaceLimitChar(
                    item['title'], '-') + ".mp4"
                filePath = os.path.abspath(filePath)
                if os.access(filePath, 0):
                    os.remove(filePath)

                try:
                    resolutionList, urlList = self.tool.getVideoResolutionList(
                        item['id'])
                    selectIndex = self.__getVideoResolutionIndex(
                        resolutionList)
                    if self.ffmpeg.mergerByM3u8_Multithreading2(
                            urlList[int(selectIndex)],
                            filePath,
                            showprogress=self.showpro):
                        printSUCCESS(14, item['title'])
                    else:
                        printErr(14, item['title'])
                except:
                    printErr(14, item['title'])
            # return

        return