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