From c2f57323515e6f5ce4d4474ed8ebc31623d614a9 Mon Sep 17 00:00:00 2001 From: MasterofJOKers Date: Sun, 21 Apr 2024 23:18:54 +0200 Subject: [PATCH] mirror-yt: Handle returncode 1 We switch from `subprocess.run` to manually handling a `Popen` object so we can decide whether a returncode of 1 is fine or now depending on the output we see. Since we want live output during the download, we cannot just capture all the output. Instead, we have to regularly print whatever we captured. Therefore, we need a `Popen` object and need to call `communicate()` with a timeout on it. --- mirror-yt.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/mirror-yt.py b/mirror-yt.py index 128e522..8feac33 100644 --- a/mirror-yt.py +++ b/mirror-yt.py @@ -60,7 +60,28 @@ def download(allow_unknown, base_dir, channel_file): '--write-thumbnail', url ] - subprocess.run(cmd, check=True, cwd=target_dir) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=target_dir) + prev_output = '' + while p.poll() is None: + try: + stdout_data, stderr_data = p.communicate(timeout=0.5) + except subprocess.TimeoutExpired as e: + if e.output: + print(e.output[len(prev_output):].decode('utf-8'), end='') + prev_output = e.output or '' + else: + print(stdout_data[len(prev_output):].decode('utf-8')) + + stdout_lines = stdout_data.decode('utf-8').splitlines() + if p.returncode == 0: + return + if p.returncode == 101: + pass + elif p.returncode == 1 and stdout_lines[-1] and 'Finished downloading playlist' in stdout_lines[-1]: + pass + else: + breakpoint() + raise subprocess.CalledProcessError(returncode=p.returncode, cmd=cmd) sleep_seconds = 5 print(f"Sleeping {sleep_seconds}s")