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.

154 lines
5.1 KiB

  1. from __future__ import unicode_literals
  2. import os.path
  3. import subprocess
  4. from .common import FileDownloader
  5. from ..utils import (
  6. encodeFilename,
  7. encodeArgument,
  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 _option(self, command_option, param):
  40. param = self.params.get(param)
  41. if param is None or param is False:
  42. return []
  43. if isinstance(param, bool):
  44. return [command_option]
  45. return [command_option, param]
  46. def _configuration_args(self, default=[]):
  47. ex_args = self.params.get('external_downloader_args')
  48. if ex_args is None:
  49. return default
  50. assert isinstance(ex_args, list)
  51. return ex_args
  52. def _call_downloader(self, tmpfilename, info_dict):
  53. """ Either overwrite this or implement _make_cmd """
  54. cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
  55. self._debug_cmd(cmd)
  56. p = subprocess.Popen(
  57. cmd, stderr=subprocess.PIPE)
  58. _, stderr = p.communicate()
  59. if p.returncode != 0:
  60. self.to_stderr(stderr)
  61. return p.returncode
  62. class CurlFD(ExternalFD):
  63. def _make_cmd(self, tmpfilename, info_dict):
  64. cmd = [self.exe, '--location', '-o', tmpfilename]
  65. for key, val in info_dict['http_headers'].items():
  66. cmd += ['--header', '%s: %s' % (key, val)]
  67. cmd += self._option('--interface', 'source_address')
  68. cmd += self._option('--proxy', 'proxy')
  69. cmd += self._option('--insecure', 'nocheckcertificate')
  70. cmd += self._configuration_args()
  71. cmd += ['--', info_dict['url']]
  72. return cmd
  73. class AxelFD(ExternalFD):
  74. def _make_cmd(self, tmpfilename, info_dict):
  75. cmd = [self.exe, '-o', tmpfilename]
  76. for key, val in info_dict['http_headers'].items():
  77. cmd += ['-H', '%s: %s' % (key, val)]
  78. cmd += self._configuration_args()
  79. cmd += ['--', info_dict['url']]
  80. return cmd
  81. class WgetFD(ExternalFD):
  82. def _make_cmd(self, tmpfilename, info_dict):
  83. cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
  84. for key, val in info_dict['http_headers'].items():
  85. cmd += ['--header', '%s: %s' % (key, val)]
  86. cmd += self._option('--bind-address', 'source_address')
  87. cmd += self._option('--proxy', 'proxy')
  88. cmd += self._option('--no-check-certificate', 'nocheckcertificate')
  89. cmd += self._configuration_args()
  90. cmd += ['--', info_dict['url']]
  91. return cmd
  92. class Aria2cFD(ExternalFD):
  93. def _make_cmd(self, tmpfilename, info_dict):
  94. cmd = [self.exe, '-c']
  95. cmd += self._configuration_args([
  96. '--min-split-size', '1M', '--max-connection-per-server', '4'])
  97. dn = os.path.dirname(tmpfilename)
  98. if dn:
  99. cmd += ['--dir', dn]
  100. cmd += ['--out', os.path.basename(tmpfilename)]
  101. for key, val in info_dict['http_headers'].items():
  102. cmd += ['--header', '%s: %s' % (key, val)]
  103. cmd += self._option('--interface', 'source_address')
  104. cmd += self._option('--all-proxy', 'proxy')
  105. cmd += self._option('--check-certificate=false', 'nocheckcertificate')
  106. cmd += ['--', info_dict['url']]
  107. return cmd
  108. class HttpieFD(ExternalFD):
  109. def _make_cmd(self, tmpfilename, info_dict):
  110. cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
  111. for key, val in info_dict['http_headers'].items():
  112. cmd += ['%s:%s' % (key, val)]
  113. return cmd
  114. _BY_NAME = dict(
  115. (klass.get_basename(), klass)
  116. for name, klass in globals().items()
  117. if name.endswith('FD') and name != 'ExternalFD'
  118. )
  119. def list_external_downloaders():
  120. return sorted(_BY_NAME.keys())
  121. def get_external_downloader(external_downloader):
  122. """ Given the name of the executable, see whether we support the given
  123. downloader . """
  124. # Drop .exe extension on Windows
  125. bn = os.path.splitext(os.path.basename(external_downloader))[0]
  126. return _BY_NAME[bn]