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 _call_downloader(self, tmpfilename, info_dict):
49 """ Either overwrite this or implement _make_cmd """
50 cmd = self._make_cmd(tmpfilename, info_dict)
52 if sys.platform == 'win32' and sys.version_info < (3, 0):
53 # Windows subprocess module does not actually support Unicode
55 # See http://stackoverflow.com/a/9951851/35070
56 subprocess_encoding = sys.getfilesystemencoding()
57 cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
59 subprocess_encoding = None
60 self._debug_cmd(cmd, subprocess_encoding)
63 cmd, stderr=subprocess.PIPE)
64 _, stderr = p.communicate()
66 self.to_stderr(stderr)
70 class CurlFD(ExternalFD):
71 def _make_cmd(self, tmpfilename, info_dict):
72 cmd = [self.exe, '-o', tmpfilename]
73 for key, val in info_dict['http_headers'].items():
74 cmd += ['--header', '%s: %s' % (key, val)]
75 cmd += ['--', info_dict['url']]
79 class WgetFD(ExternalFD):
80 def _make_cmd(self, tmpfilename, info_dict):
81 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
82 for key, val in info_dict['http_headers'].items():
83 cmd += ['--header', '%s: %s' % (key, val)]
84 cmd += ['--', info_dict['url']]
88 class Aria2cFD(ExternalFD):
89 def _make_cmd(self, tmpfilename, info_dict):
92 '--min-split-size', '1M', '--max-connection-per-server', '4']
93 dn = os.path.dirname(tmpfilename)
96 cmd += ['--out', os.path.basename(tmpfilename)]
97 for key, val in info_dict['http_headers'].items():
98 cmd += ['--header', '%s: %s' % (key, val)]
99 cmd += ['--', info_dict['url']]
103 (klass.get_basename(), klass)
104 for name, klass in globals().items()
105 if name.endswith('FD') and name != 'ExternalFD'
109 def list_external_downloaders():
110 return sorted(_BY_NAME.keys())
113 def get_external_downloader(external_downloader):
114 """ Given the name of the executable, see whether we support the given
116 bn = os.path.basename(external_downloader)