]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/xattrpp.py
   5 from .common 
import PostProcessor
 
   9     subprocess_check_output
 
  13 class XAttrMetadataPP(PostProcessor
): 
  16     # More info about extended attributes for media: 
  17     #   http://freedesktop.org/wiki/CommonExtendedAttributes/ 
  18     #   http://www.freedesktop.org/wiki/PhreedomDraft/ 
  19     #   http://dublincore.org/documents/usageguide/elements.shtml 
  22     #  * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated) 
  23     #  * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution' 
  27         """ Set extended attributes on downloaded file (if xattr support is found). """ 
  29         # This mess below finds the best xattr tool for the job and creates a 
  30         # "write_xattr" function. 
  32             # try the pyxattr module... 
  35             def write_xattr(path
, key
, value
): 
  36                 return xattr
.setxattr(path
, key
, value
) 
  40                 # Write xattrs to NTFS Alternate Data Streams: 
  41                 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29 
  42                 def write_xattr(path
, key
, value
): 
  44                     assert os
.path
.exists(path
) 
  46                     ads_fn 
= path 
+ ":" + key
 
  47                     with open(ads_fn
, "wb") as f
: 
  50                 user_has_setfattr 
= check_executable("setfattr", ['--version']) 
  51                 user_has_xattr 
= check_executable("xattr", ['-h']) 
  53                 if user_has_setfattr 
or user_has_xattr
: 
  55                     def write_xattr(path
, key
, value
): 
  57                             cmd 
= ['setfattr', '-n', key
, '-v', value
, path
] 
  59                             cmd 
= ['xattr', '-w', key
, value
, path
] 
  61                         subprocess_check_output(cmd
) 
  64                     # On Unix, and can't find pyxattr, setfattr, or xattr. 
  65                     if sys
.platform
.startswith('linux'): 
  66                         self
._downloader
.report_error( 
  67                             "Couldn't find a tool to set the xattrs. " 
  68                             "Install either the python 'pyxattr' or 'xattr' " 
  69                             "modules, or the GNU 'attr' package " 
  70                             "(which contains the 'setfattr' tool).") 
  72                         self
._downloader
.report_error( 
  73                             "Couldn't find a tool to set the xattrs. " 
  74                             "Install either the python 'xattr' module, " 
  75                             "or the 'xattr' binary.") 
  77         # Write the metadata to the file's xattrs 
  78         self
._downloader
.to_screen('[metadata] Writing metadata to file\'s xattrs') 
  80         filename 
= info
['filepath'] 
  84                 'user.xdg.referrer.url': 'webpage_url', 
  85                 # 'user.xdg.comment':            'description', 
  86                 'user.dublincore.title': 'title', 
  87                 'user.dublincore.date': 'upload_date', 
  88                 'user.dublincore.description': 'description', 
  89                 'user.dublincore.contributor': 'uploader', 
  90                 'user.dublincore.format': 'format', 
  93             for xattrname
, infoname 
in xattr_mapping
.items(): 
  95                 value 
= info
.get(infoname
) 
  98                     if infoname 
== "upload_date": 
  99                         value 
= hyphenate_date(value
) 
 101                     byte_value 
= value
.encode('utf-8') 
 102                     write_xattr(filename
, xattrname
, byte_value
) 
 106         except (subprocess
.CalledProcessError
, OSError): 
 107             self
._downloader
.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")