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):
50 source_address = self.ydl.params.get('source_address')
52 command_part = [command_option, source_address]
55 def _call_downloader(self, tmpfilename, info_dict):
56 """ Either overwrite this or implement _make_cmd """
57 cmd = self._make_cmd(tmpfilename, info_dict)
59 if sys.platform == 'win32' and sys.version_info < (3, 0):
60 # Windows subprocess module does not actually support Unicode
62 # See http://stackoverflow.com/a/9951851/35070
63 subprocess_encoding = sys.getfilesystemencoding()
64 cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
66 subprocess_encoding = None
67 self._debug_cmd(cmd, subprocess_encoding)
70 cmd, stderr=subprocess.PIPE)
71 _, stderr = p.communicate()
73 self.to_stderr(stderr)
77 class CurlFD(ExternalFD):
78 def _make_cmd(self, tmpfilename, info_dict):
79 cmd = [self.exe, '-o', tmpfilename]
80 for key, val in info_dict['http_headers'].items():
81 cmd += ['--header', '%s: %s' % (key, val)]
82 cmd += self._source_address('--interface')
83 cmd += ['--', info_dict['url']]
87 class WgetFD(ExternalFD):
88 def _make_cmd(self, tmpfilename, info_dict):
89 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
90 for key, val in info_dict['http_headers'].items():
91 cmd += ['--header', '%s: %s' % (key, val)]
92 cmd += self._source_address('--bind-address')
93 cmd += ['--', info_dict['url']]
97 class Aria2cFD(ExternalFD):
98 def _make_cmd(self, tmpfilename, info_dict):
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 (klass.get_basename(), klass)
114 for name, klass in globals().items()
115 if name.endswith('FD') and name != 'ExternalFD'
119 def list_external_downloaders():
120 return sorted(_BY_NAME.keys())
123 def get_external_downloader(external_downloader):
124 """ Given the name of the executable, see whether we support the given
126 bn = os.path.basename(external_downloader)