5 from .common import PostProcessor
11 class XAttrMetadataPP(PostProcessor):
14 # More info about extended attributes for media:
15 # http://freedesktop.org/wiki/CommonExtendedAttributes/
16 # http://www.freedesktop.org/wiki/PhreedomDraft/
17 # http://dublincore.org/documents/usageguide/elements.shtml
20 # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
21 # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
25 """ Set extended attributes on downloaded file (if xattr support is found). """
27 # This mess below finds the best xattr tool for the job and creates a
28 # "write_xattr" function.
30 # try the pyxattr module...
32 def write_xattr(path, key, value):
33 return xattr.setxattr(path, key, value)
37 if os.name == 'posix':
39 for dir in os.environ["PATH"].split(":"):
40 path = os.path.join(dir, bin)
41 if os.path.exists(path):
44 user_has_setfattr = which("setfattr")
45 user_has_xattr = which("xattr")
47 if user_has_setfattr or user_has_xattr:
49 def write_xattr(path, key, value):
52 # setfattr: /tmp/blah: Operation not supported
53 "Operation not supported": errno.EOPNOTSUPP,
54 # setfattr: ~/blah: No such file or directory
55 # xattr: No such file: ~/blah
56 "No such file": errno.ENOENT,
60 cmd = ['setfattr', '-n', key, '-v', value, path]
62 cmd = ['xattr', '-w', key, value, path]
65 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
66 except subprocess.CalledProcessError as e:
67 errorstr = e.output.strip().decode()
68 for potential_errorstr, potential_errno in potential_errors.items():
69 if errorstr.find(potential_errorstr) > -1:
70 e = OSError(potential_errno, potential_errorstr)
73 raise # Reraise unhandled error
76 # On Unix, and can't find pyxattr, setfattr, or xattr.
77 if sys.platform.startswith('linux'):
78 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).")
79 elif sys.platform == 'darwin':
80 self._downloader.report_error("Couldn't find a tool to set the xattrs. Install either the python 'xattr' module, or the 'xattr' binary.")
82 # Write xattrs to NTFS Alternate Data Streams: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
83 def write_xattr(path, key, value):
84 assert(key.find(":") < 0)
85 assert(path.find(":") < 0)
86 assert(os.path.exists(path))
88 ads_fn = path + ":" + key
89 with open(ads_fn, "w") as f:
92 # Write the metadata to the file's xattrs
93 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
95 filename = info['filepath']
99 'user.xdg.referrer.url': 'webpage_url',
100 # 'user.xdg.comment': 'description',
101 'user.dublincore.title': 'title',
102 'user.dublincore.date': 'upload_date',
103 'user.dublincore.description': 'description',
104 'user.dublincore.contributor': 'uploader',
105 'user.dublincore.format': 'format',
108 for xattrname, infoname in xattr_mapping.items():
110 value = info.get(infoname)
113 if infoname == "upload_date":
114 value = hyphenate_date(value)
116 write_xattr(filename, xattrname, value)
121 self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")