2022-04-29 03:48:36 +02:00
|
|
|
import os
|
|
|
|
|
2014-01-07 05:59:22 +01:00
|
|
|
from .common import PostProcessor
|
2016-03-03 12:24:24 +01:00
|
|
|
from ..compat import compat_os_name
|
2014-01-07 05:59:22 +01:00
|
|
|
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.xdg.comment': 'description',
|
|
|
|
'user.dublincore.title': 'title',
|
|
|
|
'user.dublincore.date': 'upload_date',
|
|
|
|
'user.dublincore.description': 'description',
|
|
|
|
'user.dublincore.contributor': 'uploader',
|
|
|
|
'user.dublincore.format': 'format',
|
|
|
|
}
|
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')
|
2014-01-07 05:59:22 +01:00
|
|
|
try:
|
2022-05-01 01:16:05 +02:00
|
|
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
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
|
|
|
|
2016-09-29 18:28:32 +02:00
|
|
|
except XAttrUnavailableError as e:
|
2021-09-24 02:21:54 +02:00
|
|
|
raise PostProcessingError(str(e))
|
2015-05-14 08:26:47 +02:00
|
|
|
except XAttrMetadataError as e:
|
|
|
|
if e.reason == 'NO_SPACE':
|
2021-01-10 14:44:54 +01:00
|
|
|
self.report_warning(
|
2019-05-10 22:56:22 +02:00
|
|
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
2022-05-01 01:16:05 +02:00
|
|
|
'Some extended attributes are not written')
|
2015-05-14 08:51:00 +02:00
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
2022-05-01 01:16:05 +02:00
|
|
|
self.report_warning('Unable to write extended attributes due to too long values.')
|
2015-05-14 08:26:47 +02:00
|
|
|
else:
|
2022-05-01 01:16:05 +02:00
|
|
|
tip = ('You need to use NTFS' if compat_os_name == 'nt'
|
|
|
|
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
|