Pass shaka-packager & aria CTRL+C to caller as KeyboardInterrupt()s

This commit is contained in:
rlaphoenix 2023-02-28 17:14:02 +00:00
parent ad1990cc42
commit 8365d798a4
2 changed files with 56 additions and 44 deletions

View File

@ -83,53 +83,62 @@ def aria2c(
return aria2c(uri, out, headers, pproxy_) return aria2c(uri, out, headers, pproxy_)
arguments += ["--all-proxy", proxy] arguments += ["--all-proxy", proxy]
p = subprocess.Popen( try:
[executable, *arguments], p = subprocess.Popen(
stdin=subprocess.PIPE, [executable, *arguments],
stderr=[None, subprocess.DEVNULL][silent], stdin=subprocess.PIPE,
stdout=( stderr=[None, subprocess.DEVNULL][silent],
subprocess.PIPE if progress else stdout=(
subprocess.DEVNULL if silent else subprocess.PIPE if progress else
None subprocess.DEVNULL if silent else
), None
universal_newlines=True ),
) universal_newlines=True
)
p._stdin_write(uri) # noqa
p._stdin_write(uri) # noqa if progress:
is_dl_summary = False
for line in iter(p.stdout.readline, ""):
line = line.strip()
if line:
if line.startswith("[") and line.endswith("]"):
if "%" in line:
# id, dledMiB/totalMiB(x%), CN:xx, DL:xxMiB, ETA:Xs
# eta may not always be available
data_parts = line[1:-1].split()
perc_parts = data_parts[1].split("(")
if len(perc_parts) == 2:
# might otherwise be e.g., 0B/0B, with no % symbol provided
progress(
total=100,
completed=int(perc_parts[1][:-2]),
downloaded=f"{data_parts[3].split(':')[1]}/s"
)
elif line.startswith("Download Results"):
# we know it's 100% downloaded, but let's use the avg dl speed value
is_dl_summary = True
elif is_dl_summary and "OK" in line and "|" in line:
gid, status, avg_speed, path_or_uri = line.split("|")
progress(total=100, completed=100, downloaded=avg_speed.strip())
elif not is_dl_summary:
buffer_msg = line.split(" ", maxsplit=2)
buffer_msg = f"[Aria2c]: {buffer_msg[-1].strip()}"
console.log(Text.from_ansi(buffer_msg))
if progress: p.wait()
is_dl_summary = False
for line in iter(p.stdout.readline, ""):
line = line.strip()
if line:
if line.startswith("[") and line.endswith("]"):
if "%" in line:
# id, dledMiB/totalMiB(x%), CN:xx, DL:xxMiB, ETA:Xs
# eta may not always be available
data_parts = line[1:-1].split()
perc_parts = data_parts[1].split("(")
if len(perc_parts) == 2:
# might otherwise be e.g., 0B/0B, with no % symbol provided
progress(
total=100,
completed=int(perc_parts[1][:-2]),
downloaded=f"{data_parts[3].split(':')[1]}/s"
)
elif line.startswith("Download Results"):
# we know it's 100% downloaded, but let's use the avg dl speed value
is_dl_summary = True
elif is_dl_summary and "OK" in line and "|" in line:
gid, status, avg_speed, path_or_uri = line.split("|")
progress(total=100, completed=100, downloaded=avg_speed.strip())
elif not is_dl_summary:
buffer_msg = line.split(" ", maxsplit=2)
buffer_msg = f"[Aria2c]: {buffer_msg[-1].strip()}"
console.log(Text.from_ansi(buffer_msg))
p.wait() if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, arguments)
if p.returncode != 0: except ConnectionResetError:
raise subprocess.CalledProcessError(p.returncode, arguments) # interrupted while passing URI to download
raise KeyboardInterrupt()
except subprocess.CalledProcessError as e:
if e.returncode in (7, 0xC000013A):
# 7 is when Aria2(c) handled the CTRL+C
# 0xC000013A is when it never got the chance to
raise KeyboardInterrupt()
raise
return p.returncode return p.returncode

View File

@ -234,6 +234,7 @@ class Widevine:
decrypted_path = path.with_suffix(f".decrypted{path.suffix}") decrypted_path = path.with_suffix(f".decrypted{path.suffix}")
config.directories.temp.mkdir(parents=True, exist_ok=True) config.directories.temp.mkdir(parents=True, exist_ok=True)
try: try:
subprocess.check_call([ subprocess.check_call([
executable, executable,
@ -253,6 +254,8 @@ class Widevine:
"--temp_dir", config.directories.temp "--temp_dir", config.directories.temp
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if e.returncode == 0xC000013A: # STATUS_CONTROL_C_EXIT
raise KeyboardInterrupt()
raise subprocess.SubprocessError(f"Failed to Decrypt! Shaka Packager Error: {e}") raise subprocess.SubprocessError(f"Failed to Decrypt! Shaka Packager Error: {e}")
path.unlink() path.unlink()