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.

110 lines
4.1 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. preferredencoding,
  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(key.find(":") < 0)
  36. assert(path.find(":") < 0)
  37. assert(os.path.exists(path))
  38. ads_fn = path + ":" + key
  39. with open(ads_fn, "w") as f:
  40. f.write(value)
  41. else:
  42. user_has_setfattr = check_executable("setfattr", ['--version'])
  43. user_has_xattr = check_executable("xattr", ['-h'])
  44. if user_has_setfattr or user_has_xattr:
  45. def write_xattr(path, key, value):
  46. if user_has_setfattr:
  47. cmd = ['setfattr', '-n', key, '-v', value, path]
  48. elif user_has_xattr:
  49. cmd = ['xattr', '-w', key, value, path]
  50. subprocess.check_output(cmd)
  51. else:
  52. # On Unix, and can't find pyxattr, setfattr, or xattr.
  53. if sys.platform.startswith('linux'):
  54. self._downloader.report_error(
  55. "Couldn't find a tool to set the xattrs. "
  56. "Install either the python 'pyxattr' or 'xattr' "
  57. "modules, or the GNU 'attr' package "
  58. "(which contains the 'setfattr' tool).")
  59. else:
  60. self._downloader.report_error(
  61. "Couldn't find a tool to set the xattrs. "
  62. "Install either the python 'xattr' module, "
  63. "or the 'xattr' binary.")
  64. # Write the metadata to the file's xattrs
  65. self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
  66. filename = info['filepath']
  67. try:
  68. xattr_mapping = {
  69. 'user.xdg.referrer.url': 'webpage_url',
  70. # 'user.xdg.comment': 'description',
  71. 'user.dublincore.title': 'title',
  72. 'user.dublincore.date': 'upload_date',
  73. 'user.dublincore.description': 'description',
  74. 'user.dublincore.contributor': 'uploader',
  75. 'user.dublincore.format': 'format',
  76. }
  77. for xattrname, infoname in xattr_mapping.items():
  78. value = info.get(infoname)
  79. if value:
  80. if infoname == "upload_date":
  81. value = hyphenate_date(value)
  82. byte_value = value.encode('utf-8')
  83. write_xattr(filename, xattrname, byte_value)
  84. return True, info
  85. except (subprocess.CalledProcessError, OSError):
  86. self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
  87. return False, info