From 7c315da6f4509b48cc1e35f5e3092420a43982b4 Mon Sep 17 00:00:00 2001
From: logykk <joshbelcher00@gmail.com>
Date: Wed, 23 Mar 2022 19:05:40 +1300
Subject: [PATCH 1/2] Proper fix for incomplete downloads

---
 CHANGELOG.md      | 3 +++
 setup.py          | 2 +-
 zotify/podcast.py | 5 ++++-
 zotify/track.py   | 5 ++++-
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 70294d6..c7c3875 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
 # Changelog
 
+## v0.6.5
+- Implemented more stable fix for bug still persisting after v0.6.4
+
 ## v0.6.4
 - Fixed upstream bug causing tracks to not download fully
 
diff --git a/setup.py b/setup.py
index b5a8292..45a0937 100644
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@ README = (HERE / "README.md").read_text()
 # This call to setup() does all the work
 setup(
     name="zotify",
-    version="0.6.4",
+    version="0.6.5",
     author="Zotify Contributors",
     description="A music and podcast downloader.",
     long_description=README,
diff --git a/zotify/podcast.py b/zotify/podcast.py
index adabc4a..f50d117 100644
--- a/zotify/podcast.py
+++ b/zotify/podcast.py
@@ -118,10 +118,13 @@ def download_episode(episode_id) -> None:
                 unit_divisor=1024
             ) as p_bar:
                 prepare_download_loader.stop()
-                for _ in range(int(total_size / Zotify.CONFIG.get_chunk_size()) + 2):
+                while True:
+                #for _ in range(int(total_size / Zotify.CONFIG.get_chunk_size()) + 2):
                     data = stream.input_stream.stream().read(Zotify.CONFIG.get_chunk_size())
                     p_bar.update(file.write(data))
                     downloaded += len(data)
+                    if data == b'':
+                        break
                     if Zotify.CONFIG.get_download_real_time():
                         delta_real = time.time() - time_start
                         delta_want = (downloaded / total_size) * (duration_ms/1000)
diff --git a/zotify/track.py b/zotify/track.py
index e031378..81cfccd 100644
--- a/zotify/track.py
+++ b/zotify/track.py
@@ -221,10 +221,13 @@ def download_track(mode: str, track_id: str, extra_keys=None, disable_progressba
                             unit_divisor=1024,
                             disable=disable_progressbar
                     ) as p_bar:
-                        for _ in range(int(total_size / Zotify.CONFIG.get_chunk_size()) + 2):
+                        while True:
+                        #for _ in range(int(total_size / Zotify.CONFIG.get_chunk_size()) + 2):
                             data = stream.input_stream.stream().read(Zotify.CONFIG.get_chunk_size())
                             p_bar.update(file.write(data))
                             downloaded += len(data)
+                            if data == b'':
+                                break
                             if Zotify.CONFIG.get_download_real_time():
                                 delta_real = time.time() - time_start
                                 delta_want = (downloaded / total_size) * (duration_ms/1000)

From e052e13584b9d065e397fbefc5f69026982d6930 Mon Sep 17 00:00:00 2001
From: logykk <joshbelcher00@gmail.com>
Date: Wed, 23 Mar 2022 20:01:26 +1300
Subject: [PATCH 2/2] Basic fault tolerance

---
 zotify/track.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/zotify/track.py b/zotify/track.py
index 81cfccd..20633ae 100644
--- a/zotify/track.py
+++ b/zotify/track.py
@@ -221,13 +221,13 @@ def download_track(mode: str, track_id: str, extra_keys=None, disable_progressba
                             unit_divisor=1024,
                             disable=disable_progressbar
                     ) as p_bar:
-                        while True:
+                        b = 0
+                        while b < 5:
                         #for _ in range(int(total_size / Zotify.CONFIG.get_chunk_size()) + 2):
                             data = stream.input_stream.stream().read(Zotify.CONFIG.get_chunk_size())
                             p_bar.update(file.write(data))
                             downloaded += len(data)
-                            if data == b'':
-                                break
+                            b += 1 if data == b'' else 0
                             if Zotify.CONFIG.get_download_real_time():
                                 delta_real = time.time() - time_start
                                 delta_want = (downloaded / total_size) * (duration_ms/1000)