external.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from __future__ import unicode_literals
  2. import os.path
  3. import subprocess
  4. import sys
  5. from .common import FileDownloader
  6. from ..utils import (
  7. encodeFilename,
  8. )
  9. class ExternalFD(FileDownloader):
  10. def real_download(self, filename, info_dict):
  11. self.report_destination(filename)
  12. tmpfilename = self.temp_name(filename)
  13. retval = self._call_downloader(tmpfilename, info_dict)
  14. if retval == 0:
  15. fsize = os.path.getsize(encodeFilename(tmpfilename))
  16. self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
  17. self.try_rename(tmpfilename, filename)
  18. self._hook_progress({
  19. 'downloaded_bytes': fsize,
  20. 'total_bytes': fsize,
  21. 'filename': filename,
  22. 'status': 'finished',
  23. })
  24. return True
  25. else:
  26. self.to_stderr('\n')
  27. self.report_error('%s exited with code %d' % (
  28. self.get_basename(), retval))
  29. return False
  30. @classmethod
  31. def get_basename(cls):
  32. return cls.__name__[:-2].lower()
  33. @property
  34. def exe(self):
  35. return self.params.get('external_downloader')
  36. @classmethod
  37. def supports(cls, info_dict):
  38. return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
  39. def _source_address(self, command_option):
  40. command_part = []
  41. source_address = self.ydl.params.get('source_address')
  42. if source_address is None:
  43. command_part = [command_option, source_address]
  44. return command_part
  45. def _call_downloader(self, tmpfilename, info_dict):
  46. """ Either overwrite this or implement _make_cmd """
  47. cmd = self._make_cmd(tmpfilename, info_dict)
  48. if sys.platform == 'win32' and sys.version_info < (3, 0):
  49. # Windows subprocess module does not actually support Unicode
  50. # on Python 2.x
  51. # See http://stackoverflow.com/a/9951851/35070
  52. subprocess_encoding = sys.getfilesystemencoding()
  53. cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
  54. else:
  55. subprocess_encoding = None
  56. self._debug_cmd(cmd, subprocess_encoding)
  57. p = subprocess.Popen(
  58. cmd, stderr=subprocess.PIPE)
  59. _, stderr = p.communicate()
  60. if p.returncode != 0:
  61. self.to_stderr(stderr)
  62. return p.returncode
  63. class CurlFD(ExternalFD):
  64. def _make_cmd(self, tmpfilename, info_dict):
  65. cmd = [self.exe, '-o', tmpfilename]
  66. for key, val in info_dict['http_headers'].items():
  67. cmd += ['--header', '%s: %s' % (key, val)]
  68. cmd += self._source_address('--interface')
  69. cmd += ['--', info_dict['url']]
  70. return cmd
  71. class WgetFD(ExternalFD):
  72. def _make_cmd(self, tmpfilename, info_dict):
  73. cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
  74. for key, val in info_dict['http_headers'].items():
  75. cmd += ['--header', '%s: %s' % (key, val)]
  76. cmd += self._source_address('--bind-address')
  77. cmd += ['--', info_dict['url']]
  78. return cmd
  79. class Aria2cFD(ExternalFD):
  80. def _make_cmd(self, tmpfilename, info_dict):
  81. cmd = [
  82. self.exe, '-c',
  83. '--min-split-size', '1M', '--max-connection-per-server', '4']
  84. dn = os.path.dirname(tmpfilename)
  85. if dn:
  86. cmd += ['--dir', dn]
  87. cmd += ['--out', os.path.basename(tmpfilename)]
  88. for key, val in info_dict['http_headers'].items():
  89. cmd += ['--header', '%s: %s' % (key, val)]
  90. cmd += self._source_address('--interface')
  91. cmd += ['--', info_dict['url']]
  92. return cmd
  93. _BY_NAME = dict(
  94. (klass.get_basename(), klass)
  95. for name, klass in globals().items()
  96. if name.endswith('FD') and name != 'ExternalFD'
  97. )
  98. def list_external_downloaders():
  99. return sorted(_BY_NAME.keys())
  100. def get_external_downloader(external_downloader):
  101. """ Given the name of the executable, see whether we support the given
  102. downloader . """
  103. bn = os.path.basename(external_downloader)
  104. return _BY_NAME[bn]