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.

111 lines
4.1 KiB

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