]>
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: |
f446cc66 | 60 | self.report_error(str(e)) |
efa97bdc YCH |
61 | return [], info |
62 | ||
86c7fdb1 YCH |
63 | except XAttrMetadataError as e: |
64 | if e.reason == 'NO_SPACE': | |
f446cc66 | 65 | self.report_warning( |
3089bc74 S |
66 | 'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. ' |
67 | + (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize()) | |
fbff30d2 | 68 | elif e.reason == 'VALUE_TOO_LONG': |
f446cc66 | 69 | self.report_warning( |
fbff30d2 | 70 | 'Unable to write extended attributes due to too long values.') |
86c7fdb1 | 71 | else: |
c827d4cf | 72 | msg = 'This filesystem doesn\'t support extended attributes. ' |
e9c0cdd3 | 73 | if compat_os_name == 'nt': |
c827d4cf YCH |
74 | msg += 'You need to use NTFS.' |
75 | else: | |
76 | msg += '(You may have to enable them in your /etc/fstab)' | |
f446cc66 | 77 | self.report_error(msg) |
592e97e8 | 78 | return [], info |