2022-04-29 01:48:36 +00:00
|
|
|
import os
|
|
|
|
|
2014-01-07 04:59:22 +00:00
|
|
|
from .common import PostProcessor
|
2016-03-03 11:24:24 +00:00
|
|
|
from ..compat import compat_os_name
|
2014-01-07 04:59:22 +00:00
|
|
|
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.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 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')
|
2014-01-07 04:59:22 +00:00
|
|
|
try:
|
2022-04-30 23:16:05 +00:00
|
|
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
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
|
|
|
|
2016-09-29 16:28:32 +00:00
|
|
|
except XAttrUnavailableError as e:
|
2021-09-24 00:21:54 +00:00
|
|
|
raise PostProcessingError(str(e))
|
2015-05-14 06:26:47 +00:00
|
|
|
except XAttrMetadataError as e:
|
|
|
|
if e.reason == 'NO_SPACE':
|
2021-01-10 13:44:54 +00:00
|
|
|
self.report_warning(
|
2019-05-10 20:56:22 +00:00
|
|
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
2022-04-30 23:16:05 +00:00
|
|
|
'Some extended attributes are not written')
|
2015-05-14 06:51:00 +00:00
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
2022-04-30 23:16:05 +00:00
|
|
|
self.report_warning('Unable to write extended attributes due to too long values.')
|
2015-05-14 06:26:47 +00:00
|
|
|
else:
|
2022-04-30 23:16:05 +00: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 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
|