initial bittorrent support

merge-requests/5/head
Lauren Liberda 2021-03-12 03:04:37 +01:00
parent 3426d75467
commit 58538a2c64
6 changed files with 39 additions and 4 deletions

View File

@ -60,6 +60,7 @@ from .utils import (
format_bytes,
formatSeconds,
GeoRestrictedError,
HaruhiDLError,
int_or_none,
ISO3166Utils,
locked_file,
@ -1903,6 +1904,10 @@ class HaruhiDL(object):
fd.add_progress_hook(ph)
if self.params.get('verbose'):
self.to_screen('[debug] Invoking downloader on %r' % info.get('url'))
if info.get('protocol') == 'bittorrent' and not self.params.get('allow_p2p'):
raise HaruhiDLError('Peer-to-peer format got selected, but peer-to-peer '
'downloads are not allowed. '
'Choose different format or add --allow-p2p option')
return fd.download(name, info)
if info_dict.get('requested_formats') is not None:

View File

@ -438,6 +438,8 @@ def _real_main(argv=None):
'geo_bypass': opts.geo_bypass,
'geo_bypass_country': opts.geo_bypass_country,
'geo_bypass_ip_block': opts.geo_bypass_ip_block,
'allow_p2p': opts.allow_p2p if not opts.prefer_p2p else True,
'prefer_p2p': opts.prefer_p2p,
# just for deprecation check
'autonumber': opts.autonumber if opts.autonumber is True else None,
'usetitle': opts.usetitle if opts.usetitle is True else None,

View File

@ -10,6 +10,7 @@ from .rtsp import RtspFD
from .ism import IsmFD
from .external import (
get_external_downloader,
Aria2cFD,
FFmpegFD,
)
@ -26,6 +27,7 @@ PROTOCOL_MAP = {
'f4m': F4mFD,
'http_dash_segments': DashSegmentsFD,
'ism': IsmFD,
'bittorrent': Aria2cFD,
}

View File

@ -182,15 +182,16 @@ class Aria2cFD(ExternalFD):
AVAILABLE_OPT = '-v'
def _make_cmd(self, tmpfilename, info_dict):
cmd = [self.exe, '-c']
cmd = [self.exe or 'aria2c', '-c']
cmd += self._configuration_args([
'--min-split-size', '1M', '--max-connection-per-server', '4'])
dn = os.path.dirname(tmpfilename)
if dn:
cmd += ['--dir', dn]
cmd += ['--out', os.path.basename(tmpfilename)]
for key, val in info_dict['http_headers'].items():
cmd += ['--header', '%s: %s' % (key, val)]
if info_dict['protocol'] != 'bittorrent':
for key, val in info_dict['http_headers'].items():
cmd += ['--header', '%s: %s' % (key, val)]
cmd += self._option('--interface', 'source_address')
cmd += self._option('--all-proxy', 'proxy')
cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')

View File

@ -1410,7 +1410,19 @@ class InfoExtractor(object):
preference -= 0.5
protocol = f.get('protocol') or determine_protocol(f)
proto_preference = 0 if protocol in ['http', 'https'] else (-0.5 if protocol == 'rtsp' else -0.1)
if protocol in ['http', 'https']:
proto_preference = 0
elif protocol == 'rtsp':
proto_preference = -0.5
elif protocol == 'bittorrent':
if self._downloader.params.get('prefer_p2p') is True:
proto_preference = 1
elif self._downloader.params.get('allow_p2p') is True:
proto_preference = -0.1
else:
proto_preference = -2
else:
proto_preference = -0.1
if f.get('vcodec') == 'none': # audio only
preference -= 50

View File

@ -533,6 +533,19 @@ def parseOpts(overrideArguments=None):
'--external-downloader-args',
dest='external_downloader_args', metavar='ARGS',
help='Give these arguments to the external downloader')
downloader.add_option(
'--allow-p2p',
dest='allow_p2p', action='store_true', default=None,
help='Allow downloading by peer-to-peer protocols (BitTorrent). '
'May be a violation of your local laws on file sharing in some cases')
downloader.add_option(
'--forbid-p2p',
dest='allow_p2p', action='store_false', default=None,
help='Explicitly forbid downloading by peer-to-peer protocols')
downloader.add_option(
'--prefer-p2p',
dest='prefer_p2p', action='store_true', default=None,
help='Prefer peer-to-peer formats (implies --allow-p2p)')
workarounds = optparse.OptionGroup(parser, 'Workarounds')
workarounds.add_option(