]>
Commit | Line | Data |
---|---|---|
dcddc10a PH |
1 | from __future__ import unicode_literals |
2 | ||
496c1923 | 3 | from .common import PostProcessor |
e9c0cdd3 | 4 | from ..compat import compat_os_name |
496c1923 PH |
5 | from ..utils import ( |
6 | hyphenate_date, | |
efa97bdc YCH |
7 | write_xattr, |
8 | XAttrMetadataError, | |
9 | XAttrUnavailableError, | |
496c1923 PH |
10 | ) |
11 | ||
12 | ||
13 | class XAttrMetadataPP(PostProcessor): | |
496c1923 PH |
14 | # |
15 | # More info about extended attributes for media: | |
16 | # http://freedesktop.org/wiki/CommonExtendedAttributes/ | |
17 | # http://www.freedesktop.org/wiki/PhreedomDraft/ | |
18 | # http://dublincore.org/documents/usageguide/elements.shtml | |
19 | # | |
20 | # TODO: | |
21 | # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated) | |
22 | # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution' | |
23 | # | |
24 | ||
25 | def run(self, info): | |
26 | """ Set extended attributes on downloaded file (if xattr support is found). """ | |
27 | ||
496c1923 | 28 | # Write the metadata to the file's xattrs |
1b77b347 | 29 | self.to_screen('Writing metadata to file\'s xattrs') |
496c1923 PH |
30 | |
31 | filename = info['filepath'] | |
32 | ||
33 | try: | |
34 | xattr_mapping = { | |
35 | 'user.xdg.referrer.url': 'webpage_url', | |
36 | # 'user.xdg.comment': 'description', | |
37 | 'user.dublincore.title': 'title', | |
38 | 'user.dublincore.date': 'upload_date', | |
39 | 'user.dublincore.description': 'description', | |
40 | 'user.dublincore.contributor': 'uploader', | |
41 | 'user.dublincore.format': 'format', | |
42 | } | |
43 | ||
7974e289 | 44 | num_written = 0 |
496c1923 PH |
45 | for xattrname, infoname in xattr_mapping.items(): |
46 | ||
47 | value = info.get(infoname) | |
48 | ||
49 | if value: | |
611c1dd9 | 50 | if infoname == 'upload_date': |
496c1923 PH |
51 | value = hyphenate_date(value) |
52 | ||
afc7bc33 | 53 | byte_value = value.encode('utf-8') |
42cc71e8 | 54 | write_xattr(filename, xattrname, byte_value) |
7974e289 | 55 | num_written += 1 |
496c1923 | 56 | |
592e97e8 | 57 | return [], info |
496c1923 | 58 | |
efa97bdc | 59 | except XAttrUnavailableError as e: |
b1940459 | 60 | raise PostProcessingError(str(e)) |
efa97bdc | 61 | |
86c7fdb1 YCH |
62 | except XAttrMetadataError as e: |
63 | if e.reason == 'NO_SPACE': | |
f446cc66 | 64 | self.report_warning( |
3089bc74 S |
65 | 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. ' |
66 | + (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize()) | |
fbff30d2 | 67 | elif e.reason == 'VALUE_TOO_LONG': |
f446cc66 | 68 | self.report_warning( |
fbff30d2 | 69 | 'Unable to write extended attributes due to too long values.') |
86c7fdb1 | 70 | else: |
c827d4cf | 71 | msg = 'This filesystem doesn\'t support extended attributes. ' |
e9c0cdd3 | 72 | if compat_os_name == 'nt': |
c827d4cf YCH |
73 | msg += 'You need to use NTFS.' |
74 | else: | |
75 | msg += '(You may have to enable them in your /etc/fstab)' | |
b1940459 | 76 | raise PostProcessingError(str(e)) |
592e97e8 | 77 | return [], info |