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