]> jfr.im git - yt-dlp.git/blame - yt_dlp/postprocessor/xattrpp.py
[cleanup] Minor fixes (See desc)
[yt-dlp.git] / yt_dlp / postprocessor / xattrpp.py
CommitLineData
1d485a1a 1import os
2
496c1923 3from .common import PostProcessor
e9c0cdd3 4from ..compat import compat_os_name
496c1923 5from ..utils import (
51ff9ca0 6 PostProcessingError,
efa97bdc
YCH
7 XAttrMetadataError,
8 XAttrUnavailableError,
f8271158 9 hyphenate_date,
10 write_xattr,
496c1923
PH
11)
12
13
14class XAttrMetadataPP(PostProcessor):
6f7563be 15 """Set extended attributes on downloaded file (if xattr support is found)
16
17 More info about extended attributes for media:
18 http://freedesktop.org/wiki/CommonExtendedAttributes/
19 http://www.freedesktop.org/wiki/PhreedomDraft/
20 http://dublincore.org/documents/usageguide/elements.shtml
21
22 TODO:
23 * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
24 * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
25 """
26
27 XATTR_MAPPING = {
28 'user.xdg.referrer.url': 'webpage_url',
29 # 'user.xdg.comment': 'description',
30 'user.dublincore.title': 'title',
31 'user.dublincore.date': 'upload_date',
32 'user.dublincore.description': 'description',
33 'user.dublincore.contributor': 'uploader',
34 'user.dublincore.format': 'format',
35 }
496c1923
PH
36
37 def run(self, info):
6f7563be 38 mtime = os.stat(info['filepath']).st_mtime
1b77b347 39 self.to_screen('Writing metadata to file\'s xattrs')
496c1923 40 try:
6f7563be 41 for xattrname, infoname in self.XATTR_MAPPING.items():
496c1923 42 value = info.get(infoname)
496c1923 43 if value:
611c1dd9 44 if infoname == 'upload_date':
496c1923 45 value = hyphenate_date(value)
0f06bcd7 46 write_xattr(info['filepath'], xattrname, value.encode())
496c1923 47
efa97bdc 48 except XAttrUnavailableError as e:
b1940459 49 raise PostProcessingError(str(e))
86c7fdb1
YCH
50 except XAttrMetadataError as e:
51 if e.reason == 'NO_SPACE':
f446cc66 52 self.report_warning(
3089bc74 53 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
6f7563be 54 'Some extended attributes are not written')
fbff30d2 55 elif e.reason == 'VALUE_TOO_LONG':
6f7563be 56 self.report_warning('Unable to write extended attributes due to too long values.')
86c7fdb1 57 else:
6f7563be 58 tip = ('You need to use NTFS' if compat_os_name == 'nt'
59 else 'You may have to enable them in your "/etc/fstab"')
60 raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')
1d485a1a 61
6f7563be 62 self.try_utime(info['filepath'], mtime, mtime)
1d485a1a 63 return [], info