Package elisa :: Package plugins :: Package bad :: Package album_art_plugin :: Module coverindir_metadata
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.album_art_plugin.coverindir_metadata

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  This metadata provider, looks if there is might a Cover (file) in the 
 19  directory of the file. 
 20  """ 
 21   
 22   
 23  __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>' 
 24   
 25  import os, glob 
 26   
 27  from twisted.internet import threads 
 28   
 29  from elisa.core import common 
 30  from elisa.base_components.metadata_provider import MetadataProvider 
 31  from elisa.core.media_uri import MediaUri 
 32   
 33   
34 -class CoverInDir(MetadataProvider):
35 """ 36 This CoverInDir Metadata Provider just looks if there are might covers in 37 the directory of the URI. If there are more than one found, it uses the 38 best suited one. 39 40 @ivar list_of_extentions: a list of extentions our cover could have. 41 @type list_of_extentions: list 42 43 @ivar list_of_names: a list of good names for our cover sorted by 44 priority. if there is none found, which is 45 exactly spelled this way, we'll look for one, 46 which contains any of this names... 47 48 @type list_of_names: list 49 """ 50 51 # lowercase only 52 list_of_extentions = ['png', 'jpg', 'xpm', 'jpeg'] 53 54 # lowercase only 55 list_of_names = ['cover', 'front', 'cd', 'dvd'] 56 # TODO: add some more 57 # TODO: Should we use multilingual ones, too? 58 59
60 - def get_rank(self):
61 ## We'll look here first 62 return 120
63
64 - def able_to_handle(self, metadata):
65 able = False 66 content_type = metadata.get('content-type') 67 file_type = metadata.get('file_type') 68 69 if metadata.has_key('uri'): 70 uri = metadata['uri'] 71 cover = metadata.get('cover') 72 default_image = metadata.get('default_image') 73 self.debug("file_type=%r, content_type=%r, uri.scheme=%r", 74 file_type, content_type, uri.scheme) 75 if not cover and not default_image: 76 able = ((file_type == 'audio') or (content_type in ('artist', 77 'album', 78 'audio'))) \ 79 and uri.scheme in ('file', 'elisa') 80 81 return able
82
83 - def get_metadata(self, metadata, low_priority=False):
84 d = threads.deferToThread(self._search_for_cover, metadata) 85 return d
86
87 - def _find_sub_string(self, data, match_list):
88 found = False 89 #-1 not in [ for item in match_list ] 90 for item in match_list: 91 if data.find(item) != -1: 92 found = True 93 break 94 return found
95
96 - def _search_for_cover(self, metadata):
97 uri = metadata['uri'] 98 if uri.scheme == 'file': 99 result = self._files_scan(metadata) 100 else: 101 result = self._elisa_db_scan(metadata) 102 return result
103
104 - def _files_scan(self, metadata):
105 uri = metadata['uri'] 106 self.debug("Scanning local files of %r", uri) 107 extensions = self.list_of_extentions + \ 108 [ e.upper() for e in self.list_of_extentions ] 109 parent = uri.parent 110 directory = parent.path 111 112 if os.path.isdir(directory): 113 file_list = [] 114 for onefile in os.listdir(directory): 115 child = parent.join(onefile) 116 117 # inspect only directory we're looking metadata for 118 if os.path.isdir(child.path) and child == uri: 119 for extension in extensions: 120 files = glob.glob('%s/*.%s' % (child.path, 121 extension)) 122 for filename in files: 123 name, ext = os.path.splitext(filename) 124 name = os.path.basename(name) 125 if self._find_sub_string(name.lower(), 126 self.list_of_names): 127 child_uri = MediaUri('file://%s' % filename) 128 file_list.append(child_uri) 129 130 else: 131 name, ext = os.path.splitext(onefile) 132 if ext == '': 133 continue 134 135 if ext[1:].lower() in self.list_of_extentions and \ 136 self._find_sub_string(name.lower(), 137 self.list_of_names): 138 file_list.append(child) 139 140 # No suited files found. Return 141 if len(file_list) == 0: 142 return metadata 143 144 # Only one is found: use it! 145 if len(file_list) > 0: 146 return self._set_cover(metadata,file_list[0])
147
148 - def _elisa_db_scan(self, metadata):
149 uri = metadata['uri'] 150 self.debug("Retrieving cover of %r from db", uri) 151 152 def got_children(children): 153 if len(children) > 0: 154 child_metadata = children[0][1] 155 default_image = child_metadata.get('default_image') 156 if default_image: 157 return self._set_cover(metadata, default_image)
158 159 media_manager = common.application.media_manager 160 dfr = media_manager.get_direct_children(uri, []) 161 dfr.addCallback(got_children) 162 return metadata
163
164 - def _set_cover(self, metadata, cover_path):
165 if metadata.get('cover') == None: 166 metadata['cover'] = cover_path 167 168 # Set globally image if not set yet: 169 if metadata.get('default_image') == None: 170 self.debug("Setting default_image to %r", cover_path) 171 metadata['default_image'] = cover_path 172 173 return metadata
174 175 176 177 if __name__ == "__main__": 178 179 from twisted.internet import reactor 180 181 c = CoverInDir() 182 uri = MediaUri("file:///tmp/covers/momo.mp3") 183 184 print "rank:",c.get_rank() 185 print "Should be able to handle:", c.able_to_handle({'uri':uri}) 186 print "Should not be able to handle:", c.able_to_handle({'uri' : uri, 187 'cover': 'blalbal'}) 188
189 - def start():
190 191 def print_it(metadata): 192 print "metadata is", metadata
193 194 195 df = c.get_metadata({'uri' : uri}) 196 df.addCallback(print_it) 197 198 199 reactor.callWhenRunning(start) 200 201 reactor.run() 202