]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/external.py
1d5cc99043d02f658064e688c268c37171c37325
[youtubedl] / youtube_dl / downloader / external.py
1 from __future__ import unicode_literals
2
3 import os.path
4 import subprocess
5
6 from .common import FileDownloader
7 from ..utils import (
8 encodeFilename,
9 encodeArgument,
10 )
11
12
13 class ExternalFD(FileDownloader):
14 def real_download(self, filename, info_dict):
15 self.report_destination(filename)
16 tmpfilename = self.temp_name(filename)
17
18 retval = self._call_downloader(tmpfilename, info_dict)
19 if retval == 0:
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)
23 self._hook_progress({
24 'downloaded_bytes': fsize,
25 'total_bytes': fsize,
26 'filename': filename,
27 'status': 'finished',
28 })
29 return True
30 else:
31 self.to_stderr('\n')
32 self.report_error('%s exited with code %d' % (
33 self.get_basename(), retval))
34 return False
35
36 @classmethod
37 def get_basename(cls):
38 return cls.__name__[:-2].lower()
39
40 @property
41 def exe(self):
42 return self.params.get('external_downloader')
43
44 @classmethod
45 def supports(cls, info_dict):
46 return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
47
48 def _source_address(self, command_option):
49 source_address = self.params.get('source_address')
50 if source_address is None:
51 return []
52 return [command_option, source_address]
53
54 def _configuration_args(self, default=[]):
55 ex_args = self.params.get('external_downloader_args')
56 if ex_args is None:
57 return default
58 assert isinstance(ex_args, list)
59 return ex_args
60
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)]
64
65 self._debug_cmd(cmd)
66
67 p = subprocess.Popen(
68 cmd, stderr=subprocess.PIPE)
69 _, stderr = p.communicate()
70 if p.returncode != 0:
71 self.to_stderr(stderr)
72 return p.returncode
73
74
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']]
83 return cmd
84
85
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']]
94 return cmd
95
96
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)
103 if dn:
104 cmd += ['--dir', dn]
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']]
110 return cmd
111
112
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)]
118 return cmd
119
120 _BY_NAME = dict(
121 (klass.get_basename(), klass)
122 for name, klass in globals().items()
123 if name.endswith('FD') and name != 'ExternalFD'
124 )
125
126
127 def list_external_downloaders():
128 return sorted(_BY_NAME.keys())
129
130
131 def get_external_downloader(external_downloader):
132 """ Given the name of the executable, see whether we support the given
133 downloader . """
134 # Drop .exe extension on Windows
135 bn = os.path.splitext(os.path.basename(external_downloader))[0]
136 return _BY_NAME[bn]