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