~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to plugins/lastfmcovers/__init__.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import hashlib, re, urllib
2
 
from xl.cover import *
3
 
from xl import event
4
 
 
5
 
def enable(exaile):
6
 
    if exaile.loading:
7
 
        event.add_callback(_enable, "exaile_loaded")
8
 
    else:
9
 
        _enable(None, exaile, None)
10
 
 
11
 
def _enable(eventname, exaile, nothing):
12
 
    exaile.covers.add_search_method(LastFMCoverSearch())
13
 
 
14
 
def disable(exaile):
15
 
    exaile.covers.remove_search_method_by_name('lastfm')
16
 
 
17
 
class LastFMCoverSearch(CoverSearchMethod):
18
 
    """
19
 
        Searches Last.fm for covers
20
 
    """
21
 
    name = 'lastfm'
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"
26
 
 
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"]
30
 
 
31
 
    def find_covers(self, track, limit=-1):
32
 
        """
33
 
            Searches last.fm for album covers
34
 
        """
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]
39
 
 
40
 
        data = urllib.urlopen(self.url % 
41
 
        {
42
 
            'album': urllib.quote_plus(album.encode("utf-8")),
43
 
            'artist': urllib.quote_plus(artist.encode("utf-8"))
44
 
        }).read()
45
 
 
46
 
        m = self.regex.search(data)
47
 
        if not m:
48
 
            raise NoCoverFoundException()
49
 
 
50
 
        image = m.group(1)
51
 
        if image.lower().endswith('.gif'):
52
 
            raise NoCoverFoundException()
53
 
 
54
 
        h = urllib.urlopen(image)
55
 
        data = h.read()
56
 
        h.close()
57
 
 
58
 
        if hashlib.sha1(data).hexdigest() in self.black_list:
59
 
            raise NoCoverFoundException()
60
 
 
61
 
        covername = os.path.join(cache_dir, hashlib.md5(m.group(1)).hexdigest())
62
 
        covername += ".jpg"
63
 
        h = open(covername, 'w')
64
 
        h.write(data)
65
 
        h.close()
66
 
 
67
 
        return [covername]