From 58538a2c64df63218822735d6bdcb3224298a630 Mon Sep 17 00:00:00 2001 From: Lauren Liberda Date: Fri, 12 Mar 2021 03:04:37 +0100 Subject: [PATCH] initial bittorrent support --- haruhi_dl/HaruhiDL.py | 5 +++++ haruhi_dl/__init__.py | 2 ++ haruhi_dl/downloader/__init__.py | 2 ++ haruhi_dl/downloader/external.py | 7 ++++--- haruhi_dl/extractor/common.py | 14 +++++++++++++- haruhi_dl/options.py | 13 +++++++++++++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/haruhi_dl/HaruhiDL.py b/haruhi_dl/HaruhiDL.py index 9c0e2b125..6692fb89e 100755 --- a/haruhi_dl/HaruhiDL.py +++ b/haruhi_dl/HaruhiDL.py @@ -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: diff --git a/haruhi_dl/__init__.py b/haruhi_dl/__init__.py index 167881c65..e6e275bab 100644 --- a/haruhi_dl/__init__.py +++ b/haruhi_dl/__init__.py @@ -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, diff --git a/haruhi_dl/downloader/__init__.py b/haruhi_dl/downloader/__init__.py index 2e485df9d..1f7577c50 100644 --- a/haruhi_dl/downloader/__init__.py +++ b/haruhi_dl/downloader/__init__.py @@ -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, } diff --git a/haruhi_dl/downloader/external.py b/haruhi_dl/downloader/external.py index c31f8910a..b74e95bf1 100644 --- a/haruhi_dl/downloader/external.py +++ b/haruhi_dl/downloader/external.py @@ -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', '=') diff --git a/haruhi_dl/extractor/common.py b/haruhi_dl/extractor/common.py index 204a132c7..5b4ad411c 100644 --- a/haruhi_dl/extractor/common.py +++ b/haruhi_dl/extractor/common.py @@ -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 diff --git a/haruhi_dl/options.py b/haruhi_dl/options.py index 1517748b4..72b1deeef 100644 --- a/haruhi_dl/options.py +++ b/haruhi_dl/options.py @@ -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(