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