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(
80 "Couldn't find a tool to set the xattrs. "
81 "Install either the python 'pyxattr' or 'xattr' "
82 "modules, or the GNU 'attr' package "
83 "(which contains the 'setfattr' tool).")
85 self._downloader.report_error(
86 "Couldn't find a tool to set the xattrs. "
87 "Install either the python 'xattr' module, "
88 "or the 'xattr' binary.")
90 # Write xattrs to NTFS Alternate Data Streams: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
91 def write_xattr(path, key, value):
92 assert(key.find(":") < 0)
93 assert(path.find(":") < 0)
94 assert(os.path.exists(path))
96 ads_fn = path + ":" + key
97 with open(ads_fn, "w") as f:
100 # Write the metadata to the file's xattrs
101 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
103 filename = info['filepath']
107 'user.xdg.referrer.url': 'webpage_url',
108 # 'user.xdg.comment': 'description',
109 'user.dublincore.title': 'title',
110 'user.dublincore.date': 'upload_date',
111 'user.dublincore.description': 'description',
112 'user.dublincore.contributor': 'uploader',
113 'user.dublincore.format': 'format',
116 for xattrname, infoname in xattr_mapping.items():
118 value = info.get(infoname)
121 if infoname == "upload_date":
122 value = hyphenate_date(value)
124 byte_value = value.encode(preferredencoding())
125 write_xattr(filename, xattrname, byte_value)
130 self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")