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.

109 lines
4.0 KiB

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