Split postprocessor package into multiple modules
[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     hyphenate_date,
8 )
9
10
11 class XAttrMetadataPP(PostProcessor):
12
13     #
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
18     #
19     # TODO:
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'
22     #
23
24     def run(self, info):
25         """ Set extended attributes on downloaded file (if xattr support is found). """
26
27         # This mess below finds the best xattr tool for the job and creates a
28         # "write_xattr" function.
29         try:
30             # try the pyxattr module...
31             import xattr
32             def write_xattr(path, key, value):
33                 return xattr.setxattr(path, key, value)
34
35         except ImportError:
36
37             if os.name == 'posix':
38                 def which(bin):
39                     for dir in os.environ["PATH"].split(":"):
40                         path = os.path.join(dir, bin)
41                         if os.path.exists(path):
42                             return path
43
44                 user_has_setfattr = which("setfattr")
45                 user_has_xattr    = which("xattr")
46
47                 if user_has_setfattr or user_has_xattr:
48
49                     def write_xattr(path, key, value):
50                         import errno
51                         potential_errors = {
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,
57                         }
58
59                         if user_has_setfattr:
60                             cmd = ['setfattr', '-n', key, '-v', value, path]
61                         elif user_has_xattr:
62                             cmd = ['xattr', '-w', key, value, path]
63
64                         try:
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)
71                                     e.__cause__ = None
72                                     raise e
73                             raise # Reraise unhandled error
74
75                 else:
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.")
81             else:
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))
87
88                     ads_fn = path + ":" + key
89                     with open(ads_fn, "w") as f:
90                         f.write(value)
91
92         # Write the metadata to the file's xattrs
93         self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
94
95         filename = info['filepath']
96
97         try:
98             xattr_mapping = {
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',
106             }
107
108             for xattrname, infoname in xattr_mapping.items():
109
110                 value = info.get(infoname)
111
112                 if value:
113                     if infoname == "upload_date":
114                         value = hyphenate_date(value)
115
116                     write_xattr(filename, xattrname, value)
117
118             return True, info
119
120         except OSError:
121             self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
122             return False, info
123