2014-11-26 20:01:20 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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 (
|
|
|
|
hyphenate_date,
|
2016-09-29 18:28:32 +02:00
|
|
|
write_xattr,
|
2021-09-24 02:50:42 +02:00
|
|
|
PostProcessingError,
|
2016-09-29 18:28:32 +02:00
|
|
|
XAttrMetadataError,
|
|
|
|
XAttrUnavailableError,
|
2014-01-07 05:59:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class XAttrMetadataPP(PostProcessor):
|
|
|
|
#
|
|
|
|
# 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'
|
|
|
|
#
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
""" Set extended attributes on downloaded file (if xattr support is found). """
|
|
|
|
|
|
|
|
# Write the metadata to the file's xattrs
|
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
|
|
|
|
|
|
|
filename = info['filepath']
|
|
|
|
|
|
|
|
try:
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:00:14 +01:00
|
|
|
num_written = 0
|
2014-01-07 05:59:22 +01:00
|
|
|
for xattrname, infoname in xattr_mapping.items():
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-01-07 06:49:15 +01:00
|
|
|
byte_value = value.encode('utf-8')
|
2014-01-07 06:11:21 +01:00
|
|
|
write_xattr(filename, xattrname, byte_value)
|
2017-12-13 19:00:14 +01:00
|
|
|
num_written += 1
|
2014-01-07 05:59:22 +01:00
|
|
|
|
2015-04-18 11:36:42 +02:00
|
|
|
return [], info
|
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))
|
2016-09-29 18:28:32 +02:00
|
|
|
|
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. '
|
|
|
|
+ (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
|
2015-05-14 08:51:00 +02:00
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
2021-01-10 14:44:54 +01:00
|
|
|
self.report_warning(
|
2015-05-14 08:51:00 +02:00
|
|
|
'Unable to write extended attributes due to too long values.')
|
2015-05-14 08:26:47 +02:00
|
|
|
else:
|
2015-05-14 10:53:10 +02:00
|
|
|
msg = 'This filesystem doesn\'t support extended attributes. '
|
2016-03-03 12:24:24 +01:00
|
|
|
if compat_os_name == 'nt':
|
2015-05-14 10:53:10 +02:00
|
|
|
msg += 'You need to use NTFS.'
|
|
|
|
else:
|
|
|
|
msg += '(You may have to enable them in your /etc/fstab)'
|
2021-09-24 02:21:54 +02:00
|
|
|
raise PostProcessingError(str(e))
|
2015-04-18 11:36:42 +02:00
|
|
|
return [], info
|