1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
52 """ we set some special things here """
53 self._cached = {}
54 self.renew_cache()
55
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
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
89
91 if metadata.get('cover', None) != None:
92 if metadata.get('default_image', '') != None:
93 return False
94
95
96
97 if metadata.get('album', None) != None and \
98 metadata.get('artist', None)!= None:
99 return True
100
101 return False
102
106
107
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
124 if metadata.get('cover', None) == None:
125 metadata['cover'] = cover_path
126
127
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
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