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 _source_address(self, command_option):
49 source_address = self.params.get('source_address')
50 if source_address is None:
52 return [command_option, source_address]
54 def _no_check_certificate(self, command_option):
55 return [command_option] if self.params.get('nocheckcertificate', False) else []
57 def _configuration_args(self, default=[]):
58 ex_args = self.params.get('external_downloader_args')
61 assert isinstance(ex_args, list)
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._source_address('--interface')
84 cmd += self._configuration_args()
85 cmd += ['--', info_dict['url']]
89 class AxelFD(ExternalFD):
90 def _make_cmd(self, tmpfilename, info_dict):
91 cmd = [self.exe, '-o', tmpfilename]
92 for key, val in info_dict['http_headers'].items():
93 cmd += ['-H', '%s: %s' % (key, val)]
94 cmd += self._configuration_args()
95 cmd += ['--', info_dict['url']]
99 class WgetFD(ExternalFD):
100 def _make_cmd(self, tmpfilename, info_dict):
101 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
102 for key, val in info_dict['http_headers'].items():
103 cmd += ['--header', '%s: %s' % (key, val)]
104 cmd += self._source_address('--bind-address')
105 cmd += self._no_check_certificate('--no-check-certificate')
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._source_address('--interface')
123 cmd += ['--', info_dict['url']]
127 class HttpieFD(ExternalFD):
128 def _make_cmd(self, tmpfilename, info_dict):
129 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
130 for key, val in info_dict['http_headers'].items():
131 cmd += ['%s:%s' % (key, val)]
135 (klass.get_basename(), klass)
136 for name, klass in globals().items()
137 if name.endswith('FD') and name != 'ExternalFD'
141 def list_external_downloaders():
142 return sorted(_BY_NAME.keys())
145 def get_external_downloader(external_downloader):
146 """ Given the name of the executable, see whether we support the given
148 # Drop .exe extension on Windows
149 bn = os.path.splitext(os.path.basename(external_downloader))[0]