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