]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/xattrpp.py
New upstream version 2017.03.07
[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 for xattrname, infoname in xattr_mapping.items():
46
47 value = info.get(infoname)
48
49 if value:
50 if infoname == 'upload_date':
51 value = hyphenate_date(value)
52
53 byte_value = value.encode('utf-8')
54 write_xattr(filename, xattrname, byte_value)
55
56 return [], info
57
58 except XAttrUnavailableError as e:
59 self._downloader.report_error(str(e))
60 return [], info
61
62 except XAttrMetadataError as e:
63 if e.reason == 'NO_SPACE':
64 self._downloader.report_warning(
65 'There\'s no disk space left or disk quota exceeded. ' +
66 'Extended attributes are not written.')
67 elif e.reason == 'VALUE_TOO_LONG':
68 self._downloader.report_warning(
69 'Unable to write extended attributes due to too long values.')
70 else:
71 msg = 'This filesystem doesn\'t support extended attributes. '
72 if compat_os_name == 'nt':
73 msg += 'You need to use NTFS.'
74 else:
75 msg += '(You may have to enable them in your /etc/fstab)'
76 self._downloader.report_error(msg)
77 return [], info