Merge branch 'shahid' of https://github.com/remitamine/youtube-dl into remitamine...
[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         if isinstance(param, bool):
53             return [command_option]
54         return [command_option, param]
55
56     def _configuration_args(self, default=[]):
57         ex_args = self.params.get('external_downloader_args')
58         if ex_args is None:
59             return default
60         assert isinstance(ex_args, list)
61         return ex_args
62
63     def _call_downloader(self, tmpfilename, info_dict):
64         """ Either overwrite this or implement _make_cmd """
65         cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
66
67         self._debug_cmd(cmd)
68
69         p = subprocess.Popen(
70             cmd, stderr=subprocess.PIPE)
71         _, stderr = p.communicate()
72         if p.returncode != 0:
73             self.to_stderr(stderr)
74         return p.returncode
75
76
77 class CurlFD(ExternalFD):
78     def _make_cmd(self, tmpfilename, info_dict):
79         cmd = [self.exe, '--location', '-o', tmpfilename]
80         for key, val in info_dict['http_headers'].items():
81             cmd += ['--header', '%s: %s' % (key, val)]
82         cmd += self._option('--interface', 'source_address')
83         cmd += self._configuration_args()
84         cmd += ['--', info_dict['url']]
85         return cmd
86
87
88 class AxelFD(ExternalFD):
89     def _make_cmd(self, tmpfilename, info_dict):
90         cmd = [self.exe, '-o', tmpfilename]
91         for key, val in info_dict['http_headers'].items():
92             cmd += ['-H', '%s: %s' % (key, val)]
93         cmd += self._configuration_args()
94         cmd += ['--', info_dict['url']]
95         return cmd
96
97
98 class WgetFD(ExternalFD):
99     def _make_cmd(self, tmpfilename, info_dict):
100         cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
101         for key, val in info_dict['http_headers'].items():
102             cmd += ['--header', '%s: %s' % (key, val)]
103         cmd += self._option('--bind-address', 'source_address')
104         cmd += self._option('--proxy', 'proxy')
105         cmd += self._option('--no-check-certificate', 'nocheckcertificate')
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._option('--interface', 'source_address')
123         cmd += self._option('--all-proxy', 'proxy')
124         cmd += ['--', info_dict['url']]
125         return cmd
126
127
128 class HttpieFD(ExternalFD):
129     def _make_cmd(self, tmpfilename, info_dict):
130         cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
131         for key, val in info_dict['http_headers'].items():
132             cmd += ['%s:%s' % (key, val)]
133         return cmd
134
135 _BY_NAME = dict(
136     (klass.get_basename(), klass)
137     for name, klass in globals().items()
138     if name.endswith('FD') and name != 'ExternalFD'
139 )
140
141
142 def list_external_downloaders():
143     return sorted(_BY_NAME.keys())
144
145
146 def get_external_downloader(external_downloader):
147     """ Given the name of the executable, see whether we support the given
148         downloader . """
149     # Drop .exe extension on Windows
150     bn = os.path.splitext(os.path.basename(external_downloader))[0]
151     return _BY_NAME[bn]