[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / postprocessor / xattrpp.py
blobf822eff41cf093fc2a3411b3310e8956c988f809
1 import os
3 from .common import PostProcessor
4 from ..compat import compat_os_name
5 from ..utils import (
6 PostProcessingError,
7 XAttrMetadataError,
8 XAttrUnavailableError,
9 hyphenate_date,
10 write_xattr,
14 class XAttrMetadataPP(PostProcessor):
15 """Set extended attributes on downloaded file (if xattr support is found)
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
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 """
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',
37 def run(self, info):
38 mtime = os.stat(info['filepath']).st_mtime
39 self.to_screen('Writing metadata to file\'s xattrs')
40 try:
41 for xattrname, infoname in self.XATTR_MAPPING.items():
42 value = info.get(infoname)
43 if value:
44 if infoname == 'upload_date':
45 value = hyphenate_date(value)
46 write_xattr(info['filepath'], xattrname, value.encode())
48 except XAttrUnavailableError as e:
49 raise PostProcessingError(str(e))
50 except XAttrMetadataError as e:
51 if e.reason == 'NO_SPACE':
52 self.report_warning(
53 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
54 'Some extended attributes are not written')
55 elif e.reason == 'VALUE_TOO_LONG':
56 self.report_warning('Unable to write extended attributes due to too long values.')
57 else:
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}')
62 self.try_utime(info['filepath'], mtime, mtime)
63 return [], info