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.

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