]>
jfr.im git - yt-dlp.git/blob - youtube_dl/postprocessor/xattrpp.py
1 from __future__
import unicode_literals
7 from .common
import PostProcessor
9 subprocess_check_output
17 class XAttrMetadataPP(PostProcessor
):
20 # More info about extended attributes for media:
21 # http://freedesktop.org/wiki/CommonExtendedAttributes/
22 # http://www.freedesktop.org/wiki/PhreedomDraft/
23 # http://dublincore.org/documents/usageguide/elements.shtml
26 # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
27 # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
31 """ Set extended attributes on downloaded file (if xattr support is found). """
33 # This mess below finds the best xattr tool for the job and creates a
34 # "write_xattr" function.
36 # try the pyxattr module...
39 def write_xattr(path
, key
, value
):
40 return xattr
.setxattr(path
, key
, value
)
44 # Write xattrs to NTFS Alternate Data Streams:
45 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
46 def write_xattr(path
, key
, value
):
48 assert os
.path
.exists(path
)
50 ads_fn
= path
+ ":" + key
51 with open(ads_fn
, "wb") as f
:
54 user_has_setfattr
= check_executable("setfattr", ['--version'])
55 user_has_xattr
= check_executable("xattr", ['-h'])
57 if user_has_setfattr
or user_has_xattr
:
59 def write_xattr(path
, key
, value
):
61 cmd
= ['setfattr', '-n', key
, '-v', value
, path
]
63 cmd
= ['xattr', '-w', key
, value
, path
]
65 subprocess_check_output(cmd
)
68 # On Unix, and can't find pyxattr, setfattr, or xattr.
69 if sys
.platform
.startswith('linux'):
70 self
._downloader
.report_error(
71 "Couldn't find a tool to set the xattrs. "
72 "Install either the python 'pyxattr' or 'xattr' "
73 "modules, or the GNU 'attr' package "
74 "(which contains the 'setfattr' tool).")
76 self
._downloader
.report_error(
77 "Couldn't find a tool to set the xattrs. "
78 "Install either the python 'xattr' module, "
79 "or the 'xattr' binary.")
81 # Write the metadata to the file's xattrs
82 self
._downloader
.to_screen('[metadata] Writing metadata to file\'s xattrs')
84 filename
= info
['filepath']
88 'user.xdg.referrer.url': 'webpage_url',
89 # 'user.xdg.comment': 'description',
90 'user.dublincore.title': 'title',
91 'user.dublincore.date': 'upload_date',
92 'user.dublincore.description': 'description',
93 'user.dublincore.contributor': 'uploader',
94 'user.dublincore.format': 'format',
97 for xattrname
, infoname
in xattr_mapping
.items():
99 value
= info
.get(infoname
)
102 if infoname
== "upload_date":
103 value
= hyphenate_date(value
)
105 byte_value
= value
.encode('utf-8')
106 write_xattr(filename
, xattrname
, byte_value
)
110 except (subprocess
.CalledProcessError
, OSError):
111 self
._downloader
.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")