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 _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._option('--proxy', 'proxy')
84 cmd += self._option('--insecure', 'nocheckcertificate')
85 cmd += self._configuration_args()
86 cmd += ['--', info_dict['url']]
90 class AxelFD(ExternalFD):
91 def _make_cmd(self, tmpfilename, info_dict):
92 cmd = [self.exe, '-o', tmpfilename]
93 for key, val in info_dict['http_headers'].items():
94 cmd += ['-H', '%s: %s' % (key, val)]
95 cmd += self._configuration_args()
96 cmd += ['--', info_dict['url']]
100 class WgetFD(ExternalFD):
101 def _make_cmd(self, tmpfilename, info_dict):
102 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
103 for key, val in info_dict['http_headers'].items():
104 cmd += ['--header', '%s: %s' % (key, val)]
105 cmd += self._option('--bind-address', 'source_address')
106 cmd += self._option('--proxy', 'proxy')
107 cmd += self._option('--no-check-certificate', 'nocheckcertificate')
108 cmd += self._configuration_args()
109 cmd += ['--', info_dict['url']]
113 class Aria2cFD(ExternalFD):
114 def _make_cmd(self, tmpfilename, info_dict):
115 cmd = [self.exe, '-c']
116 cmd += self._configuration_args([
117 '--min-split-size', '1M', '--max-connection-per-server', '4'])
118 dn = os.path.dirname(tmpfilename)
121 cmd += ['--out', os.path.basename(tmpfilename)]
122 for key, val in info_dict['http_headers'].items():
123 cmd += ['--header', '%s: %s' % (key, val)]
124 cmd += self._option('--interface', 'source_address')
125 cmd += self._option('--all-proxy', 'proxy')
126 cmd += self._option('--check-certificate=false', 'nocheckcertificate')
127 cmd += ['--', info_dict['url']]
131 class HttpieFD(ExternalFD):
132 def _make_cmd(self, tmpfilename, info_dict):
133 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
134 for key, val in info_dict['http_headers'].items():
135 cmd += ['%s:%s' % (key, val)]
139 (klass.get_basename(), klass)
140 for name, klass in globals().items()
141 if name.endswith('FD') and name != 'ExternalFD'
145 def list_external_downloaders():
146 return sorted(_BY_NAME.keys())
149 def get_external_downloader(external_downloader):
150 """ Given the name of the executable, see whether we support the given
152 # Drop .exe extension on Windows
153 bn = os.path.splitext(os.path.basename(external_downloader))[0]