feat: a colorful download interface to aria2pFD

This commit is contained in:
teddy171 2023-01-26 19:03:32 +08:00
parent 08da8657b7
commit 6175fa7066

View File

@ -5,6 +5,10 @@ import re
import subprocess import subprocess
import sys import sys
import time import time
try:
import aria2p
except ModuleNotFoundError:
pass
from .common import FileDownloader from .common import FileDownloader
from ..compat import ( from ..compat import (
@ -19,6 +23,7 @@ from ..utils import (
cli_configuration_args, cli_configuration_args,
encodeFilename, encodeFilename,
encodeArgument, encodeArgument,
format_bytes,
handle_youtubedl_headers, handle_youtubedl_headers,
check_executable, check_executable,
is_outdated_version, is_outdated_version,
@ -212,18 +217,27 @@ class Aria2pFD(ExternalFD):
AVAILABLE_OPT = 'show' AVAILABLE_OPT = 'show'
def _call_downloader(self, tmpfilename, info_dict): def _call_downloader(self, tmpfilename, info_dict):
import aria2p try:
aria2 = aria2p.API( aria2 = aria2p.API(
aria2p.Client( aria2p.Client(
host="http://localhost", host="http://localhost",
port=6800, port=6800,
secret="" secret=""
)
) )
) except NameError as exc:
raise ModuleNotFoundError("No moudle named aria2p") from exc
# ANSI colors
color_code = tuple(
f"\033[{i}m"
for i in range(30, 38)
)
pre_len = -1
options = dict() options = dict()
options["min-split-size"] = "1M" options["min-split-size"] = "1M"
options["max-connection-per-server"] = 4 options["max-connection-per-server"] = 4
options["auto-file-renaming"] = "false"
dn = os.path.dirname(tmpfilename) dn = os.path.dirname(tmpfilename)
if dn: if dn:
options["dir"] = dn options["dir"] = dn
@ -241,8 +255,27 @@ class Aria2pFD(ExternalFD):
download = aria2.add_uris([info_dict['url']], options) download = aria2.add_uris([info_dict['url']], options)
while download.status == "active": while download.status == "active":
download = aria2.get_download(download.gid) download = aria2.get_download(download.gid)
print(download.progress_string()) message = (
print(download.status) f"{color_code[5]}["
f"{color_code[0]}#{download.gid[:7]} "
f"{format_bytes(download.completed_length)}/{format_bytes(download.total_length)}"
f"{color_code[6]}({download.progress/100:.0%}) "
f"{color_code[0]}CN:{download.connections} "
f"{color_code[0]}DL:{color_code[2]}{format_bytes(download.download_speed)} "
f"{color_code[0]}ETA:{color_code[6]}{download.eta}"
f"{color_code[5]}]"
)
if pre_len > len(message):
message += " " * (pre_len - len(message))
pre_len = len(message)
message += '\r'
print(message, end="", flush=True)
time.sleep(.5)
if pre_len != -1:
print(" " * pre_len, '\r', end="", flush=True)
if download.status == "complete":
return 0
return 1
class HttpieFD(ExternalFD): class HttpieFD(ExternalFD):