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 if isinstance(param, bool):
53 return [command_option]
54 return [command_option, param]
56 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
57 param = self.params.get(param)
58 if not isinstance(param, bool):
61 return [command_option + separator + (true_value if param else false_value)]
62 return [command_option, true_value if param else false_value]
64 def _configuration_args(self, default=[]):
65 ex_args = self.params.get('external_downloader_args')
68 assert isinstance(ex_args, list)
71 def _call_downloader(self, tmpfilename, info_dict):
72 """ Either overwrite this or implement _make_cmd """
73 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
78 cmd, stderr=subprocess.PIPE)
79 _, stderr = p.communicate()
81 self.to_stderr(stderr)
85 class CurlFD(ExternalFD):
86 def _make_cmd(self, tmpfilename, info_dict):
87 cmd = [self.exe, '--location', '-o', tmpfilename]
88 for key, val in info_dict['http_headers'].items():
89 cmd += ['--header', '%s: %s' % (key, val)]
90 cmd += self._option('--interface', 'source_address')
91 cmd += self._option('--proxy', 'proxy')
92 cmd += self._option('--insecure', 'nocheckcertificate')
93 cmd += self._configuration_args()
94 cmd += ['--', info_dict['url']]
98 class AxelFD(ExternalFD):
99 def _make_cmd(self, tmpfilename, info_dict):
100 cmd = [self.exe, '-o', tmpfilename]
101 for key, val in info_dict['http_headers'].items():
102 cmd += ['-H', '%s: %s' % (key, val)]
103 cmd += self._configuration_args()
104 cmd += ['--', info_dict['url']]
108 class WgetFD(ExternalFD):
109 def _make_cmd(self, tmpfilename, info_dict):
110 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
111 for key, val in info_dict['http_headers'].items():
112 cmd += ['--header', '%s: %s' % (key, val)]
113 cmd += self._option('--bind-address', 'source_address')
114 cmd += self._option('--proxy', 'proxy')
115 cmd += self._option('--no-check-certificate', 'nocheckcertificate')
116 cmd += self._configuration_args()
117 cmd += ['--', info_dict['url']]
121 class Aria2cFD(ExternalFD):
122 def _make_cmd(self, tmpfilename, info_dict):
123 cmd = [self.exe, '-c']
124 cmd += self._configuration_args([
125 '--min-split-size', '1M', '--max-connection-per-server', '4'])
126 dn = os.path.dirname(tmpfilename)
129 cmd += ['--out', os.path.basename(tmpfilename)]
130 for key, val in info_dict['http_headers'].items():
131 cmd += ['--header', '%s: %s' % (key, val)]
132 cmd += self._option('--interface', 'source_address')
133 cmd += self._option('--all-proxy', 'proxy')
134 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
135 cmd += ['--', info_dict['url']]
139 class HttpieFD(ExternalFD):
140 def _make_cmd(self, tmpfilename, info_dict):
141 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
142 for key, val in info_dict['http_headers'].items():
143 cmd += ['%s:%s' % (key, val)]
147 (klass.get_basename(), klass)
148 for name, klass in globals().items()
149 if name.endswith('FD') and name != 'ExternalFD'
153 def list_external_downloaders():
154 return sorted(_BY_NAME.keys())
157 def get_external_downloader(external_downloader):
158 """ Given the name of the executable, see whether we support the given
160 # Drop .exe extension on Windows
161 bn = os.path.splitext(os.path.basename(external_downloader))[0]