]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/xattrpp.py
Update changelog
[youtubedl] / youtube_dl / postprocessor / xattrpp.py
1 from __future__ import unicode_literals
2
3 from .common import PostProcessor
4 from ..compat import compat_os_name
5 from ..utils import (
6 hyphenate_date,
7 write_xattr,
8 XAttrMetadataError,
9 XAttrUnavailableError,
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 # Write the metadata to the file's xattrs
30 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
31
32 filename = info['filepath']
33
34 try:
35 xattr_mapping = {
36 'user.xdg.referrer.url': 'webpage_url',
37 # 'user.xdg.comment': 'description',
38 'user.dublincore.title': 'title',
39 'user.dublincore.date': 'upload_date',
40 'user.dublincore.description': 'description',
41 'user.dublincore.contributor': 'uploader',
42 'user.dublincore.format': 'format',
43 }
44
45 num_written = 0
46 for xattrname, infoname in xattr_mapping.items():
47
48 value = info.get(infoname)
49
50 if value:
51 if infoname == 'upload_date':
52 value = hyphenate_date(value)
53
54 byte_value = value.encode('utf-8')
55 write_xattr(filename, xattrname, byte_value)
56 num_written += 1
57
58 return [], info
59
60 except XAttrUnavailableError as e:
61 self._downloader.report_error(str(e))
62 return [], info
63
64 except XAttrMetadataError as e:
65 if e.reason == 'NO_SPACE':
66 self._downloader.report_warning(
67 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. ' +
68 (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
69 elif e.reason == 'VALUE_TOO_LONG':
70 self._downloader.report_warning(
71 'Unable to write extended attributes due to too long values.')
72 else:
73 msg = 'This filesystem doesn\'t support extended attributes. '
74 if compat_os_name == 'nt':
75 msg += 'You need to use NTFS.'
76 else:
77 msg += '(You may have to enable them in your /etc/fstab)'
78 self._downloader.report_error(msg)
79 return [], info