From 808c3bab5145b16b314ab330a054284bc0811623 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sun, 15 Dec 2013 16:03:31 +0100 Subject: [PATCH] Move album blacklist filter in player client --- sima/client.py | 21 ++++++++------ sima/lib/album.py | 51 +++++++++++++++++++++++++++++++++ sima/plugins/internal/lastfm.py | 10 ++++--- 3 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 sima/lib/album.py diff --git a/sima/client.py b/sima/client.py index 8122fdc..96ca725 100644 --- a/sima/client.py +++ b/sima/client.py @@ -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 index 0000000..8695aa4 --- /dev/null +++ b/sima/lib/album.py @@ -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 diff --git a/sima/plugins/internal/lastfm.py b/sima/plugins/internal/lastfm.py index c0904b0..8dccac9 100644 --- a/sima/plugins/internal/lastfm.py +++ b/sima/plugins/internal/lastfm.py @@ -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. -- 2.39.5