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.

79 lines
2.9 KiB

  1. from __future__ import unicode_literals
  2. from .common import PostProcessor
  3. from ..compat import compat_os_name
  4. from ..utils import (
  5. hyphenate_date,
  6. write_xattr,
  7. XAttrMetadataError,
  8. XAttrUnavailableError,
  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. # Write the metadata to the file's xattrs
  24. self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
  25. filename = info['filepath']
  26. try:
  27. xattr_mapping = {
  28. 'user.xdg.referrer.url': 'webpage_url',
  29. # 'user.xdg.comment': 'description',
  30. 'user.dublincore.title': 'title',
  31. 'user.dublincore.date': 'upload_date',
  32. 'user.dublincore.description': 'description',
  33. 'user.dublincore.contributor': 'uploader',
  34. 'user.dublincore.format': 'format',
  35. }
  36. num_written = 0
  37. for xattrname, infoname in xattr_mapping.items():
  38. value = info.get(infoname)
  39. if value:
  40. if infoname == 'upload_date':
  41. value = hyphenate_date(value)
  42. byte_value = value.encode('utf-8')
  43. write_xattr(filename, xattrname, byte_value)
  44. num_written += 1
  45. return [], info
  46. except XAttrUnavailableError as e:
  47. self._downloader.report_error(str(e))
  48. return [], info
  49. except XAttrMetadataError as e:
  50. if e.reason == 'NO_SPACE':
  51. self._downloader.report_warning(
  52. 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
  53. + (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
  54. elif e.reason == 'VALUE_TOO_LONG':
  55. self._downloader.report_warning(
  56. 'Unable to write extended attributes due to too long values.')
  57. else:
  58. msg = 'This filesystem doesn\'t support extended attributes. '
  59. if compat_os_name == 'nt':
  60. msg += 'You need to use NTFS.'
  61. else:
  62. msg += '(You may have to enable them in your /etc/fstab)'
  63. self._downloader.report_error(msg)
  64. return [], info