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 WgetFD(ExternalFD):
87 def _make_cmd(self, tmpfilename, info_dict):
88 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
89 for key, val in info_dict['http_headers'].items():
90 cmd += ['--header', '%s: %s' % (key, val)]
91 cmd += self._source_address('--bind-address')
92 cmd += self._configuration_args()
93 cmd += ['--', info_dict['url']]
97 class Aria2cFD(ExternalFD):
98 def _make_cmd(self, tmpfilename, info_dict):
99 cmd = [self.exe, '-c']
100 cmd += self._configuration_args([
101 '--min-split-size', '1M', '--max-connection-per-server', '4'])
102 dn = os.path.dirname(tmpfilename)
105 cmd += ['--out', os.path.basename(tmpfilename)]
106 for key, val in info_dict['http_headers'].items():
107 cmd += ['--header', '%s: %s' % (key, val)]
108 cmd += self._source_address('--interface')
109 cmd += ['--', info_dict['url']]
113 class HttpieFD(ExternalFD):
114 def _make_cmd(self, tmpfilename, info_dict):
115 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
116 for key, val in info_dict['http_headers'].items():
117 cmd += ['%s:%s' % (key, val)]
121 (klass.get_basename(), klass)
122 for name, klass in globals().items()
123 if name.endswith('FD') and name != 'ExternalFD'
127 def list_external_downloaders():
128 return sorted(_BY_NAME.keys())
131 def get_external_downloader(external_downloader):
132 """ Given the name of the executable, see whether we support the given
134 # Drop .exe extension on Windows
135 bn = os.path.splitext(os.path.basename(external_downloader))[0]