def checkLogin(): if not isNull(USER.accessToken): #print('Checking Access Token...') #add to translations msg, check = API.verifyAccessToken(USER.accessToken) if check == True: Printf.info( LANG.MSG_VALID_ACCESSTOKEN.format( displayTime(int(USER.expiresAfter - time.time())))) return else: Printf.info(LANG.MSG_INVAILD_ACCESSTOKEN) msg, check = API.refreshAccessToken(USER.refreshToken) if check == True: Printf.success( LANG.MSG_VALID_ACCESSTOKEN.format( displayTime(int(API.key.expiresIn)))) USER.userid = API.key.userId USER.countryCode = API.key.countryCode USER.accessToken = API.key.accessToken USER.expiresAfter = time.time() + int(API.key.expiresIn) UserSettings.save(USER) return else: Printf.err(msg) tmp = UserSettings() #clears saved tokens UserSettings.save(tmp) login() return
def loginByWeb(): start = time.time() elapsed = 0 while elapsed < API.key.authCheckTimeout: elapsed = time.time() - start msg, check = API.checkAuthStatus() if not check: if msg == "pending": time.sleep(API.key.authCheckInterval + 1) continue return False if check: Printf.success( LANG.MSG_VALID_ACCESSTOKEN.format( displayTime(int(API.key.expiresIn)))) TOKEN.userid = API.key.userId TOKEN.countryCode = API.key.countryCode TOKEN.accessToken = API.key.accessToken TOKEN.refreshToken = API.key.refreshToken TOKEN.expiresAfter = time.time() + int(API.key.expiresIn) TokenSettings.save(TOKEN) return True Printf.err(LANG.AUTH_TIMEOUT) return False
def downloadVideo(video: Video, album=None, playlist=None): msg, stream = API.getVideoStreamUrl(video.id, CONF.videoQuality) Printf.video(video, stream) if not aigpy.string.isNull(msg): Printf.err(video.title + "." + msg) return False, msg path = getVideoPath(CONF, video, album, playlist) logging.info("[DL Video] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.m3u8Url) m3u8content = requests.get(stream.m3u8Url).content if m3u8content is None: Printf.err(video.title + ' get m3u8 content failed.') return False, "Get m3u8 content failed" urls = aigpy.m3u8.parseTsUrls(m3u8content) if len(urls) <= 0: Printf.err(video.title + ' parse ts urls failed.') logging.info("[DL Video] title=" + video.title + "\m3u8Content=" + str(m3u8content)) return False, 'Parse ts urls failed.' check, msg = aigpy.m3u8.downloadByTsUrls(urls, path) # check, msg = aigpy.m3u8.download(stream.m3u8Url, path) if check is True: Printf.success(aigpy.path.getFileName(path)) return True, '' else: Printf.err("\nDownload failed!" + msg + '(' + aigpy.path.getFileName(path) + ')') return False, msg
def loginByConfig(): if aigpy.stringHelper.isNull(TOKEN.accessToken): return False msg, check = API.verifyAccessToken(TOKEN.accessToken) if check: Printf.info( LANG.MSG_VALID_ACCESSTOKEN.format( displayTime(int(TOKEN.expiresAfter - time.time())))) API.key.countryCode = TOKEN.countryCode API.key.userId = TOKEN.userid API.key.accessToken = TOKEN.accessToken return True Printf.info(LANG.MSG_INVAILD_ACCESSTOKEN) msg, check = API.refreshAccessToken(TOKEN.refreshToken) if check: Printf.success( LANG.MSG_VALID_ACCESSTOKEN.format( displayTime(int(API.key.expiresIn)))) TOKEN.userid = API.key.userId TOKEN.countryCode = API.key.countryCode TOKEN.accessToken = API.key.accessToken TOKEN.expiresAfter = time.time() + int(API.key.expiresIn) TokenSettings.save(TOKEN) return True else: tmp = TokenSettings() # clears saved tokens TokenSettings.save(tmp) return False
def __downloadTrack__(conf, track, album=None, playlist=None): msg, stream = API.getStreamUrl(track.id, conf.audioQuality) if not isNull(msg): Printf.err(track.title + "." + msg) return path = __getTrackPath__(conf, track, stream, album, playlist) # Printf.info("Download \"" + track.title + "\" Codec: " + stream.codec) check, err = downloadFileRetErr(stream.url, path + '.part', showprogress=True, stimeout=20) if not check: Printf.err("\n Download failed!" + getFileName(path)) return # encrypted -> decrypt and remove encrypted file if 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) __setMetaData__(track, album, path) Printf.success(getFileName(path))
def login(): print(LANG.AUTH_START_LOGIN) msg, check = API.getDeviceCode() if check == False: Printf.err(msg) return print(LANG.AUTH_LOGIN_CODE.format(green(API.key.userCode))) print(LANG.AUTH_NEXT_STEP.format(green(API.key.verificationUrl), yellow(displayTime(API.key.authCheckTimeout)))) print(LANG.AUTH_WAITING) start = time.time() elapsed = 0 while elapsed < API.key.authCheckTimeout: elapsed = time.time() - start # print("Check auth status...") msg, check = API.checkAuthStatus() if check == False: if msg == "pending": time.sleep(API.key.authCheckInterval + 1) continue Printf.err(msg) break if check == True: Printf.success(LANG.MSG_VALID_ACCESSTOKEN.format(displayTime(int(API.key.expiresIn)))) TOKEN.userid = API.key.userId TOKEN.countryCode = API.key.countryCode TOKEN.accessToken = API.key.accessToken TOKEN.refreshToken = API.key.refreshToken TOKEN.expiresAfter = time.time() + int(API.key.expiresIn) TokenSettings.save(TOKEN) break if elapsed >= API.key.authCheckTimeout: Printf.err(LANG.AUTH_TIMEOUT) return
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 __downloadVideo__(conf, video, album=None, playlist=None): msg, stream = API.getVideoStreamUrl(video.id, conf.videoQuality) if not isNull(msg): Printf.err(video.title + "." + msg) return path = __getVideoPath__(conf, video, album, playlist) if m3u8Helper.download(stream.m3u8Url, path): Printf.success(getFileName(path)) else: Printf.err("\nDownload failed!" + getFileName(path))
def __downloadVideo__(conf, video, album=None, playlist=None): msg, stream = API.getVideoStreamUrl(video.id, conf.videoQuality) if not aigpy.string.isNull(msg): Printf.err(video.title + "." + msg) return path = __getVideoPath__(conf, video, album, playlist) logging.info("[DL Video] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.m3u8Url) if aigpy.m3u8.download(stream.m3u8Url, path): Printf.success(aigpy.path.getFileName(path)) else: Printf.err("\nDownload failed!" + aigpy.path.getFileName(path))
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 __downloadTrack__(conf: Settings, track, album=None, playlist=None): try: msg, stream = API.getStreamUrl(track.id, conf.audioQuality) if not 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: playSong(track, path) Printf.success(getFileName(path) + " (skip:already exists!)") return # Printf.info("Download \"" + track.title + "\" Codec: " + stream.codec) if conf.multiThreadDownload: check, err = downloadFileMultiThread( stream.url, path + '.part', stimeout=20, showprogress=conf.showProgress) else: check, err = downloadFile(stream.url, path + '.part', stimeout=20, showprogress=conf.showProgress) if not check: Printf.err("Download failed! " + getFileName(path) + ' (' + str(err) + ')') return # encrypted -> decrypt and remove encrypted file if 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) __setMetaData__(track, album, path, contributors) # Printf.success(getFileName()) playSong(track, path) except Exception as e: Printf.err("Download failed! " + track.title + ' (' + str(e) + ')')
def __downloadVideo__(conf, video:Video, album=None, playlist=None): if video.allowStreaming is False: Printf.err("Download failed! " + video.title + ' not allow streaming.') return msg, stream = API.getVideoStreamUrl(video.id, conf.videoQuality) Printf.video(video, stream) if not aigpy.string.isNull(msg): Printf.err(video.title + "." + msg) return path = __getVideoPath__(conf, video, album, playlist) logging.info("[DL Video] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.m3u8Url) check, msg = aigpy.m3u8.download(stream.m3u8Url, path) if check is True: Printf.success(aigpy.path.getFileName(path)) else: Printf.err("\nDownload failed!" + msg + '(' + aigpy.path.getFileName(path) + ')')