def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, partSize=1048576): try: msg, stream = API.getStreamUrl(track.id, CONF.audioQuality) if not aigpy.string.isNull(msg) or stream is None: Printf.err(track.title + "." + msg) return False, msg if CONF.showTrackInfo: Printf.track(track, stream) if userProgress is not None: userProgress.updateStream(stream) path = getTrackPath(CONF, track, stream, album, playlist) # check exist if skip(path, stream.url): Printf.success( aigpy.path.getFileName(path) + " (skip:already exists!)") return True, "" # download logging.info("[DL Track] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.url) tool = aigpy.download.DownloadTool(path + '.part', [stream.url]) tool.setUserProgress(userProgress) tool.setPartSize(partSize) check, err = tool.start(CONF.showProgress) if not check: Printf.err("Download failed! " + aigpy.path.getFileName(path) + ' (' + str(err) + ')') return False, str(err) # encrypted -> decrypt and remove encrypted file encrypted(stream, path + '.part', path) # convert path = convert(path, stream) # contributors msg, contributors = API.getTrackContributors(track.id) msg, tidalLyrics = API.getLyrics(track.id) lyrics = '' if tidalLyrics is None else tidalLyrics.subtitles if CONF.lyricFile: if tidalLyrics is None: Printf.info(f'Failed to get lyrics from tidal!"{track.title}"') else: lrcPath = path.rsplit(".", 1)[0] + '.lrc' aigpy.fileHelper.write(lrcPath, tidalLyrics.subtitles, 'w') setMetaData(track, album, path, contributors, lyrics) Printf.success(aigpy.path.getFileName(path)) return True, "" except Exception as e: Printf.err("Download failed! " + track.title + ' (' + str(e) + ')') return False, str(e)
def __downloadTrack__(conf: Settings, track: Track, album=None, playlist=None): try: if track.allowStreaming is False: Printf.err("Download failed! " + track.title + ' not allow streaming.') return msg, stream = API.getStreamUrl(track.id, conf.audioQuality) Printf.track(track, stream) if not aigpy.string.isNull(msg) or stream is None: Printf.err(track.title + "." + msg) return path = __getTrackPath__(conf, track, stream, album, playlist) # check exist if conf.checkExist and __isNeedDownload__(path, stream.url) == False: Printf.success( aigpy.path.getFileName(path) + " (skip:already exists!)") return logging.info("[DL Track] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.url) tool = aigpy.download.DownloadTool(path + '.part', [stream.url]) check, err = tool.start(conf.showProgress) if not check: Printf.err("Download failed! " + aigpy.path.getFileName(path) + ' (' + str(err) + ')') return # encrypted -> decrypt and remove encrypted file if aigpy.string.isNull(stream.encryptionKey): os.replace(path + '.part', path) else: key, nonce = decrypt_security_token(stream.encryptionKey) decrypt_file(path + '.part', path, key, nonce) os.remove(path + '.part') path = __convertToM4a__(path, stream.codec) # contributors contributors = API.getTrackContributors(track.id) lyrics = '' if conf.addLyrics: lyrics = __getLyrics__(track.title, track.artists[0].name, conf.lyricsServerProxy) __setMetaData__(track, album, path, contributors, lyrics) Printf.success(aigpy.path.getFileName(path)) except Exception as e: Printf.err("Download failed! " + track.title + ' (' + str(e) + ')')
def __track__(conf, obj): Printf.track(obj) msg, album = API.getAlbum(obj.album.id) if conf.saveCovers: __downloadCover__(conf, album) __downloadTrack__(conf, obj, album)