You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.3 KiB

  1. from __future__ import unicode_literals
  2. import os.path
  3. import subprocess
  4. from .common import FileDownloader
  5. from ..utils import (
  6. cli_option,
  7. cli_valueless_option,
  8. cli_bool_option,
  9. cli_configuration_args,
  10. encodeFilename,
  11. encodeArgument,
  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. retval = self._call_downloader(tmpfilename, info_dict)
  18. if retval == 0:
  19. fsize = os.path.getsize(encodeFilename(tmpfilename))
  20. self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize))
  21. self.try_rename(tmpfilename, filename)
  22. self._hook_progress({
  23. 'downloaded_bytes': fsize,
  24. 'total_bytes': fsize,
  25. 'filename': filename,
  26. 'status': 'finished',
  27. })
  28. return True
  29. else:
  30. self.to_stderr('\n')
  31. self.report_error('%s exited with code %d' % (
  32. self.get_basename(), retval))
  33. return False
  34. @classmethod
  35. def get_basename(cls):
  36. return cls.__name__[:-2].lower()
  37. @property
  38. def exe(self):
  39. return self.params.get('external_downloader')
  40. @classmethod
  41. def supports(cls, info_dict):
  42. return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps')
  43. def _option(self, command_option, param):
  44. return cli_option(self.params, command_option, param)
  45. def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
  46. return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
  47. def _valueless_option(self, command_option, param, expected_value=True):
  48. return cli_valueless_option(self.params, command_option, param, expected_value)
  49. def _configuration_args(self, default=[]):
  50. return cli_configuration_args(self.params, 'external_downloader_args', default)
  51. def _call_downloader(self, tmpfilename, info_dict):
  52. """ Either overwrite this or implement _make_cmd """
  53. cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
  54. self._debug_cmd(cmd)
  55. p = subprocess.Popen(
  56. cmd, stderr=subprocess.PIPE)
  57. _, stderr = p.communicate()
  58. if p.returncode != 0:
  59. self.to_stderr(stderr)
  60. return p.returncode
  61. class CurlFD(ExternalFD):
  62. def _make_cmd(self, tmpfilename, info_dict):
  63. cmd = [self.exe, '--location', '-o', tmpfilename]
  64. for key, val in info_dict['http_headers'].items():
  65. cmd += ['--header', '%s: %s' % (key, val)]
  66. cmd += self._option('--interface', 'source_address')
  67. cmd += self._option('--proxy', 'proxy')
  68. cmd += self._valueless_option('--insecure', 'nocheckcertificate')
  69. cmd += self._configuration_args()
  70. cmd += ['--', info_dict['url']]
  71. return cmd
  72. class AxelFD(ExternalFD):
  73. def _make_cmd(self, tmpfilename, info_dict):
  74. cmd = [self.exe, '-o', tmpfilename]
  75. for key, val in info_dict['http_headers'].items():
  76. cmd += ['-H', '%s: %s' % (key, val)]
  77. cmd += self._configuration_args()
  78. cmd += ['--', info_dict['url']]
  79. return cmd
  80. class WgetFD(ExternalFD):
  81. def _make_cmd(self, tmpfilename, info_dict):
  82. cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
  83. for key, val in info_dict['http_headers'].items():
  84. cmd += ['--header', '%s: %s' % (key, val)]
  85. cmd += self._option('--bind-address', 'source_address')
  86. cmd += self._option('--proxy', 'proxy')
  87. cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
  88. cmd += self._configuration_args()
  89. cmd += ['--', info_dict['url']]
  90. return cmd
  91. class Aria2cFD(ExternalFD):
  92. def _make_cmd(self, tmpfilename, info_dict):
  93. cmd = [self.exe, '-c']
  94. cmd += self._configuration_args([
  95. '--min-split-size', '1M', '--max-connection-per-server', '4'])
  96. dn = os.path.dirname(tmpfilename)
  97. if dn:
  98. cmd += ['--dir', dn]
  99. cmd += ['--out', os.path.basename(tmpfilename)]
  100. for key, val in info_dict['http_headers'].items():
  101. cmd += ['--header', '%s: %s' % (key, val)]
  102. cmd += self._option('--interface', 'source_address')
  103. cmd += self._option('--all-proxy', 'proxy')
  104. cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
  105. cmd += ['--', info_dict['url']]
  106. return cmd
  107. class HttpieFD(ExternalFD):
  108. def _make_cmd(self, tmpfilename, info_dict):
  109. cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
  110. for key, val in info_dict['http_headers'].items():
  111. cmd += ['%s:%s' % (key, val)]
  112. return cmd
  113. _BY_NAME = dict(
  114. (klass.get_basename(), klass)
  115. for name, klass in globals().items()
  116. if name.endswith('FD') and name != 'ExternalFD'
  117. )
  118. def list_external_downloaders():
  119. return sorted(_BY_NAME.keys())
  120. def get_external_downloader(external_downloader):
  121. """ Given the name of the executable, see whether we support the given
  122. downloader . """
  123. # Drop .exe extension on Windows
  124. bn = os.path.splitext(os.path.basename(external_downloader))[0]
  125. return _BY_NAME[bn]