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.

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