[xattr] Rework
[youtube-dl] / 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     preferredencoding,
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(key.find(":") < 0)
44                     assert(path.find(":") < 0)
45                     assert(os.path.exists(path))
46
47                     ads_fn = path + ":" + key
48                     with open(ads_fn, "w") as f:
49                         f.write(value)
50             else:
51                 user_has_setfattr = check_executable("setfattr", ['--version'])
52                 user_has_xattr = check_executable("xattr", ['-h'])
53
54                 if user_has_setfattr or user_has_xattr:
55
56                     def write_xattr(path, key, value):
57                         if user_has_setfattr:
58                             cmd = ['setfattr', '-n', key, '-v', value, path]
59                         elif user_has_xattr:
60                             cmd = ['xattr', '-w', key, value, path]
61
62                         subprocess.check_output(cmd)
63
64                 else:
65                     # On Unix, and can't find pyxattr, setfattr, or xattr.
66                     if sys.platform.startswith('linux'):
67                         self._downloader.report_error(
68                             "Couldn't find a tool to set the xattrs. "
69                             "Install either the python 'pyxattr' or 'xattr' "
70                             "modules, or the GNU 'attr' package "
71                             "(which contains the 'setfattr' tool).")
72                     else:
73                         self._downloader.report_error(
74                             "Couldn't find a tool to set the xattrs. "
75                             "Install either the python 'xattr' module, "
76                             "or the 'xattr' binary.")
77
78         # Write the metadata to the file's xattrs
79         self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
80
81         filename = info['filepath']
82
83         try:
84             xattr_mapping = {
85                 'user.xdg.referrer.url': 'webpage_url',
86                 # 'user.xdg.comment':            'description',
87                 'user.dublincore.title': 'title',
88                 'user.dublincore.date': 'upload_date',
89                 'user.dublincore.description': 'description',
90                 'user.dublincore.contributor': 'uploader',
91                 'user.dublincore.format': 'format',
92             }
93
94             for xattrname, infoname in xattr_mapping.items():
95
96                 value = info.get(infoname)
97
98                 if value:
99                     if infoname == "upload_date":
100                         value = hyphenate_date(value)
101
102                     byte_value = value.encode(preferredencoding())
103                     write_xattr(filename, xattrname, byte_value)
104
105             return True, info
106
107         except (subprocess.CalledProcessError, OSError):
108             self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
109             return False, info
110