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)
52 if isinstance(param, bool):
53 return [command_option]
54 return [command_option, param]
56 def _configuration_args(self, default=[]):
57 ex_args = self.params.get('external_downloader_args')
60 assert isinstance(ex_args, list)
63 def _call_downloader(self, tmpfilename, info_dict):
64 """ Either overwrite this or implement _make_cmd """
65 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
70 cmd, stderr=subprocess.PIPE)
71 _, stderr = p.communicate()
73 self.to_stderr(stderr)
77 class CurlFD(ExternalFD):
78 def _make_cmd(self, tmpfilename, info_dict):
79 cmd = [self.exe, '--location', '-o', tmpfilename]
80 for key, val in info_dict['http_headers'].items():
81 cmd += ['--header', '%s: %s' % (key, val)]
82 cmd += self._option('--interface', 'source_address')
83 cmd += self._configuration_args()
84 cmd += ['--', info_dict['url']]
88 class AxelFD(ExternalFD):
89 def _make_cmd(self, tmpfilename, info_dict):
90 cmd = [self.exe, '-o', tmpfilename]
91 for key, val in info_dict['http_headers'].items():
92 cmd += ['-H', '%s: %s' % (key, val)]
93 cmd += self._configuration_args()
94 cmd += ['--', info_dict['url']]
98 class WgetFD(ExternalFD):
99 def _make_cmd(self, tmpfilename, info_dict):
100 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
101 for key, val in info_dict['http_headers'].items():
102 cmd += ['--header', '%s: %s' % (key, val)]
103 cmd += self._option('--bind-address', 'source_address')
104 cmd += self._option('--proxy', 'proxy')
105 cmd += self._option('--no-check-certificate', 'nocheckcertificate')
106 cmd += self._configuration_args()
107 cmd += ['--', info_dict['url']]
111 class Aria2cFD(ExternalFD):
112 def _make_cmd(self, tmpfilename, info_dict):
113 cmd = [self.exe, '-c']
114 cmd += self._configuration_args([
115 '--min-split-size', '1M', '--max-connection-per-server', '4'])
116 dn = os.path.dirname(tmpfilename)
119 cmd += ['--out', os.path.basename(tmpfilename)]
120 for key, val in info_dict['http_headers'].items():
121 cmd += ['--header', '%s: %s' % (key, val)]
122 cmd += self._option('--interface', 'source_address')
123 cmd += self._option('--all-proxy', 'proxy')
124 cmd += ['--', info_dict['url']]
128 class HttpieFD(ExternalFD):
129 def _make_cmd(self, tmpfilename, info_dict):
130 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
131 for key, val in info_dict['http_headers'].items():
132 cmd += ['%s:%s' % (key, val)]
136 (klass.get_basename(), klass)
137 for name, klass in globals().items()
138 if name.endswith('FD') and name != 'ExternalFD'
142 def list_external_downloaders():
143 return sorted(_BY_NAME.keys())
146 def get_external_downloader(external_downloader):
147 """ Given the name of the executable, see whether we support the given
149 # Drop .exe extension on Windows
150 bn = os.path.splitext(os.path.basename(external_downloader))[0]