]> jfr.im git - yt-dlp.git/blame - youtube_dl/postprocessor/xattrpp.py
Fix W504 and disable W503 (closes #20863)
[yt-dlp.git] / youtube_dl / postprocessor / xattrpp.py
CommitLineData
dcddc10a
PH
1from __future__ import unicode_literals
2
496c1923 3from .common import PostProcessor
e9c0cdd3 4from ..compat import compat_os_name
496c1923
PH
5from ..utils import (
6 hyphenate_date,
efa97bdc
YCH
7 write_xattr,
8 XAttrMetadataError,
9 XAttrUnavailableError,
496c1923
PH
10)
11
12
13class 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
496c1923 29 # Write the metadata to the file's xattrs
168da92b 30 self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
496c1923
PH
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
7974e289 45 num_written = 0
496c1923
PH
46 for xattrname, infoname in xattr_mapping.items():
47
48 value = info.get(infoname)
49
50 if value:
611c1dd9 51 if infoname == 'upload_date':
496c1923
PH
52 value = hyphenate_date(value)
53
afc7bc33 54 byte_value = value.encode('utf-8')
42cc71e8 55 write_xattr(filename, xattrname, byte_value)
7974e289 56 num_written += 1
496c1923 57
592e97e8 58 return [], info
496c1923 59
efa97bdc
YCH
60 except XAttrUnavailableError as e:
61 self._downloader.report_error(str(e))
62 return [], info
63
86c7fdb1
YCH
64 except XAttrMetadataError as e:
65 if e.reason == 'NO_SPACE':
66 self._downloader.report_warning(
3089bc74
S
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())
fbff30d2
YCH
69 elif e.reason == 'VALUE_TOO_LONG':
70 self._downloader.report_warning(
71 'Unable to write extended attributes due to too long values.')
86c7fdb1 72 else:
c827d4cf 73 msg = 'This filesystem doesn\'t support extended attributes. '
e9c0cdd3 74 if compat_os_name == 'nt':
c827d4cf
YCH
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)
592e97e8 79 return [], info