]> kaliko git repositories - mpd-sima.git/commitdiff
Move album blacklist filter in player client
authorkaliko <efrim@azylum.org>
Sun, 15 Dec 2013 15:03:31 +0000 (16:03 +0100)
committerkaliko <efrim@azylum.org>
Sun, 15 Dec 2013 15:03:31 +0000 (16:03 +0100)
sima/client.py
sima/lib/album.py [new file with mode: 0644]
sima/plugins/internal/lastfm.py

index 8122fdcd0f49a8a258dc096ef3c3b74a902e8cca..96ca7250092072ddb9f51a4140ff505db6d270c1 100644 (file)
@@ -21,6 +21,7 @@ except ImportError as err:
 # local import
 from .lib.player import Player
 from .lib.track import Track
+from .lib.album import Album
 from .lib.simastr import SimaStr
 
 
@@ -213,24 +214,26 @@ class PlayerClient(Player):
             return alb_art_search
         return self.find('artist', artist, 'album', album)
 
-    #@blacklist(album=True)
+    @blacklist(album=True)
     def find_albums(self, artist):
         """
         Fetch all albums for "AlbumArtist"  == artist
         Filter albums returned for "artist" == artist since MPD returns any
                album containing at least a single track for artist
         """
-        albums = set()
-        albums.update(self.list('album', 'albumartist', artist))
+        albums = []
+        kwalbart = {'albumartist':artist, 'artist':artist}
+        for album in self.list('album', 'albumartist', artist):
+            if album not in albums:
+                albums.append(Album(name=album, **kwalbart))
         for album in self.list('album', 'artist', artist):
             arts = set([trk.artist for trk in self.find('album', album)])
-            if len(arts) < 2:
-                albums.add(album)
-            else:
+            if len(arts) < 2:  # TODO: better heuristic, use a ratio instead
+                if album not in albums:
+                    albums.append(Album(name=album, albumartist=artist))
+            elif album not in albums:
                 self.log.debug('"{0}" probably not an album of "{1}"'.format(
-                             album, artist) + '({0})'.format('/'.join(arts)))
-        if albums:
-            self.log.debug('Albums candidate: {0}'.format('/'.join(albums)))
+                               album, artist) + '({0})'.format('/'.join(arts)))
         return albums
 
     def monitor(self):
diff --git a/sima/lib/album.py b/sima/lib/album.py
new file mode 100644 (file)
index 0000000..8695aa4
--- /dev/null
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+
+from .track import Track
+
+class MetaException(Exception):
+    pass
+
+class Meta:
+
+    def __init__(self, **kwargs):
+        self.name = None
+        self.mbid = None
+        if 'name' not in kwargs:
+            raise MetaException('need at least a "name" argument')
+        self.__dict__.update(kwargs)
+
+    def __repr__(self):
+        fmt = '{0}(name="{1.name}", mbid="{1.mbid}")'
+        return fmt.format(self.__class__.__name__, self)
+
+    def __str__(self):
+        return str(self.name)
+
+    def __hash__(self):
+        if self.mbid is not None:
+            return hash(self.mbid)
+        else:
+            return id(self)
+
+
+class Album(Meta):
+    __hash__ = Meta.__hash__
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+    def __eq__(self, other):
+        """
+        Perform mbid equality test if present,
+        else fallback on self.name equality
+        """
+        if hasattr(other, 'mbid'):
+            if other.mbid and self.mbid:
+                return self.mbid == other.mbid
+        return str(self) == str(other)
+
+    @property
+    def album(self):
+        return self.name
+
+# vim: ai ts=4 sw=4 sts=4 expandtab
index c0904b0ffb95f0cb5adbac4bb5f93b55226f89a8..8dccac980975245a0da2b600c77fd4736c9a2f02 100644 (file)
@@ -265,7 +265,13 @@ class Lastfm(Plugin):
         for artist in artists:
             self.log.info('Looking for an album to add for "%s"...' % artist)
             albums = self.player.find_albums(artist)
+            # str conversion while Album type is not propagated
+            albums = [ str(album) for album in albums]
+            if albums:
+                self.log.debug('Albums candidate: {0:s}'.format(' / '.join(albums)))
+            else: continue
             # albums yet in history for this artist
+            albums = set(albums)
             albums_yet_in_hist = albums & self._get_album_history(artist=artist)
             albums_not_in_hist = list(albums - albums_yet_in_hist)
             # Get to next artist if there are no unplayed albums
@@ -276,10 +282,6 @@ class Lastfm(Plugin):
             random.shuffle(albums_not_in_hist)
             for album in albums_not_in_hist:
                 tracks = self.player.find_album(artist, album)
-                if tracks and self.sdb.get_bl_album(tracks[0], add_not=True):
-                    self.log.info('Blacklisted album: "%s"' % album)
-                    self.log.debug('using track: "%s"' % tracks[0])
-                    continue
                 # Look if one track of the album is already queued
                 # Good heuristic, at least enough to guess if the whole album is
                 # already queued.