2022-04-29 01:48:36 +00:00
|
|
|
import os
|
|
|
|
|
2014-01-07 04:59:22 +00:00
|
|
|
from .common import PostProcessor
|
|
|
|
from ..utils import (
|
2021-09-24 00:50:42 +00:00
|
|
|
PostProcessingError,
|
2016-09-29 16:28:32 +00:00
|
|
|
XAttrMetadataError,
|
|
|
|
XAttrUnavailableError,
|
2022-04-11 22:32:57 +00:00
|
|
|
hyphenate_date,
|
|
|
|
write_xattr,
|
2014-01-07 04:59:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class XAttrMetadataPP(PostProcessor):
|
2022-04-30 23:16:05 +00:00
|
|
|
"""Set extended attributes on downloaded file (if xattr support is found)
|
|
|
|
|
|
|
|
More info about extended attributes for media:
|
|
|
|
http://freedesktop.org/wiki/CommonExtendedAttributes/
|
|
|
|
http://www.freedesktop.org/wiki/PhreedomDraft/
|
|
|
|
http://dublincore.org/documents/usageguide/elements.shtml
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
* capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
|
|
|
|
* figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
|
|
|
|
"""
|
|
|
|
|
|
|
|
XATTR_MAPPING = {
|
|
|
|
'user.xdg.referrer.url': 'webpage_url',
|
|
|
|
'user.dublincore.title': 'title',
|
|
|
|
'user.dublincore.date': 'upload_date',
|
|
|
|
'user.dublincore.contributor': 'uploader',
|
|
|
|
'user.dublincore.format': 'format',
|
2024-10-07 23:17:55 +00:00
|
|
|
# We do this last because it may get us close to the xattr limits
|
|
|
|
# (e.g., 4kB on ext4), and we don't want to have the other ones fail
|
|
|
|
'user.dublincore.description': 'description',
|
|
|
|
# 'user.xdg.comment': 'description',
|
2022-04-30 23:16:05 +00:00
|
|
|
}
|
2014-01-07 04:59:22 +00:00
|
|
|
|
|
|
|
def run(self, info):
|
2022-04-30 23:16:05 +00:00
|
|
|
mtime = os.stat(info['filepath']).st_mtime
|
2021-01-07 19:28:41 +00:00
|
|
|
self.to_screen('Writing metadata to file\'s xattrs')
|
2024-10-07 23:17:55 +00:00
|
|
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
|
|
|
try:
|
2014-01-07 04:59:22 +00:00
|
|
|
value = info.get(infoname)
|
|
|
|
if value:
|
2016-02-14 09:37:17 +00:00
|
|
|
if infoname == 'upload_date':
|
2014-01-07 04:59:22 +00:00
|
|
|
value = hyphenate_date(value)
|
2022-05-09 11:54:28 +00:00
|
|
|
write_xattr(info['filepath'], xattrname, value.encode())
|
2014-01-07 04:59:22 +00:00
|
|
|
|
2024-10-07 23:17:55 +00:00
|
|
|
except XAttrUnavailableError as e:
|
|
|
|
raise PostProcessingError(str(e))
|
|
|
|
except XAttrMetadataError as e:
|
|
|
|
if e.reason == 'NO_SPACE':
|
|
|
|
self.report_warning(
|
|
|
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
|
|
|
f'Extended attribute "{xattrname}" was not written.')
|
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
|
|
|
self.report_warning(f'Unable to write extended attribute "{xattrname}" due to too long values.')
|
|
|
|
else:
|
2024-11-16 23:24:11 +00:00
|
|
|
tip = ('You need to use NTFS' if os.name == 'nt'
|
2024-10-07 23:17:55 +00:00
|
|
|
else 'You may have to enable them in your "/etc/fstab"')
|
|
|
|
raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')
|
2022-04-29 01:48:36 +00:00
|
|
|
|
2022-04-30 23:16:05 +00:00
|
|
|
self.try_utime(info['filepath'], mtime, mtime)
|
2022-04-29 01:48:36 +00:00
|
|
|
return [], info
|