5 from .common import PostProcessor
13 class XAttrMetadataPP(PostProcessor):
16 # More info about extended attributes for media:
17 # http://freedesktop.org/wiki/CommonExtendedAttributes/
18 # http://www.freedesktop.org/wiki/PhreedomDraft/
19 # http://dublincore.org/documents/usageguide/elements.shtml
22 # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
23 # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
27 """ Set extended attributes on downloaded file (if xattr support is found). """
29 # This mess below finds the best xattr tool for the job and creates a
30 # "write_xattr" function.
32 # try the pyxattr module...
35 def write_xattr(path, key, value):
36 return xattr.setxattr(path, key, value)
40 # Write xattrs to NTFS Alternate Data Streams:
41 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
42 def write_xattr(path, key, value):
43 assert(key.find(":") < 0)
44 assert(path.find(":") < 0)
45 assert(os.path.exists(path))
47 ads_fn = path + ":" + key
48 with open(ads_fn, "w") as f:
51 user_has_setfattr = check_executable("setfattr", ['--version'])
52 user_has_xattr = check_executable("xattr", ['-h'])
54 if user_has_setfattr or user_has_xattr:
56 def write_xattr(path, key, value):
58 cmd = ['setfattr', '-n', key, '-v', value, path]
60 cmd = ['xattr', '-w', key, value, path]
62 subprocess.check_output(cmd)
65 # On Unix, and can't find pyxattr, setfattr, or xattr.
66 if sys.platform.startswith('linux'):
67 self._downloader.report_error(
68 "Couldn't find a tool to set the xattrs. "
69 "Install either the python 'pyxattr' or 'xattr' "
70 "modules, or the GNU 'attr' package "
71 "(which contains the 'setfattr' tool).")
73 self._downloader.report_error(
74 "Couldn't find a tool to set the xattrs. "
75 "Install either the python 'xattr' module, "
76 "or the 'xattr' binary.")
78 # Write the metadata to the file's xattrs
79 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
81 filename = info['filepath']
85 'user.xdg.referrer.url': 'webpage_url',
86 # 'user.xdg.comment': 'description',
87 'user.dublincore.title': 'title',
88 'user.dublincore.date': 'upload_date',
89 'user.dublincore.description': 'description',
90 'user.dublincore.contributor': 'uploader',
91 'user.dublincore.format': 'format',
94 for xattrname, infoname in xattr_mapping.items():
96 value = info.get(infoname)
99 if infoname == "upload_date":
100 value = hyphenate_date(value)
102 byte_value = value.encode(preferredencoding())
103 write_xattr(filename, xattrname, byte_value)
107 except (subprocess.CalledProcessError, OSError):
108 self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")