]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/xattrpp.py
Merge tag 'upstream/2014.01.17.2'
[youtubedl] / youtube_dl / postprocessor / xattrpp.py
1 import os
2 import subprocess
3 import sys
4
5 from .common import PostProcessor
6 from ..utils import (
7 check_executable,
8 hyphenate_date,
9 )
10
11
12 class XAttrMetadataPP(PostProcessor):
13
14 #
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
19 #
20 # TODO:
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'
23 #
24
25 def run(self, info):
26 """ Set extended attributes on downloaded file (if xattr support is found). """
27
28 # This mess below finds the best xattr tool for the job and creates a
29 # "write_xattr" function.
30 try:
31 # try the pyxattr module...
32 import xattr
33
34 def write_xattr(path, key, value):
35 return xattr.setxattr(path, key, value)
36
37 except ImportError:
38 if os.name == 'nt':
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):
42 assert ':' not in key
43 assert os.path.exists(path)
44
45 ads_fn = path + ":" + key
46 with open(ads_fn, "wb") as f:
47 f.write(value)
48 else:
49 user_has_setfattr = check_executable("setfattr", ['--version'])
50 user_has_xattr = check_executable("xattr", ['-h'])
51
52 if user_has_setfattr or user_has_xattr:
53
54 def write_xattr(path, key, value):
55 if user_has_setfattr:
56 cmd = ['setfattr', '-n', key, '-v', value, path]
57 elif user_has_xattr:
58 cmd = ['xattr', '-w', key, value, path]
59
60 subprocess.check_output(cmd)
61
62 else:
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).")
70 else:
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.")
75
76 # Write the metadata to the file's xattrs
77 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
78
79 filename = info['filepath']
80
81 try:
82 xattr_mapping = {
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',
90 }
91
92 for xattrname, infoname in xattr_mapping.items():
93
94 value = info.get(infoname)
95
96 if value:
97 if infoname == "upload_date":
98 value = hyphenate_date(value)
99
100 byte_value = value.encode('utf-8')
101 write_xattr(filename, xattrname, byte_value)
102
103 return True, info
104
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)")
107 return False, info
108