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