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.

77 lines
2.7 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. for xattrname, infoname in xattr_mapping.items():
  37. value = info.get(infoname)
  38. if value:
  39. if infoname == 'upload_date':
  40. value = hyphenate_date(value)
  41. byte_value = value.encode('utf-8')
  42. write_xattr(filename, xattrname, byte_value)
  43. return [], info
  44. except XAttrUnavailableError as e:
  45. self._downloader.report_error(str(e))
  46. return [], info
  47. except XAttrMetadataError as e:
  48. if e.reason == 'NO_SPACE':
  49. self._downloader.report_warning(
  50. 'There\'s no disk space left or disk quota exceeded. ' +
  51. 'Extended attributes are not written.')
  52. elif e.reason == 'VALUE_TOO_LONG':
  53. self._downloader.report_warning(
  54. 'Unable to write extended attributes due to too long values.')
  55. else:
  56. msg = 'This filesystem doesn\'t support extended attributes. '
  57. if compat_os_name == 'nt':
  58. msg += 'You need to use NTFS.'
  59. else:
  60. msg += '(You may have to enable them in your /etc/fstab)'
  61. self._downloader.report_error(msg)
  62. return [], info