]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/downloader/external.py
[viddler] PEP8
[youtube-dl.git] / youtube_dl / downloader / external.py
1 from __future__ import unicode_literals
2
3 import os.path
4 import subprocess
5 import sys
6
7 from .common import FileDownloader
8 from ..utils import (
9     encodeFilename,
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 _call_downloader(self, tmpfilename, info_dict):
49         """ Either overwrite this or implement _make_cmd """
50         cmd = self._make_cmd(tmpfilename, info_dict)
51
52         if sys.platform == 'win32' and sys.version_info < (3, 0):
53             # Windows subprocess module does not actually support Unicode
54             # on Python 2.x
55             # See http://stackoverflow.com/a/9951851/35070
56             subprocess_encoding = sys.getfilesystemencoding()
57             cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
58         else:
59             subprocess_encoding = None
60         self._debug_cmd(cmd, subprocess_encoding)
61
62         p = subprocess.Popen(
63             cmd, stderr=subprocess.PIPE)
64         _, stderr = p.communicate()
65         if p.returncode != 0:
66             self.to_stderr(stderr)
67         return p.returncode
68
69
70 class CurlFD(ExternalFD):
71     def _make_cmd(self, tmpfilename, info_dict):
72         cmd = [self.exe, '-o', tmpfilename]
73         for key, val in info_dict['http_headers'].items():
74             cmd += ['--header', '%s: %s' % (key, val)]
75         cmd += ['--', info_dict['url']]
76         return cmd
77
78
79 class WgetFD(ExternalFD):
80     def _make_cmd(self, tmpfilename, info_dict):
81         cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
82         for key, val in info_dict['http_headers'].items():
83             cmd += ['--header', '%s: %s' % (key, val)]
84         cmd += ['--', info_dict['url']]
85         return cmd
86
87
88 class Aria2cFD(ExternalFD):
89     def _make_cmd(self, tmpfilename, info_dict):
90         cmd = [
91             self.exe, '-c',
92             '--min-split-size', '1M', '--max-connection-per-server', '4']
93         dn = os.path.dirname(tmpfilename)
94         if dn:
95             cmd += ['--dir', dn]
96         cmd += ['--out', os.path.basename(tmpfilename)]
97         for key, val in info_dict['http_headers'].items():
98             cmd += ['--header', '%s: %s' % (key, val)]
99         cmd += ['--', info_dict['url']]
100         return cmd
101
102 _BY_NAME = dict(
103     (klass.get_basename(), klass)
104     for name, klass in globals().items()
105     if name.endswith('FD') and name != 'ExternalFD'
106 )
107
108
109 def list_external_downloaders():
110     return sorted(_BY_NAME.keys())
111
112
113 def get_external_downloader(external_downloader):
114     """ Given the name of the executable, see whether we support the given
115         downloader . """
116     bn = os.path.basename(external_downloader)
117     return _BY_NAME[bn]