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.

126 lines
4.0 KiB

  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. source_address = self.params.get('source_address')
  41. if source_address is None:
  42. return []
  43. return [command_option, source_address]
  44. def _call_downloader(self, tmpfilename, info_dict):
  45. """ Either overwrite this or implement _make_cmd """
  46. cmd = self._make_cmd(tmpfilename, info_dict)
  47. if sys.platform == 'win32' and sys.version_info < (3, 0):
  48. # Windows subprocess module does not actually support Unicode
  49. # on Python 2.x
  50. # See http://stackoverflow.com/a/9951851/35070
  51. subprocess_encoding = sys.getfilesystemencoding()
  52. cmd = [a.encode(subprocess_encoding, 'ignore') for a in cmd]
  53. else:
  54. subprocess_encoding = None
  55. self._debug_cmd(cmd, subprocess_encoding)
  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._source_address('--interface')
  68. cmd += ['--', info_dict['url']]
  69. return cmd
  70. class WgetFD(ExternalFD):
  71. def _make_cmd(self, tmpfilename, info_dict):
  72. cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies']
  73. for key, val in info_dict['http_headers'].items():
  74. cmd += ['--header', '%s: %s' % (key, val)]
  75. cmd += self._source_address('--bind-address')
  76. cmd += ['--', info_dict['url']]
  77. return cmd
  78. class Aria2cFD(ExternalFD):
  79. def _make_cmd(self, tmpfilename, info_dict):
  80. cmd = [
  81. self.exe, '-c',
  82. '--min-split-size', '1M', '--max-connection-per-server', '4']
  83. dn = os.path.dirname(tmpfilename)
  84. if dn:
  85. cmd += ['--dir', dn]
  86. cmd += ['--out', os.path.basename(tmpfilename)]
  87. for key, val in info_dict['http_headers'].items():
  88. cmd += ['--header', '%s: %s' % (key, val)]
  89. cmd += self._source_address('--interface')
  90. cmd += ['--', info_dict['url']]
  91. return cmd
  92. _BY_NAME = dict(
  93. (klass.get_basename(), klass)
  94. for name, klass in globals().items()
  95. if name.endswith('FD') and name != 'ExternalFD'
  96. )
  97. def list_external_downloaders():
  98. return sorted(_BY_NAME.keys())
  99. def get_external_downloader(external_downloader):
  100. """ Given the name of the executable, see whether we support the given
  101. downloader . """
  102. bn = os.path.basename(external_downloader)
  103. return _BY_NAME[bn]