Apply suggestions from code review

Co-authored-by: dirkf <fieldhouse@gmx.net>
This commit is contained in:
teddy171 2023-01-28 14:28:02 +08:00 committed by GitHub
parent 8845e952ae
commit d0915ce1ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 35 deletions

View File

@ -58,7 +58,7 @@ class FileDownloader(object):
_TEST_FILE_SIZE = 10241 _TEST_FILE_SIZE = 10241
params = None params = None
def __init__(self, ydl, params: dict): def __init__(self, ydl, params):
"""Create a FileDownloader object with the given options.""" """Create a FileDownloader object with the given options."""
self.ydl = ydl self.ydl = ydl
self._progress_hooks = [] self._progress_hooks = []

View File

@ -5,10 +5,6 @@ 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 (
@ -214,15 +210,23 @@ class Aria2pFD(ExternalFD):
To use aria2p as downloader, you need to install aria2c and aria2p, aria2p can download with pip. To use aria2p as downloader, you need to install aria2c and aria2p, aria2p can download with pip.
Then run aria2c in the background and enable with the --enable-rpc option. Then run aria2c in the background and enable with the --enable-rpc option.
''' '''
AVAILABLE_OPT = 'show' try:
import aria2p
__avail = True
except ModuleNotFoundError:
__avail = False
def _call_downloader(self, tmpfilename: str, info_dict: dict): @classmethod
def available(cls):
return cls.__avail
def _call_downloader(self, tmpfilename, info_dict):
try: 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: except NameError as exc:
@ -234,10 +238,11 @@ class Aria2pFD(ExternalFD):
for i in range(30, 38) for i in range(30, 38)
) )
pre_len = -1 pre_len = -1
options = dict() options = {
options["min-split-size"] = "1M" 'min-split-size': '1M',
options["max-connection-per-server"] = 4 'max-connection-per-server': 4,
options["auto-file-renaming"] = "false" 'auto-file-renaming': 'false',
}
download_dir = os.path.dirname(tmpfilename) download_dir = os.path.dirname(tmpfilename)
if download_dir: if download_dir:
options["dir"] = download_dir options["dir"] = download_dir
@ -246,31 +251,26 @@ class Aria2pFD(ExternalFD):
options["out"] = os.path.basename(tmpfilename) options["out"] = os.path.basename(tmpfilename)
options["header"] = [] options["header"] = []
for key, val in info_dict['http_headers'].items(): for key, val in info_dict['http_headers'].items():
options["header"].append(f"{key}: {val}") options['header'].append('{0}: {1}'.format(key, val))
download = aria2.add_uris([info_dict['url']], options) download = aria2.add_uris([info_dict['url']], options)
while download.status == "active": status = {
'status': 'downloading',
'filename': self._filename,
'tmpfilename': tmpfilename,
}
started = time.time()
while download.status == 'active':
download = aria2.get_download(download.gid) download = aria2.get_download(download.gid)
message = ( status.update({
f"{color_code[5]}[" 'downloaded_bytes': download.completed_length,
f"{color_code[0]}#{download.gid[:7]} " 'total_bytes': download.total_length,
f"{format_bytes(download.completed_length)}/{format_bytes(download.total_length)}" 'elapsed': time.time() - started,
f"{color_code[6]}({download.progress/100:.0%}) " 'eta': download.eta,
f"{color_code[0]}CN:{download.connections} " 'speed': download.download_speed,
f"{color_code[0]}DL:{color_code[2]}{format_bytes(download.download_speed)} " })
f"{color_code[0]}ETA:{color_code[6]}{download.eta}" self._hook_progress(status)
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) time.sleep(.5)
if pre_len != -1: return int(download.status == 'complete')
print(" " * pre_len, '\r', end="", flush=True)
if download.status == "complete":
return 0
return 1
class HttpieFD(ExternalFD): class HttpieFD(ExternalFD):