diff --git a/tiddl/__init__.py b/tiddl/__init__.py index 2738816..1d50582 100644 --- a/tiddl/__init__.py +++ b/tiddl/__init__.py @@ -4,7 +4,7 @@ import time from .api import TidalApi from .auth import getDeviceAuth, getToken, refreshToken from .config import Config -from .utils import decodeManifest, parseTrackManifest, downloadTrack +from .download import downloadTrack def main(): @@ -58,9 +58,8 @@ def main(): track_id = int(input("Enter track id to download: ")) track = api.getTrack(track_id, config["settings"]["track_quality"]) - decoded_manifest = decodeManifest(track["manifest"]) - track_urls, codecs = parseTrackManifest(decoded_manifest) - downloadTrack(track_id, track_urls) + track_path = downloadTrack(track_id, track["manifest"], config["settings"]["download_path"]) + print(f"✨ Track saved in {track_path}") if __name__ == "__main__": diff --git a/tiddl/utils.py b/tiddl/download.py similarity index 67% rename from tiddl/utils.py rename to tiddl/download.py index abf6de3..8792ef4 100644 --- a/tiddl/utils.py +++ b/tiddl/download.py @@ -48,15 +48,27 @@ def parseTrackManifest(xml_content: str): return urls, codecs -def downloadTrack(track_id: int, urls: list[str]): - # both mp3 and flac extensions work - print("downloading...") - filename = f"{track_id}.flac" - with open(filename, "wb+") as f: - progress = 0 - for url in urls: - progress += 1 - req = requests.get(url) - print(f"{round(progress / len(urls) * 100)}%") - f.write(req.content) - print(f"file saved as ./{filename}") +def threadDownload(urls: list[str]) -> bytes: + # TODO: implement threaded download ⚡️ + + data = b"" + for index, url in enumerate(urls): + req = requests.get(url) + data += req.content + print(f"{round(index / len(urls) * 100)}%") + + return data + + +def downloadTrack(track_id: int, manifest: str, path: str): + decoded_manifest = decodeManifest(manifest) + track_urls, codecs = parseTrackManifest(decoded_manifest) + track_data = threadDownload(track_urls) + + # TODO: use proper file extension ✨ + file_path = f"{path}/{track_id}.flac" + + with open(file_path, "wb+") as f: + f.write(track_data) + + return file_path