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 _configuration_args(self, default=[]):
55 ex_args = self.params.get('external_downloader_args')
58 assert isinstance(ex_args, list)
61 def _call_downloader(self, tmpfilename, info_dict):
62 """ Either overwrite this or implement _make_cmd """
63 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
68 cmd, stderr=subprocess.PIPE)
69 _, stderr = p.communicate()
71 self.to_stderr(stderr)
75 class CurlFD(ExternalFD):
76 def _make_cmd(self, tmpfilename, info_dict):
77 cmd = [self.exe, '--location', '-o', tmpfilename]
78 for key, val in info_dict['http_headers'].items():
79 cmd += ['--header', '%s: %s' % (key, val)]
80 cmd += self._source_address('--interface')
81 cmd += self._configuration_args()
82 cmd += ['--', info_dict['url']]
86 class AxelFD(ExternalFD):
87 def _make_cmd(self, tmpfilename, info_dict):
88 cmd = [self.exe, '-o', tmpfilename]
89 for key, val in info_dict['http_headers'].items():
90 cmd += ['-H', '%s: %s' % (key, val)]
91 cmd += self._configuration_args()
92 cmd += ['--', info_dict['url']]
96 class WgetFD(ExternalFD):
97 def _make_cmd(self, tmpfilename, info_dict):
98 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
99 for key, val in info_dict['http_headers'].items():
100 cmd += ['--header', '%s: %s' % (key, val)]
101 cmd += self._source_address('--bind-address')
102 cmd += self._configuration_args()
103 cmd += ['--', info_dict['url']]
107 class Aria2cFD(ExternalFD):
108 def _make_cmd(self, tmpfilename, info_dict):
109 cmd = [self.exe, '-c']
110 cmd += self._configuration_args([
111 '--min-split-size', '1M', '--max-connection-per-server', '4'])
112 dn = os.path.dirname(tmpfilename)
115 cmd += ['--out', os.path.basename(tmpfilename)]
116 for key, val in info_dict['http_headers'].items():
117 cmd += ['--header', '%s: %s' % (key, val)]
118 cmd += self._source_address('--interface')
119 cmd += ['--', info_dict['url']]
123 class HttpieFD(ExternalFD):
124 def _make_cmd(self, tmpfilename, info_dict):
125 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
126 for key, val in info_dict['http_headers'].items():
127 cmd += ['%s:%s' % (key, val)]
131 (klass.get_basename(), klass)
132 for name, klass in globals().items()
133 if name.endswith('FD') and name != 'ExternalFD'
137 def list_external_downloaders():
138 return sorted(_BY_NAME.keys())
141 def get_external_downloader(external_downloader):
142 """ Given the name of the executable, see whether we support the given
144 # Drop .exe extension on Windows
145 bn = os.path.splitext(os.path.basename(external_downloader))[0]