From 87d59648cf7c35729f9c2ad88b38be44aa89c1b8 Mon Sep 17 00:00:00 2001 From: Nathan Thomas Date: Wed, 24 Jan 2024 16:05:46 -0800 Subject: [PATCH] Fix tempfile issue on windows (#596) * Fix tempfile issue on windows * Cleanup * Rename var --- streamrip/client/downloadable.py | 38 ++++++++++++++------------------ 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/streamrip/client/downloadable.py b/streamrip/client/downloadable.py index 4a4e2c0..a4a8f69 100644 --- a/streamrip/client/downloadable.py +++ b/streamrip/client/downloadable.py @@ -133,29 +133,23 @@ class DeezerDownloadable(Downloadable): blowfish_key, ) - assert self.chunk_size == 2048 * 3 + buf = bytearray() + async for data, _ in resp.content.iter_chunks(): + buf += data + callback(len(data)) - # Write data from server to tempfile because there's no - # efficient way to guarantee a fixed chunk size for all iterations - # in async - async with aiofiles.tempfile.TemporaryFile("wb+") as tmp: - async for chunk in resp.content.iter_chunks(): - data, _ = chunk - await tmp.write(data) - callback(len(data)) - - await tmp.seek(0) - async with aiofiles.open(path, "wb") as audio: - while chunk := await tmp.read(self.chunk_size): - if len(chunk) >= 2048: - decrypted_chunk = ( - self._decrypt_chunk(blowfish_key, chunk[:2048]) - + chunk[2048:] - ) - else: - decrypted_chunk = chunk - - await audio.write(decrypted_chunk) + async with aiofiles.open(path, "wb") as audio: + buflen = len(buf) + for i in range(0, buflen, self.chunk_size): + data = buf[i : min(i + self.chunk_size, buflen - 1)] + if len(data) >= 2048: + decrypted_chunk = ( + self._decrypt_chunk(blowfish_key, data[:2048]) + + data[2048:] + ) + else: + decrypted_chunk = data + await audio.write(decrypted_chunk) @staticmethod def _decrypt_chunk(key, data):