1 from __future__ import unicode_literals
6 from .common import FileDownloader
13 class ExternalFD(FileDownloader):
14 def real_download(self, filename, info_dict):
15 self.report_destination(filename)
16 tmpfilename = self.temp_name(filename)
18 retval = self._call_downloader(tmpfilename, info_dict)
20 fsize = os.path.getsize(encodeFilename(tmpfilename))
21 self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
22 self.try_rename(tmpfilename, filename)
24 'downloaded_bytes': fsize,
32 self.report_error('%s exited with code %d' % (
33 self.get_basename(), retval))
37 def get_basename(cls):
38 return cls.__name__[:-2].lower()
42 return self.params.get('external_downloader')
45 def supports(cls, info_dict):
46 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
48 def _option(self, command_option, param):
49 param = self.params.get(param)
50 if param is None or param is False:
52 return [command_option, param]
54 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
55 param = self.params.get(param)
56 if not isinstance(param, bool):
59 return [command_option + separator + (true_value if param else false_value)]
60 return [command_option, true_value if param else false_value]
62 def _argless_option(self, command_option, param, expected_value=True):
63 param = self.params.get(param)
64 return [command_option] if param == expected_value else []
66 def _configuration_args(self, default=[]):
67 ex_args = self.params.get('external_downloader_args')
70 assert isinstance(ex_args, list)
73 def _call_downloader(self, tmpfilename, info_dict):
74 """ Either overwrite this or implement _make_cmd """
75 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
80 cmd, stderr=subprocess.PIPE)
81 _, stderr = p.communicate()
83 self.to_stderr(stderr)
87 class CurlFD(ExternalFD):
88 def _make_cmd(self, tmpfilename, info_dict):
89 cmd = [self.exe, '--location', '-o', tmpfilename]
90 for key, val in info_dict['http_headers'].items():
91 cmd += ['--header', '%s: %s' % (key, val)]
92 cmd += self._option('--interface', 'source_address')
93 cmd += self._option('--proxy', 'proxy')
94 cmd += self._argless_option('--insecure', 'nocheckcertificate')
95 cmd += self._configuration_args()
96 cmd += ['--', info_dict['url']]
100 class AxelFD(ExternalFD):
101 def _make_cmd(self, tmpfilename, info_dict):
102 cmd = [self.exe, '-o', tmpfilename]
103 for key, val in info_dict['http_headers'].items():
104 cmd += ['-H', '%s: %s' % (key, val)]
105 cmd += self._configuration_args()
106 cmd += ['--', info_dict['url']]
110 class WgetFD(ExternalFD):
111 def _make_cmd(self, tmpfilename, info_dict):
112 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
113 for key, val in info_dict['http_headers'].items():
114 cmd += ['--header', '%s: %s' % (key, val)]
115 cmd += self._option('--bind-address', 'source_address')
116 cmd += self._option('--proxy', 'proxy')
117 cmd += self._argless_option('--no-check-certificate', 'nocheckcertificate')
118 cmd += self._configuration_args()
119 cmd += ['--', info_dict['url']]
123 class Aria2cFD(ExternalFD):
124 def _make_cmd(self, tmpfilename, info_dict):
125 cmd = [self.exe, '-c']
126 cmd += self._configuration_args([
127 '--min-split-size', '1M', '--max-connection-per-server', '4'])
128 dn = os.path.dirname(tmpfilename)
131 cmd += ['--out', os.path.basename(tmpfilename)]
132 for key, val in info_dict['http_headers'].items():
133 cmd += ['--header', '%s: %s' % (key, val)]
134 cmd += self._option('--interface', 'source_address')
135 cmd += self._option('--all-proxy', 'proxy')
136 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
137 cmd += ['--', info_dict['url']]
141 class HttpieFD(ExternalFD):
142 def _make_cmd(self, tmpfilename, info_dict):
143 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
144 for key, val in info_dict['http_headers'].items():
145 cmd += ['%s:%s' % (key, val)]
149 (klass.get_basename(), klass)
150 for name, klass in globals().items()
151 if name.endswith('FD') and name != 'ExternalFD'
155 def list_external_downloaders():
156 return sorted(_BY_NAME.keys())
159 def get_external_downloader(external_downloader):
160 """ Given the name of the executable, see whether we support the given
162 # Drop .exe extension on Windows
163 bn = os.path.splitext(os.path.basename(external_downloader))[0]