2014-11-26 11:01:20 -08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-01-06 20:59:22 -08:00
|
|
|
from .common import PostProcessor
|
2016-03-03 03:24:24 -08:00
|
|
|
from ..compat import compat_os_name
|
2014-01-06 20:59:22 -08:00
|
|
|
from ..utils import (
|
|
|
|
hyphenate_date,
|
2016-09-29 09:28:32 -07:00
|
|
|
write_xattr,
|
|
|
|
XAttrMetadataError,
|
|
|
|
XAttrUnavailableError,
|
2014-01-06 20:59:22 -08: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
|
2014-01-06 21:34:55 -08:00
|
|
|
self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs')
|
2014-01-06 20:59:22 -08: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 10:00:14 -08:00
|
|
|
num_written = 0
|
2014-01-06 20:59:22 -08:00
|
|
|
for xattrname, infoname in xattr_mapping.items():
|
|
|
|
|
|
|
|
value = info.get(infoname)
|
|
|
|
|
|
|
|
if value:
|
2016-02-14 01:37:17 -08:00
|
|
|
if infoname == 'upload_date':
|
2014-01-06 20:59:22 -08:00
|
|
|
value = hyphenate_date(value)
|
|
|
|
|
2014-01-06 21:49:15 -08:00
|
|
|
byte_value = value.encode('utf-8')
|
2014-01-06 21:11:21 -08:00
|
|
|
write_xattr(filename, xattrname, byte_value)
|
2017-12-13 10:00:14 -08:00
|
|
|
num_written += 1
|
2014-01-06 20:59:22 -08:00
|
|
|
|
2015-04-18 02:36:42 -07:00
|
|
|
return [], info
|
2014-01-06 20:59:22 -08:00
|
|
|
|
2016-09-29 09:28:32 -07:00
|
|
|
except XAttrUnavailableError as e:
|
|
|
|
self._downloader.report_error(str(e))
|
|
|
|
return [], info
|
|
|
|
|
2015-05-13 23:26:47 -07:00
|
|
|
except XAttrMetadataError as e:
|
|
|
|
if e.reason == 'NO_SPACE':
|
|
|
|
self._downloader.report_warning(
|
2017-12-13 10:00:14 -08: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-13 23:51:00 -07:00
|
|
|
elif e.reason == 'VALUE_TOO_LONG':
|
|
|
|
self._downloader.report_warning(
|
|
|
|
'Unable to write extended attributes due to too long values.')
|
2015-05-13 23:26:47 -07:00
|
|
|
else:
|
2015-05-14 01:53:10 -07:00
|
|
|
msg = 'This filesystem doesn\'t support extended attributes. '
|
2016-03-03 03:24:24 -08:00
|
|
|
if compat_os_name == 'nt':
|
2015-05-14 01:53:10 -07:00
|
|
|
msg += 'You need to use NTFS.'
|
|
|
|
else:
|
|
|
|
msg += '(You may have to enable them in your /etc/fstab)'
|
|
|
|
self._downloader.report_error(msg)
|
2015-04-18 02:36:42 -07:00
|
|
|
return [], info
|