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.

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