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.8 KiB

  1. from __future__ import unicode_literals
  2. import os
  3. import subprocess
  4. import sys
  5. from .common import PostProcessor
  6. from ..compat import (
  7. subprocess_check_output
  8. )
  9. from ..utils import (
  10. check_executable,
  11. hyphenate_date,
  12. version_tuple,
  13. )
  14. class XAttrMetadataPP(PostProcessor):
  15. #
  16. # More info about extended attributes for media:
  17. # http://freedesktop.org/wiki/CommonExtendedAttributes/
  18. # http://www.freedesktop.org/wiki/PhreedomDraft/
  19. # http://dublincore.org/documents/usageguide/elements.shtml
  20. #
  21. # TODO:
  22. # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
  23. # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
  24. #
  25. def run(self, info):
  26. """ Set extended attributes on downloaded file (if xattr support is found). """
  27. # This mess below finds the best xattr tool for the job and creates a
  28. # "write_xattr" function.
  29. try:
  30. # try the pyxattr module...
  31. import xattr
  32. # Unicode arguments are not supported in python-pyxattr until
  33. # version 0.5.0
  34. # See https://github.com/rg3/youtube-dl/issues/5498
  35. pyxattr_required_version = '0.5.0'
  36. if version_tuple(xattr.__version__) < version_tuple(pyxattr_required_version):
  37. self._downloader.report_warning(
  38. 'python-pyxattr is detected but is too old. '
  39. 'youtube-dl requires %s or above while your version is %s. '
  40. 'Falling back to other xattr implementations' % (
  41. pyxattr_required_version, xattr.__version__))
  42. raise ImportError
  43. def write_xattr(path, key, value):
  44. return xattr.setxattr(path, key, value)
  45. except ImportError:
  46. if os.name == 'nt':
  47. # Write xattrs to NTFS Alternate Data Streams:
  48. # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
  49. def write_xattr(path, key, value):
  50. assert ':' not in key
  51. assert os.path.exists(path)
  52. ads_fn = path + ":" + key
  53. with open(ads_fn, "wb") as f:
  54. f.write(value)
  55. else:
  56. user_has_setfattr = check_executable("setfattr", ['--version'])
  57. user_has_xattr = check_executable("xattr", ['-h'])
  58. if user_has_setfattr or user_has_xattr:
  59. def write_xattr(path, key, value):
  60. if user_has_setfattr:
  61. cmd = ['setfattr', '-n', key, '-v', value, path]
  62. elif user_has_xattr:
  63. cmd = ['xattr', '-w', key, value, path]
  64. subprocess_check_output(cmd)
  65. else:
  66. # On Unix, and can't find pyxattr, setfattr, or xattr.
  67. if sys.platform.startswith('linux'):
  68. self._downloader.report_error(
  69. "Couldn't find a tool to set the xattrs. "
  70. "Install either the python 'pyxattr' or 'xattr' "
  71. "modules, or the GNU 'attr' package "
  72. "(which contains the 'setfattr' tool).")
  73. else:
  74. self._downloader.report_error(
  75. "Couldn't find a tool to set the xattrs. "
  76. "Install either the python 'xattr' module, "
  77. "or the 'xattr' binary.")
  78. # Write the metadata to the file's xattrs
  79. self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
  80. filename = info['filepath']
  81. try:
  82. xattr_mapping = {
  83. 'user.xdg.referrer.url': 'webpage_url',
  84. # 'user.xdg.comment': 'description',
  85. 'user.dublincore.title': 'title',
  86. 'user.dublincore.date': 'upload_date',
  87. 'user.dublincore.description': 'description',
  88. 'user.dublincore.contributor': 'uploader',
  89. 'user.dublincore.format': 'format',
  90. }
  91. for xattrname, infoname in xattr_mapping.items():
  92. value = info.get(infoname)
  93. if value:
  94. if infoname == "upload_date":
  95. value = hyphenate_date(value)
  96. byte_value = value.encode('utf-8')
  97. write_xattr(filename, xattrname, byte_value)
  98. return [], info
  99. except (subprocess.CalledProcessError, OSError):
  100. self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
  101. return [], info