2022-04-29 03:48:36 +02:00
|
|
|
import os
|
|
|
|
|
2014-01-07 05:59:22 +01:00
|
|
|
from .common import PostProcessor
|
|
|
|
from ..utils import (
|
2021-09-24 02:50:42 +02:00
|
|
|
PostProcessingError,
|
2016-09-29 18:28:32 +02:00
|
|
|
XAttrMetadataError,
|
|
|
|
XAttrUnavailableError,
|
2022-04-12 00:32:57 +02:00
|
|
|
hyphenate_date,
|
|
|
|
write_xattr,
|
2014-01-07 05:59:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class XAttrMetadataPP(PostProcessor):
|
2022-05-01 01:16:05 +02: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-08 01:17:55 +02: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-05-01 01:16:05 +02:00
|
|
|
}
|
2014-01-07 05:59:22 +01:00
|
|
|
|
|
|
|
def run(self, info):
|
2022-05-01 01:16:05 +02:00
|
|
|
mtime = os.stat(info['filepath']).st_mtime
|
2021-01-07 20:28:41 +01:00
|
|
|
self.to_screen('Writing metadata to file\'s xattrs')
|
2024-10-08 01:17:55 +02:00
|
|
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
|
|
|
try:
|
2014-01-07 05:59:22 +01:00
|
|
|
value = info.get(infoname)
|
|
|
|
if value:
|
2016-02-14 10:37:17 +01:00
|
|
|
if infoname == 'upload_date':
|
2014-01-07 05:59:22 +01:00
|
|
|
value = hyphenate_date(value)
|
2022-05-09 13:54:28 +02:00
|
|
|
write_xattr(info['filepath'], xattrname, value.encode())
|
2014-01-07 05:59:22 +01:00
|
|
|
|
2024-10-08 01:17:55 +02: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-17 00:24:11 +01:00
|
|
|
tip = ('You need to use NTFS' if os.name == 'nt'
|
2024-10-08 01:17:55 +02: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 03:48:36 +02:00
|
|
|
|
2022-05-01 01:16:05 +02:00
|
|
|
self.try_utime(info['filepath'], mtime, mtime)
|
2022-04-29 03:48:36 +02:00
|
|
|
return [], info
|