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