1 from __future__ import unicode_literals
7 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 _call_downloader(self, tmpfilename, info_dict):
55 """ Either overwrite this or implement _make_cmd """
56 cmd = self._make_cmd(tmpfilename, info_dict)
58 if sys.platform == 'win32' and sys.version_info < (3, 0):
59 # Windows subprocess module does not actually support Unicode
61 # See http://stackoverflow.com/a/9951851/35070
62 subprocess_encoding = sys.getfilesystemencoding()
63 cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
65 subprocess_encoding = None
66 self._debug_cmd(cmd, subprocess_encoding)
69 cmd, stderr=subprocess.PIPE)
70 _, stderr = p.communicate()
72 self.to_stderr(stderr)
76 class CurlFD(ExternalFD):
77 def _make_cmd(self, tmpfilename, info_dict):
78 cmd = [self.exe, '--location', '-o', tmpfilename]
79 for key, val in info_dict['http_headers'].items():
80 cmd += ['--header', '%s: %s' % (key, val)]
81 cmd += self._source_address('--interface')
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 += ['--', info_dict['url']]
96 class Aria2cFD(ExternalFD):
97 def _make_cmd(self, tmpfilename, info_dict):
100 '--min-split-size', '1M', '--max-connection-per-server', '4']
101 dn = os.path.dirname(tmpfilename)
104 cmd += ['--out', os.path.basename(tmpfilename)]
105 for key, val in info_dict['http_headers'].items():
106 cmd += ['--header', '%s: %s' % (key, val)]
107 cmd += self._source_address('--interface')
108 cmd += ['--', info_dict['url']]
112 (klass.get_basename(), klass)
113 for name, klass in globals().items()
114 if name.endswith('FD') and name != 'ExternalFD'
118 def list_external_downloaders():
119 return sorted(_BY_NAME.keys())
122 def get_external_downloader(external_downloader):
123 """ Given the name of the executable, see whether we support the given
125 bn = os.path.basename(external_downloader)