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