[downloader/external] change _argless_option function to _valueless_option
[youtube-dl] / 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 _option(self, command_option, param):
49         param = self.params.get(param)
50         if param is None:
51             return []
52         return [command_option, param]
53
54     def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
55         param = self.params.get(param)
56         if not isinstance(param, bool):
57             return []
58         if separator:
59             return [command_option + separator + (true_value if param else false_value)]
60         return [command_option, true_value if param else false_value]
61
62     def _valueless_option(self, command_option, param, expected_value=True):
63         param = self.params.get(param)
64         return [command_option] if param == expected_value else []
65
66     def _configuration_args(self, default=[]):
67         ex_args = self.params.get('external_downloader_args')
68         if ex_args is None:
69             return default
70         assert isinstance(ex_args, list)
71         return ex_args
72
73     def _call_downloader(self, tmpfilename, info_dict):
74         """ Either overwrite this or implement _make_cmd """
75         cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
76
77         self._debug_cmd(cmd)
78
79         p = subprocess.Popen(
80             cmd, stderr=subprocess.PIPE)
81         _, stderr = p.communicate()
82         if p.returncode != 0:
83             self.to_stderr(stderr)
84         return p.returncode
85
86
87 class CurlFD(ExternalFD):
88     def _make_cmd(self, tmpfilename, info_dict):
89         cmd = [self.exe, '--location', '-o', tmpfilename]
90         for key, val in info_dict['http_headers'].items():
91             cmd += ['--header', '%s: %s' % (key, val)]
92         cmd += self._option('--interface', 'source_address')
93         cmd += self._option('--proxy', 'proxy')
94         cmd += self._valueless_option('--insecure', 'nocheckcertificate')
95         cmd += self._configuration_args()
96         cmd += ['--', info_dict['url']]
97         return cmd
98
99
100 class AxelFD(ExternalFD):
101     def _make_cmd(self, tmpfilename, info_dict):
102         cmd = [self.exe, '-o', tmpfilename]
103         for key, val in info_dict['http_headers'].items():
104             cmd += ['-H', '%s: %s' % (key, val)]
105         cmd += self._configuration_args()
106         cmd += ['--', info_dict['url']]
107         return cmd
108
109
110 class WgetFD(ExternalFD):
111     def _make_cmd(self, tmpfilename, info_dict):
112         cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
113         for key, val in info_dict['http_headers'].items():
114             cmd += ['--header', '%s: %s' % (key, val)]
115         cmd += self._option('--bind-address', 'source_address')
116         cmd += self._option('--proxy', 'proxy')
117         cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
118         cmd += self._configuration_args()
119         cmd += ['--', info_dict['url']]
120         return cmd
121
122
123 class Aria2cFD(ExternalFD):
124     def _make_cmd(self, tmpfilename, info_dict):
125         cmd = [self.exe, '-c']
126         cmd += self._configuration_args([
127             '--min-split-size', '1M', '--max-connection-per-server', '4'])
128         dn = os.path.dirname(tmpfilename)
129         if dn:
130             cmd += ['--dir', dn]
131         cmd += ['--out', os.path.basename(tmpfilename)]
132         for key, val in info_dict['http_headers'].items():
133             cmd += ['--header', '%s: %s' % (key, val)]
134         cmd += self._option('--interface', 'source_address')
135         cmd += self._option('--all-proxy', 'proxy')
136         cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
137         cmd += ['--', info_dict['url']]
138         return cmd
139
140
141 class HttpieFD(ExternalFD):
142     def _make_cmd(self, tmpfilename, info_dict):
143         cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
144         for key, val in info_dict['http_headers'].items():
145             cmd += ['%s:%s' % (key, val)]
146         return cmd
147
148 _BY_NAME = dict(
149     (klass.get_basename(), klass)
150     for name, klass in globals().items()
151     if name.endswith('FD') and name != 'ExternalFD'
152 )
153
154
155 def list_external_downloaders():
156     return sorted(_BY_NAME.keys())
157
158
159 def get_external_downloader(external_downloader):
160     """ Given the name of the executable, see whether we support the given
161         downloader . """
162     # Drop .exe extension on Windows
163     bn = os.path.splitext(os.path.basename(external_downloader))[0]
164     return _BY_NAME[bn]