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...
33 def write_xattr(path, key, value):
34 return xattr.setxattr(path, key, value)
38 if os.name == 'posix':
40 for dir in os.environ["PATH"].split(":"):
41 path = os.path.join(dir, bin)
42 if os.path.exists(path):
45 user_has_setfattr = which("setfattr")
46 user_has_xattr = which("xattr")
48 if user_has_setfattr or user_has_xattr:
50 def write_xattr(path, key, value):
53 # setfattr: /tmp/blah: Operation not supported
54 "Operation not supported": errno.EOPNOTSUPP,
55 # setfattr: ~/blah: No such file or directory
56 # xattr: No such file: ~/blah
57 "No such file": errno.ENOENT,
61 cmd = ['setfattr', '-n', key, '-v', value, path]
63 cmd = ['xattr', '-w', key, value, path]
66 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
67 except subprocess.CalledProcessError as e:
68 errorstr = e.output.strip().decode()
69 for potential_errorstr, potential_errno in potential_errors.items():
70 if errorstr.find(potential_errorstr) > -1:
71 e = OSError(potential_errno, potential_errorstr)
74 raise # Reraise unhandled error
77 # On Unix, and can't find pyxattr, setfattr, or xattr.
78 if sys.platform.startswith('linux'):
79 self._downloader.report_error("Couldn't find a tool to set the xattrs. Install either the python 'pyxattr' or 'xattr' modules, or the GNU 'attr' package (which contains the 'setfattr' tool).")
80 elif sys.platform == 'darwin':
81 self._downloader.report_error("Couldn't find a tool to set the xattrs. Install either the python 'xattr' module, or the 'xattr' binary.")
83 # Write xattrs to NTFS Alternate Data Streams: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
84 def write_xattr(path, key, value):
85 assert(key.find(":") < 0)
86 assert(path.find(":") < 0)
87 assert(os.path.exists(path))
89 ads_fn = path + ":" + key
90 with open(ads_fn, "w") as f:
93 # Write the metadata to the file's xattrs
94 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
96 filename = info['filepath']
100 'user.xdg.referrer.url': 'webpage_url',
101 # 'user.xdg.comment': 'description',
102 'user.dublincore.title': 'title',
103 'user.dublincore.date': 'upload_date',
104 'user.dublincore.description': 'description',
105 'user.dublincore.contributor': 'uploader',
106 'user.dublincore.format': 'format',
109 for xattrname, infoname in xattr_mapping.items():
111 value = info.get(infoname)
114 if infoname == "upload_date":
115 value = hyphenate_date(value)
117 byte_value = value.encode(preferredencoding())
118 write_xattr(filename, xattrname, byte_value)
123 self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")