]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/xattrpp.py
Imported Upstream version 2014.11.21
[youtubedl] / youtube_dl / postprocessor / xattrpp.py
1 import os
2 import subprocess
3 import sys
4
5 from .common import PostProcessor
6 from ..compat import (
7 subprocess_check_output
8 )
9 from ..utils import (
10 check_executable,
11 hyphenate_date,
12 )
13
14
15 class XAttrMetadataPP(PostProcessor):
16
17 #
18 # More info about extended attributes for media:
19 # http://freedesktop.org/wiki/CommonExtendedAttributes/
20 # http://www.freedesktop.org/wiki/PhreedomDraft/
21 # http://dublincore.org/documents/usageguide/elements.shtml
22 #
23 # TODO:
24 # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
25 # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
26 #
27
28 def run(self, info):
29 """ Set extended attributes on downloaded file (if xattr support is found). """
30
31 # This mess below finds the best xattr tool for the job and creates a
32 # "write_xattr" function.
33 try:
34 # try the pyxattr module...
35 import xattr
36
37 def write_xattr(path, key, value):
38 return xattr.setxattr(path, key, value)
39
40 except ImportError:
41 if os.name == 'nt':
42 # Write xattrs to NTFS Alternate Data Streams:
43 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
44 def write_xattr(path, key, value):
45 assert ':' not in key
46 assert os.path.exists(path)
47
48 ads_fn = path + ":" + key
49 with open(ads_fn, "wb") as f:
50 f.write(value)
51 else:
52 user_has_setfattr = check_executable("setfattr", ['--version'])
53 user_has_xattr = check_executable("xattr", ['-h'])
54
55 if user_has_setfattr or user_has_xattr:
56
57 def write_xattr(path, key, value):
58 if user_has_setfattr:
59 cmd = ['setfattr', '-n', key, '-v', value, path]
60 elif user_has_xattr:
61 cmd = ['xattr', '-w', key, value, path]
62
63 subprocess_check_output(cmd)
64
65 else:
66 # On Unix, and can't find pyxattr, setfattr, or xattr.
67 if sys.platform.startswith('linux'):
68 self._downloader.report_error(
69 "Couldn't find a tool to set the xattrs. "
70 "Install either the python 'pyxattr' or 'xattr' "
71 "modules, or the GNU 'attr' package "
72 "(which contains the 'setfattr' tool).")
73 else:
74 self._downloader.report_error(
75 "Couldn't find a tool to set the xattrs. "
76 "Install either the python 'xattr' module, "
77 "or the 'xattr' binary.")
78
79 # Write the metadata to the file's xattrs
80 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
81
82 filename = info['filepath']
83
84 try:
85 xattr_mapping = {
86 'user.xdg.referrer.url': 'webpage_url',
87 # 'user.xdg.comment': 'description',
88 'user.dublincore.title': 'title',
89 'user.dublincore.date': 'upload_date',
90 'user.dublincore.description': 'description',
91 'user.dublincore.contributor': 'uploader',
92 'user.dublincore.format': 'format',
93 }
94
95 for xattrname, infoname in xattr_mapping.items():
96
97 value = info.get(infoname)
98
99 if value:
100 if infoname == "upload_date":
101 value = hyphenate_date(value)
102
103 byte_value = value.encode('utf-8')
104 write_xattr(filename, xattrname, byte_value)
105
106 return True, info
107
108 except (subprocess.CalledProcessError, OSError):
109 self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
110 return False, info
111