[ie/dropbox] Fix password-protected video extraction (#11636)
[yt-dlp3.git] / yt_dlp / postprocessor / xattrpp.py
blobe486b797b7cf0105b760a361ec2f005752dda890
1 import os
3 from .common import PostProcessor
4 from ..utils import (
5 PostProcessingError,
6 XAttrMetadataError,
7 XAttrUnavailableError,
8 hyphenate_date,
9 write_xattr,
13 class XAttrMetadataPP(PostProcessor):
14 """Set extended attributes on downloaded file (if xattr support is found)
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
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 """
26 XATTR_MAPPING = {
27 'user.xdg.referrer.url': 'webpage_url',
28 'user.dublincore.title': 'title',
29 'user.dublincore.date': 'upload_date',
30 'user.dublincore.contributor': 'uploader',
31 'user.dublincore.format': 'format',
32 # We do this last because it may get us close to the xattr limits
33 # (e.g., 4kB on ext4), and we don't want to have the other ones fail
34 'user.dublincore.description': 'description',
35 # 'user.xdg.comment': 'description',
38 def run(self, info):
39 mtime = os.stat(info['filepath']).st_mtime
40 self.to_screen('Writing metadata to file\'s xattrs')
41 for xattrname, infoname in self.XATTR_MAPPING.items():
42 try:
43 value = info.get(infoname)
44 if value:
45 if infoname == 'upload_date':
46 value = hyphenate_date(value)
47 write_xattr(info['filepath'], xattrname, value.encode())
49 except XAttrUnavailableError as e:
50 raise PostProcessingError(str(e))
51 except XAttrMetadataError as e:
52 if e.reason == 'NO_SPACE':
53 self.report_warning(
54 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
55 f'Extended attribute "{xattrname}" was not written.')
56 elif e.reason == 'VALUE_TOO_LONG':
57 self.report_warning(f'Unable to write extended attribute "{xattrname}" due to too long values.')
58 else:
59 tip = ('You need to use NTFS' if os.name == 'nt'
60 else 'You may have to enable them in your "/etc/fstab"')
61 raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')
63 self.try_utime(info['filepath'], mtime, mtime)
64 return [], info