♻️ refactor downloadTrack

This commit is contained in:
oskvr37
2024-07-26 22:27:45 +02:00
parent 6087d610c5
commit a6156bf06e
2 changed files with 27 additions and 16 deletions
+3 -4
View File
@@ -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__":
+24 -12
View File
@@ -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