Only use captured aria2c output if available

This commit is contained in:
rlaphoenix 2023-03-03 07:54:31 +00:00
parent d964dde4d5
commit 19ca567019
1 changed files with 35 additions and 34 deletions

View File

@ -96,41 +96,42 @@ def aria2c(
) )
p._stdin_write(uri) # noqa p._stdin_write(uri) # noqa
is_dl_summary = False if p.stdout:
aria_log_buffer = "" is_dl_summary = False
for line in iter(p.stdout.readline, ""): aria_log_buffer = ""
line = line.strip() for line in iter(p.stdout.readline, ""):
if line: line = line.strip()
if line.startswith("Download Results"): if line:
# we know it's 100% downloaded, but let's use the avg dl speed value if line.startswith("Download Results"):
is_dl_summary = True # we know it's 100% downloaded, but let's use the avg dl speed value
elif line.startswith("[") and line.endswith("]"): is_dl_summary = True
if progress and "%" in line: elif line.startswith("[") and line.endswith("]"):
# id, dledMiB/totalMiB(x%), CN:xx, DL:xxMiB, ETA:Xs if progress and "%" in line:
# eta may not always be available # id, dledMiB/totalMiB(x%), CN:xx, DL:xxMiB, ETA:Xs
data_parts = line[1:-1].split() # eta may not always be available
perc_parts = data_parts[1].split("(") data_parts = line[1:-1].split()
if len(perc_parts) == 2: perc_parts = data_parts[1].split("(")
# might otherwise be e.g., 0B/0B, with no % symbol provided if len(perc_parts) == 2:
progress( # might otherwise be e.g., 0B/0B, with no % symbol provided
total=100, progress(
completed=int(perc_parts[1][:-2]), total=100,
downloaded=f"{data_parts[3].split(':')[1]}/s" completed=int(perc_parts[1][:-2]),
) downloaded=f"{data_parts[3].split(':')[1]}/s"
elif is_dl_summary and "OK" in line and "|" in line: )
gid, status, avg_speed, path_or_uri = line.split("|") elif is_dl_summary and "OK" in line and "|" in line:
progress(total=100, completed=100, downloaded=avg_speed.strip()) gid, status, avg_speed, path_or_uri = line.split("|")
elif not is_dl_summary: progress(total=100, completed=100, downloaded=avg_speed.strip())
aria_log_buffer += f"{line.strip()}\n" elif not is_dl_summary:
aria_log_buffer += f"{line.strip()}\n"
if aria_log_buffer: if aria_log_buffer:
# wrap to console width - padding - '[Aria2c]: ' # wrap to console width - padding - '[Aria2c]: '
aria_log_buffer = "\n ".join(textwrap.wrap( aria_log_buffer = "\n ".join(textwrap.wrap(
aria_log_buffer.rstrip(), aria_log_buffer.rstrip(),
width=console.width - 20, width=console.width - 20,
initial_indent="" initial_indent=""
)) ))
console.log(Text.from_ansi("\n[Aria2c]: " + aria_log_buffer)) console.log(Text.from_ansi("\n[Aria2c]: " + aria_log_buffer))
p.wait() p.wait()