1
import hashlib, re, urllib
7
event.add_callback(_enable, "exaile_loaded")
9
_enable(None, exaile, None)
11
def _enable(eventname, exaile, nothing):
12
exaile.covers.add_search_method(LastFMCoverSearch())
15
exaile.covers.remove_search_method_by_name('lastfm')
17
class LastFMCoverSearch(CoverSearchMethod):
19
Searches Last.fm for covers
22
type = 'remote' # fetches remotely as opposed to locally
23
regex = re.compile(r'<coverart>.*?medium>([^<]*)</medium>.*?</coverart>',
24
re.IGNORECASE|re.DOTALL)
25
url = "http://ws.audioscrobbler.com/1.0/album/%(artist)s/%(album)s/info.xml"
27
# List of SHA1 (hex) signatures of covers to be ignored. It can be
28
# replaced by a [frozen]set if the number of elements becomes large.
29
black_list = ["57b2c37343f711c94e83a37bd91bc4d18d2ed9d5"]
31
def find_covers(self, track, limit=-1):
33
Searches last.fm for album covers
35
cache_dir = self.manager.cache_dir
36
if not track['artist'] or not track['album']:
37
raise NoCoverFoundException()
38
(artist, album) = track['artist'][0], track['album'][0]
40
data = urllib.urlopen(self.url %
42
'album': urllib.quote_plus(album.encode("utf-8")),
43
'artist': urllib.quote_plus(artist.encode("utf-8"))
46
m = self.regex.search(data)
48
raise NoCoverFoundException()
51
if image.lower().endswith('.gif'):
52
raise NoCoverFoundException()
54
h = urllib.urlopen(image)
58
if hashlib.sha1(data).hexdigest() in self.black_list:
59
raise NoCoverFoundException()
61
covername = os.path.join(cache_dir, hashlib.md5(m.group(1)).hexdigest())
63
h = open(covername, 'w')