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

Source Code for Module elisa.plugins.bad.album_art_plugin.covercache_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 of of the 
 19  cache files 
 20  """ 
 21   
 22   
 23  __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>' 
 24   
 25   
 26  from elisa.base_components.metadata_provider import MetadataProvider 
 27   
 28  import os 
 29   
 30  from twisted.internet import threads 
 31   
 32   
33 -class CoverCache(MetadataProvider):
34 """ 35 This CoverCache Metadata Provider just looks if there is a cover in 36 its cache which could fit to the used artist/album. 37 38 @ivar list_of_extentions: a list of extentions our cover could have 39 @type list_of_extentions: list 40 """ 41 42 # lowercase only 43 list_of_extentions = ['png', 'jpg', 'xpm', 'jpeg'] 44 45 config_doc = {'caches': 'the pathes to the caches of the covers'} 46 47 default_config = {'caches': []} 48 49 50
51 - def initialize(self):
52 """ we set some special things here """ 53 self._cached = {} 54 self.renew_cache()
55
56 - def renew_cache(self):
57 self._cached = {} 58 for cache in self.config.get("caches", []): 59 if not os.path.isdir(cache): 60 self.warning("%s is not a directory" % cache) 61 continue 62 for eachfile in os.listdir(cache): 63 if os.path.isdir(eachfile): 64 self.warning("Skipping %s: we don't look rekursiv!" % 65 eachfile) 66 continue 67 68 if eachfile.find('.') == -1: 69 # we don't support files without extension 70 continue 71 72 name, ext = eachfile.rsplit('.', 1) 73 74 if ext.lower() in self.list_of_extentions: 75 good_name = name.lower().rstrip().lstrip() 76 filename = "%s/%s" % (cache, eachfile) 77 78 if self._cached.has_key(good_name): 79 self.warning("Skipping %s: already an entry for %s" % 80 (filename, good_name)) 81 continue 82 83 self._cached[good_name] = filename
84 85
86 - def get_rank(self):
87 # metadata manager will look here first 88 return 180
89
90 - def able_to_handle(self, metadata):
91 if metadata.get('cover', None) != None: 92 if metadata.get('default_image', '') != None: 93 return False 94 95 # at least artist or album is needed 96 97 if metadata.get('album', None) != None and \ 98 metadata.get('artist', None)!= None: 99 return True 100 101 return False
102
103 - def get_metadata(self, metadata, low_priority=False):
104 d = threads.deferToThread(self._search_for_cover, metadata) 105 return d
106 107
108 - def _search_for_cover(self, metadata):
109 artist = metadata.get('artist', '').lower().strip() 110 album = metadata.get("album", '').lower().strip() 111 112 search_list = ["%s-%s" % (artist, album), 113 "%s - %s" % (artist, album), 114 album] 115 116 for search in search_list: 117 if self._cached.get(search, None) != None: 118 return self._set_cover(metadata, self._cached[search]) 119 120 return metadata
121 122
123 - def _set_cover(self, metadata, cover_path):
124 if metadata.get('cover', None) == None: 125 metadata['cover'] = cover_path 126 127 # set image globally if not yet set 128 if metadata.get('default_image', '') == None: 129 metadata['default_image'] = cover_path 130 131 return metadata
132 133 134 135 if __name__ == "__main__": 136 137 from twisted.internet import reactor 138 139 c = CoverCache() 140 c.config = {'caches': ['/tmp/covers', '/tmp/amazon']} 141 c.initialize() 142 143 print "rank:",c.get_rank() 144 print "Should be able to handle:", c.able_to_handle({'artist' : 145 'gorillaz', 'album' : 'gorillaz'}) 146 print "Should not be able to handle:", c.able_to_handle({'uri' : ''}) 147
148 - def start():
149 150 def print_it(metadata): 151 print "metadata is", metadata
152 153 154 df = c.get_metadata({'artist': 'gorillaz', 'album' : 'gorillaz'}) 155 df.addCallback(print_it) 156 157 158 reactor.callWhenRunning(start) 159 160 reactor.run() 161