2022-04-28 19:48:36 -06:00
|
|
|
import os
|
|
|
|
|
2014-01-06 21:59:22 -07:00
|
|
|
from .common import PostProcessor
|
2016-03-03 04:24:24 -07:00
|
|
|
from ..compat import compat_os_name
|
2014-01-06 21:59:22 -07:00
|
|
|
from ..utils import (
|
2021-09-23 18:50:42 -06:00
|
|
|
PostProcessingError,
|
2016-09-29 10:28:32 -06:00
|
|
|
XAttrMetadataError,
|
|
|
|
XAttrUnavailableError,
|
2022-04-11 16:32:57 -06:00
|
|
|
hyphenate_date,
|
|
|
|
write_xattr,
|
2014-01-06 21:59:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class XAttrMetadataPP(PostProcessor):
|
2022-04-30 17:16:05 -06: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-06 21:59:22 -07:00
|
|
|
|
|
|
|
def run(self, info):
|
2022-04-30 17:16:05 -06:00
|
|
|
mtime = os.stat(info['filepath']).st_mtime
|
2021-01-07 12:28:41 -07:00
|
|
|
self.to_screen('Writing metadata to file\'s xattrs')
|
2014-01-06 21:59:22 -07:00
|
|
|
try:
|
2022-04-30 17:16:05 -06:00
|
|
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
2014-01-06 21:59:22 -07:00
|
|
|
value = info.get(infoname)
|
|
|
|
if value:
|
2016-02-14 02:37:17 -07:00
|
|
|
if infoname == 'upload_date':
|
2014-01-06 21:59:22 -07:00
|
|
|
value = hyphenate_date(value)
|
2022-05-09 05:54:28 -06:00
|
|
|
write_xattr(info['filepath'], xattrname, value.encode())
|
2014-01-06 21:59:22 -07:00
|
|
|
|
2016-09-29 10:28:32 -06:00
|
|
|
except XAttrUnavailableError as e:
|
2021-09-23 18:21:54 -06:00
|
|
|
raise PostProcessingError(str(e))
|
2015-05-14 00:26:47 -06:00
|
|
|
except XAttrMetadataError as e:
|
|
|
|
if e.reason == 'NO_SPACE':
|
2021-01-10 06:44:54 -07:00
|
|
|
self.report_warning(
|
2019-05-10 14:56:22 -06:00
|
|
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
2022-04-30 17:16:05 -06:00
|
|
|
'Some extended attributes are not written')
|
2015-05-14 00:51:00 -06:00
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
2022-04-30 17:16:05 -06:00
|
|
|
self.report_warning('Unable to write extended attributes due to too long values.')
|
2015-05-14 00:26:47 -06:00
|
|
|
else:
|
2022-04-30 17:16:05 -06: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-28 19:48:36 -06:00
|
|
|
|
2022-04-30 17:16:05 -06:00
|
|
|
self.try_utime(info['filepath'], mtime, mtime)
|
2022-04-28 19:48:36 -06:00
|
|
|
return [], info
|