1 from __future__ import unicode_literals
6 from .common import FileDownloader
11 cli_configuration_args,
17 class ExternalFD(FileDownloader):
18 def real_download(self, filename, info_dict):
19 self.report_destination(filename)
20 tmpfilename = self.temp_name(filename)
22 retval = self._call_downloader(tmpfilename, info_dict)
24 fsize = os.path.getsize(encodeFilename(tmpfilename))
25 self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
26 self.try_rename(tmpfilename, filename)
28 'downloaded_bytes': fsize,
36 self.report_error('%s exited with code %d' % (
37 self.get_basename(), retval))
41 def get_basename(cls):
42 return cls.__name__[:-2].lower()
46 return self.params.get('external_downloader')
49 def supports(cls, info_dict):
50 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
52 def _option(self, command_option, param):
53 return cli_option(self.params, command_option, param)
55 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
56 return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
58 def _valueless_option(self, command_option, param, expected_value=True):
59 return cli_valueless_option(self.params, command_option, param, expected_value)
61 def _configuration_args(self, default=[]):
62 return cli_configuration_args(self.params, 'external_downloader_args', default)
64 def _call_downloader(self, tmpfilename, info_dict):
65 """ Either overwrite this or implement _make_cmd """
66 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
71 cmd, stderr=subprocess.PIPE)
72 _, stderr = p.communicate()
74 self.to_stderr(stderr)
78 class CurlFD(ExternalFD):
79 def _make_cmd(self, tmpfilename, info_dict):
80 cmd = [self.exe, '--location', '-o', tmpfilename]
81 for key, val in info_dict['http_headers'].items():
82 cmd += ['--header', '%s: %s' % (key, val)]
83 cmd += self._option('--interface', 'source_address')
84 cmd += self._option('--proxy', 'proxy')
85 cmd += self._valueless_option('--insecure', 'nocheckcertificate')
86 cmd += self._configuration_args()
87 cmd += ['--', info_dict['url']]
91 class AxelFD(ExternalFD):
92 def _make_cmd(self, tmpfilename, info_dict):
93 cmd = [self.exe, '-o', tmpfilename]
94 for key, val in info_dict['http_headers'].items():
95 cmd += ['-H', '%s: %s' % (key, val)]
96 cmd += self._configuration_args()
97 cmd += ['--', info_dict['url']]
101 class WgetFD(ExternalFD):
102 def _make_cmd(self, tmpfilename, info_dict):
103 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
104 for key, val in info_dict['http_headers'].items():
105 cmd += ['--header', '%s: %s' % (key, val)]
106 cmd += self._option('--bind-address', 'source_address')
107 cmd += self._option('--proxy', 'proxy')
108 cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
109 cmd += self._configuration_args()
110 cmd += ['--', info_dict['url']]
114 class Aria2cFD(ExternalFD):
115 def _make_cmd(self, tmpfilename, info_dict):
116 cmd = [self.exe, '-c']
117 cmd += self._configuration_args([
118 '--min-split-size', '1M', '--max-connection-per-server', '4'])
119 dn = os.path.dirname(tmpfilename)
122 cmd += ['--out', os.path.basename(tmpfilename)]
123 for key, val in info_dict['http_headers'].items():
124 cmd += ['--header', '%s: %s' % (key, val)]
125 cmd += self._option('--interface', 'source_address')
126 cmd += self._option('--all-proxy', 'proxy')
127 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
128 cmd += ['--', info_dict['url']]
132 class HttpieFD(ExternalFD):
133 def _make_cmd(self, tmpfilename, info_dict):
134 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
135 for key, val in info_dict['http_headers'].items():
136 cmd += ['%s:%s' % (key, val)]
140 (klass.get_basename(), klass)
141 for name, klass in globals().items()
142 if name.endswith('FD') and name != 'ExternalFD'
146 def list_external_downloaders():
147 return sorted(_BY_NAME.keys())
150 def get_external_downloader(external_downloader):
151 """ Given the name of the executable, see whether we support the given
153 # Drop .exe extension on Windows
154 bn = os.path.splitext(os.path.basename(external_downloader))[0]