From 101e516622d9a3dc9b88bc91ee1be65423b705e7 Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 12 May 2020 10:27:01 +0200 Subject: [PATCH 01/16] Cleanup log calls/comments --- sima/mpdclient.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index 9b8df6a..ea6132c 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -92,7 +92,7 @@ def blacklist(artist=False, album=False, track=False): if track and cls.database.get_bl_album(elem, add_not=True): # filter album as well in track mode # (artist have already been) - cls.log.debug('Blacklisted alb. "{0.album}"'.format(elem)) + cls.log.debug('Blacklisted alb. "%s"', elem) continue results.append(elem) return results @@ -251,9 +251,9 @@ class MPD(MPDClient): super().__getattr__('add')(payload.file) elif isinstance(payload, list): self.command_list_ok_begin() - for tr in payload: # TODO: use send command here + for tr in payload: self.add(tr) - results = client.command_list_end() + self.command_list_end() else: self.log.error('Cannot add %s', payload) @@ -388,7 +388,7 @@ class MPD(MPDClient): fuzz, artist) if found: if artist.aliases: - self.log.debug('Found: %s', '/'.join(list(artist.names)[:4])) + self.log.debug('Found aliases: %s', '/'.join(artist.names)) return artist return None -- 2.39.2 From 6ce8e09e316f315d437377ab0850da045cb0f041 Mon Sep 17 00:00:00 2001 From: kaliko Date: Thu, 14 May 2020 15:27:27 +0200 Subject: [PATCH 02/16] MPD client: Rewrote search_albums from scratch --- sima/lib/webserv.py | 21 +++++-------- sima/mpdclient.py | 77 ++++++++++++++++++++++++--------------------- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 9de9dae..44131d4 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -33,7 +33,7 @@ from hashlib import md5 # local import from .plugin import Plugin from .track import Track -from .meta import Artist, Album, MetaContainer +from .meta import Artist, MetaContainer from ..utils.utils import WSError, WSNotFound def cache(func): @@ -298,7 +298,7 @@ class WebService(Plugin): self.log.info(' / '.join(map(str, candidates))) return candidates - def _get_album_history(self, artist=None): + def _get_album_history(self, artist): """Retrieve album history""" duration = self.daemon_conf.getint('sima', 'history_duration') albums_list = set() @@ -311,24 +311,20 @@ class WebService(Plugin): """ self.to_add = list() nb_album_add = 0 - target_album_to_add = self.plugin_conf.getint('album_to_add') # pylint: disable=no-member + target_album_to_add = self.plugin_conf.getint('album_to_add') # pylint: disable=no-member for artist in artists: self.log.info('Looking for an album to add for "%s"...' % artist) albums = self.player.search_albums(artist) - # str conversion while Album type is not propagated - albums = [str(album) for album in albums] if not albums: continue - self.log.debug('Albums candidate: %s', ' / '.join(albums)) - # 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) + self.log.debug('Albums candidate: %s', albums) + albums_hist = self._get_album_history(artist) + albums_not_in_hist = [a for a in albums if a.name not in albums_hist] # Get to next artist if there are no unplayed albums if not albums_not_in_hist: self.log.info('No unplayed album found for "%s"' % artist) continue - album_to_queue = str() + album_to_queue = [] random.shuffle(albums_not_in_hist) for album in albums_not_in_hist: # Controls the album found is not already queued @@ -347,8 +343,7 @@ class WebService(Plugin): self.log.info('%s album candidate: %s - %s', self.ws.name, artist, album_to_queue) nb_album_add += 1 - candidates = self.player.find_tracks(Album(name=album_to_queue, - artist=artist)) + candidates = self.player.find_tracks(album) if self.plugin_conf.getboolean('shuffle_album'): random.shuffle(candidates) # this allows to select a maximum number of track from the album diff --git a/sima/mpdclient.py b/sima/mpdclient.py index ea6132c..e8f6972 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -325,7 +325,7 @@ class MPD(MPDClient): albums = self.find(filt) # Now look for album with no MusicBrainzIdentifier if not albums and album.artist.mbid and self.use_mbid: # Use album artist MBID if possible - filt = f"((MUSICBRAINZ_ARTISTID == '{album.artist.mbid}') AND (album == '{album.name_sz}'))" + filt = f"((MUSICBRAINZ_ALBUMARTISTID == '{album.artist.mbid}') AND (album == '{album.name_sz}'))" albums = self.find(filt) if not albums: # Falls back to (album)?artist/album name filt = f"((albumartist == '{album.artist!s}') AND (album == '{album.name_sz}'))" @@ -419,42 +419,49 @@ class MPD(MPDClient): @blacklist(album=True) def search_albums(self, artist): - """ - Fetch all albums for "AlbumArtist" == artist - Then look for albums for "artist" == artist and try to filters - multi-artists albums + """Find potential albums for "artist" - NB: Running "client.list('album', 'artist', name)" MPD returns any album - containing at least a track with "artist" == name - TODO: Use MusicBrainzID here cf. #30 @gitlab + * Fetch all albums for "AlbumArtist" == artist + → falls back to "Artist" == artist when no "AlbumArtist" tag is set + * Tries to filter some mutli-artists album + For instance an album by Artist_A may have a track by Artist_B. Then + looking for albums for Artist_B returns wrongly this album. """ - albums = [] - for name in artist.names_sz: - if artist.aliases: - self.log.debug('Searching album for aliase: "%s"', name) - kwalbart = {'albumartist': name, 'artist': name} - # MPD falls back to artist if albumartist is not available - for album in self.list('album', f"( albumartist == '{name}')"): - if not album: # list can return "" as an album - continue - album_trks = self.find_tracks(Album(name=album, artist=Artist(name=name))) - album_artists = [tr.albumartist for tr in album_trks if tr.albumartist] - if 'Various Artists' in [tr.albumartist for tr in album_trks]: - self.log.debug('Discarding %s ("Various Artists" set)', album) - continue - if album_artists and name not in album_artists: - self.log.debug('Discarding "%s", "%s" not set as albumartist', album, name) - continue - arts = {trk.artist for trk in album_trks} - # Avoid selecting album where artist is credited for a single - # track of the album - if len(set(arts)) < 2: # TODO: better heuristic, use a ratio instead - if album not in albums: - albums.append(Album(name=album, **kwalbart)) - elif album not in albums: - self.log.debug('"{0}" probably not an album of "{1}"'.format( - album, artist) + '({0})'.format('/'.join(arts))) - return albums + # First, look for all potential albums + self.log.debug('Searching album for "%s"', artist) + if artist.aliases: + self.log.debug('Searching album for %s aliases: "%s"', + artist, artist.aliases) + for name_sz in artist.names_sz: + raw_albums = self.list('album', f"( albumartist == '{name_sz}')") + albums = [Album(a, albumartist=artist.name, artist=artist) for a in raw_albums if a] + candidates = [] + for album in albums: + album_trks = self.find_tracks(album) + album_artists = {tr.albumartist for tr in album_trks if tr.albumartist} + if album.artist.names & album_artists: + candidates.append(album) + continue + if 'Various Artists' in album_artists: + self.log.debug('Discarding %s ("Various Artists" set)', album) + continue + if album_artists and album.artist.name not in album_artists: + self.log.debug('Discarding "%s", "%s" not set as albumartist', album, album.artist) + continue + # Attempt to detect false positive + # Avoid selecting albums where artist is credited for a single + # track of the album + album_trks = self.find(f"(album == '{album.name_sz}')") + arts = [trk.artist for trk in album_trks] # Artists in the album + # count artist occurences + ratio = arts.count(album.artist.name)/len(arts) + if ratio >= 0.8: + candidates.append(album) + else: + self.log.debug('"%s" probably not an album of "%s" (ratio=%.2f)', + album, artist, ratio) + continue + return candidates # #### / Search Methods ### # VIM MODLINE -- 2.39.2 From 54849baefbe39c4c0a09bd8ad27bde7743dac5ef Mon Sep 17 00:00:00 2001 From: kaliko Date: Thu, 14 May 2020 18:16:28 +0200 Subject: [PATCH 03/16] MPD client: Use tagtypes to control and set tags --- sima/launch.py | 7 +------ sima/mpdclient.py | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/sima/launch.py b/sima/launch.py index 1ae3c19..c472567 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -34,7 +34,6 @@ from os.path import isfile # local import from . import core, info from .lib.logger import set_logger -from .lib.meta import Meta from .lib.simadb import SimaDB from .utils.config import ConfMan from .utils.startopt import StartOpt @@ -121,11 +120,7 @@ def start(sopt, restart=False): # Loading contrib plugins load_plugins(sima, 'contrib') - logger.info('plugins loaded, prioriy: {}'.format(' > '.join(map(str, sima.plugins)))) - # Set use of MusicBrainzIdentifier - if not config.getboolean('sima', 'musicbrainzid'): - logger.info('Disabling MusicBrainzIdentifier') - Meta.use_mbid = False + logger.info('plugins loaded, prioriy: %s', ' > '.join(map(str, sima.plugins))) # Run as a daemon if config.getboolean('daemon', 'daemon'): diff --git a/sima/mpdclient.py b/sima/mpdclient.py index e8f6972..97be9da 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -26,7 +26,7 @@ from musicpd import MPDClient, MPDError # local import -from .lib.meta import Artist, Album +from .lib.meta import Meta, Artist, Album from .lib.track import Track from .lib.simastr import SimaStr from .utils.leven import levenshtein_ratio @@ -115,6 +115,10 @@ class MPD(MPDClient): """ needed_cmds = ['status', 'stats', 'add', 'find', 'search', 'currentsong', 'ping'] + needed_mbid_tags = {'Artist', 'Album', 'AlbumArtist', + 'Title', 'Track', 'Genre', + 'MUSICBRAINZ_ARTISTID', 'MUSICBRAINZ_ALBUMID', + 'MUSICBRAINZ_ALBUMARTISTID', 'MUSICBRAINZ_TRACKID'} database = None def __init__(self, daemon): @@ -176,20 +180,24 @@ class MPD(MPDClient): 'but command "%s" not available' % (host, cmd)) # Controls use of MusicBrainzIdentifier - # TODO: Use config instead of Artist object attibute? - if self.use_mbid: - tt = self.tagtypes() - if 'MUSICBRAINZ_ARTISTID' not in tt: - self.log.warning('Use of MusicBrainzIdentifier is set but MPD is ' - 'not providing related metadata') + self.tagtypes('clear') + for tag in MPD.needed_mbid_tags: + self.tagtypes('enable', tag) + if self.daemon.config.get('sima', 'musicbrainzid'): + tt = set(self.tagtypes()) + if len(MPD.needed_mbid_tags & tt) != len(MPD.needed_mbid_tags): + self.log.warning('Use of MusicBrainzIdentifier is set but MPD ' + 'is not providing related metadata') self.log.info(tt) self.log.warning('Disabling MusicBrainzIdentifier') - self.use_mbid = False + self.use_mbid = Meta.use_mbid = False else: - self.log.debug('Available metadata: %s', tt) # pylint: disable=no-member + self.log.debug('Available metadata: %s', tt) + self.use_mbid = Meta.use_mbid = True else: self.log.warning('Use of MusicBrainzIdentifier disabled!') self.log.info('Consider using MusicBrainzIdentifier for your music library') + self.use_mbid = Meta.use_mbid = False self._reset_cache() # ######### / Overriding MPDClient ######### -- 2.39.2 From 31cb581a72f067990a5dc3917542d15505f86990 Mon Sep 17 00:00:00 2001 From: kaliko Date: Thu, 14 May 2020 18:22:01 +0200 Subject: [PATCH 04/16] Cleanup code smell and update docstrings --- sima/launch.py | 4 ++-- sima/mpdclient.py | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/sima/launch.py b/sima/launch.py index c472567..c548ce9 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -111,9 +111,9 @@ def start(sopt, restart=False): core_plugins = [History, MpdOptions] config['sima']['musicbrainzid'] = 'False' for cplgn in core_plugins: - logger.debug('Register core {name} ({doc})'.format(**cplgn.info())) + logger.debug('Register core %(name)s (%(doc)s)', cplgn.info()) sima.register_core_plugin(cplgn) - logger.debug('core loaded, prioriy: {}'.format(' > '.join(map(str, sima.core_plugins)))) + logger.debug('core loaded, prioriy: %s', ' > '.join(map(str, sima.core_plugins))) # Loading internal plugins load_plugins(sima, 'internal') diff --git a/sima/mpdclient.py b/sima/mpdclient.py index 97be9da..6b461ba 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -44,7 +44,7 @@ def bl_artist(func): return func(*args, **kwargs) result = func(*args, **kwargs) if not result: - return + return None names = list() for art in result.names: if cls.database.get_bl_artist(art, add_not=True): @@ -52,27 +52,32 @@ def bl_artist(func): continue names.append(art) if not names: - return + return None resp = Artist(name=names.pop(), mbid=result.mbid) for name in names: resp.add_alias(name) return resp return wrapper + def tracks_wrapper(func): + """Convert plain track mapping as returned by MPDClient into :py:obj:Track + objects. This decorator accepts single track or list of tracks as input. + """ @wraps(func) def wrapper(*args, **kwargs): ret = func(*args, **kwargs) if isinstance(ret, dict): return Track(**ret) - elif isinstance(ret, list): - return [Track(**t) for t in ret] + return [Track(**t) for t in ret] return wrapper # / decorators + def blacklist(artist=False, album=False, track=False): - #pylint: disable=C0111,W0212 + # pylint: disable=C0111,W0212 field = (album, track) + def decorated(func): def wrapper(*args, **kwargs): if not args[0].database: @@ -110,7 +115,8 @@ class MPD(MPDClient): .. note:: - * find methods are looking for exact match of the object provided attributes in MPD music library + * find methods are looking for exact match of the object provided + attributes in MPD music library * search methods are looking for exact match + fuzzy match. """ needed_cmds = ['status', 'stats', 'add', 'find', @@ -132,11 +138,7 @@ class MPD(MPDClient): # ######### Overriding MPDClient ########### def __getattr__(self, cmd): """Wrapper around MPDClient calls for abstract overriding""" - track_wrapped = { - 'currentsong', - 'find', - 'playlistinfo', - } + track_wrapped = {'currentsong', 'find', 'playlistinfo', } if cmd in track_wrapped: return tracks_wrapper(super().__getattr__(cmd)) return super().__getattr__(cmd) @@ -170,7 +172,7 @@ class MPD(MPDClient): try: self.password(password) except (MPDError, IOError) as err: - raise PlayerError("Could not connect to '%s': %s", (host, err)) + raise PlayerError("Could not connect to '%s': %s" % (host, err)) # Controls we have sufficient rights available_cmd = self.commands() for cmd in MPD.needed_cmds: @@ -259,8 +261,7 @@ class MPD(MPDClient): super().__getattr__('add')(payload.file) elif isinstance(payload, list): self.command_list_ok_begin() - for tr in payload: - self.add(tr) + map(self.add, payload) self.command_list_end() else: self.log.error('Cannot add %s', payload) @@ -311,10 +312,11 @@ class MPD(MPDClient): """ if isinstance(what, Artist): return self._find_art(what) - elif isinstance(what, Album): + if isinstance(what, Album): return self._find_alb(what) - elif isinstance(what, str): + if isinstance(what, str): return self.find_tracks(Artist(name=what)) + raise PlayerError('Bad input argument') def _find_art(self, artist): tracks = set() @@ -347,7 +349,7 @@ class MPD(MPDClient): """ Search artists based on a fuzzy search in the media library >>> art = Artist(name='the beatles', mbid=) # mbid optional - >>> bea = player.search_artist(art)c + >>> bea = player.search_artist(art) >>> print(bea.names) >>> ['The Beatles', 'Beatles', 'the beatles'] -- 2.39.2 From 45e8a0624444617ee9e86fe4576d9bb4aedfbb8a Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 15 May 2020 17:38:57 +0200 Subject: [PATCH 05/16] MPD client: Improved album search --- sima/mpdclient.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index 6b461ba..ed2fbcd 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -331,15 +331,16 @@ class MPD(MPDClient): raise PlayerError('Album object have no artist attribute') albums = [] if self.use_mbid and album.mbid: - filt = f'(MUSICBRAINZ_ALBUMID == {album.mbid})' + filt = f"(MUSICBRAINZ_ALBUMID == '{album.mbid}')" albums = self.find(filt) # Now look for album with no MusicBrainzIdentifier if not albums and album.artist.mbid and self.use_mbid: # Use album artist MBID if possible filt = f"((MUSICBRAINZ_ALBUMARTISTID == '{album.artist.mbid}') AND (album == '{album.name_sz}'))" albums = self.find(filt) if not albums: # Falls back to (album)?artist/album name - filt = f"((albumartist == '{album.artist!s}') AND (album == '{album.name_sz}'))" - albums = self.find(filt) + for artist in album.artist.names_sz: + filt = f"((albumartist == '{artist}') AND (album == '{album.name_sz}'))" + albums.extend(self.find(filt)) return albums # #### / find_tracks ## -- 2.39.2 From cd79c4c0522230a3cdfc6bb53e78ab2bdd81e9ee Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 15 May 2020 17:42:37 +0200 Subject: [PATCH 06/16] MPD client: Improved search_artist with name auto correction. --- sima/mpdclient.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index ed2fbcd..0866e4c 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -361,9 +361,16 @@ class MPD(MPDClient): found = False if self.use_mbid and artist.mbid: # look for exact search w/ musicbrainz_artistid - found = bool(self.list('artist', f"(MUSICBRAINZ_ARTISTID == '{artist.mbid}')")) - if found: + library = self.list('artist', f"(MUSICBRAINZ_ARTISTID == '{artist.mbid}')") + if library: + found = True self.log.trace('Found mbid "%r" in library', artist) + # library could fetch several artist name for a single MUSICBRAINZ_ARTISTID + if len(library) > 1: + self.log.warning('I got "%s" searching for %r', library, artist) + elif len(library) == 1 and library[0] != artist.name: + self.log.debug('Update artist name %s->%s', artist, library[0]) + artist = Artist(name=library[0], mbid=artist.mbid) # Fetches remaining artists for potential match artists = self._cache['nombid_artists'] else: # not using MusicBrainzIDs @@ -387,6 +394,7 @@ class MPD(MPDClient): self.log.trace('no fuzzy matching for %r', artist) if found: return artist + return None # Now perform fuzzy search for fuzz in match: if fuzz in artist.names: # Already found in lower cased comparison -- 2.39.2 From 1c604b5636af7f7eae4d74d5c6de31f4f431b97b Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 15 May 2020 17:45:03 +0200 Subject: [PATCH 07/16] MPD client: Improved search_track --- sima/mpdclient.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index 0866e4c..bbc7e07 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -421,20 +421,21 @@ class MPD(MPDClient): all_artist_titles = frozenset([tr.title for tr in all_tracks if tr.title is not None]) match = get_close_matches(title, all_artist_titles, 50, 0.78) + tracks = [] if not match: return [] for mtitle in match: - leven = levenshtein_ratio(title.lower(), mtitle.lower()) + leven = levenshtein_ratio(title, mtitle) if leven == 1: - pass - elif leven >= 0.79: # PARAM + tracks.extend([t for t in all_tracks if t.title == mtitle]) + elif leven >= 0.77: self.log.debug('title: "%s" should match "%s" (lr=%1.3f)', mtitle, title, leven) + tracks.extend([t for t in all_tracks if t.title == mtitle]) else: self.log.debug('title: "%s" does not match "%s" (lr=%1.3f)', mtitle, title, leven) - return [] - return self.find('artist', artist, 'title', mtitle) + return tracks @blacklist(album=True) def search_albums(self, artist): -- 2.39.2 From 5aa6ec2f4b449797fbe8269c0d8d7da66bd2c9bc Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 15 May 2020 18:10:08 +0200 Subject: [PATCH 08/16] Get rid of inefficient log string formatting --- sima/core.py | 2 +- sima/lib/plugin.py | 3 +-- sima/lib/webserv.py | 34 ++++++++++++++--------------- sima/plugins/contrib/placeholder.py | 4 ++-- sima/plugins/core/uniq.py | 2 +- sima/plugins/internal/crop.py | 2 +- sima/plugins/internal/lastfm.py | 4 ++-- sima/plugins/internal/random.py | 4 ++-- sima/utils/config.py | 4 ++-- 9 files changed, 29 insertions(+), 30 deletions(-) diff --git a/sima/core.py b/sima/core.py index 4596115..27735bd 100644 --- a/sima/core.py +++ b/sima/core.py @@ -118,7 +118,7 @@ class Sima(Daemon): while True: tmp = sleepfor.pop(0) sleepfor.append(tmp) - self.log.info('Trying to reconnect in {:>4d} seconds'.format(tmp)) + self.log.info('Trying to reconnect in %4d seconds', tmp) time.sleep(tmp) try: self.player.connect() diff --git a/sima/lib/plugin.py b/sima/lib/plugin.py index 8057d86..f61cb72 100644 --- a/sima/lib/plugin.py +++ b/sima/lib/plugin.py @@ -65,8 +65,7 @@ class Plugin: if not self.plugin_conf: self.plugin_conf = {'priority': '80'} #if self.plugin_conf: - # self.log.debug('Got config for {0}: {1}'.format(self, - # self.plugin_conf)) + # self.log.debug('Got config for %s: ùs', self, self.plugin_conf) @property def priority(self): diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 44131d4..f72215c 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -71,7 +71,7 @@ class WebService(Plugin): self._flush_cache() wrapper = {'track': self._track, 'top': self._top, - 'album': self._album,} + 'album': self._album} self.queue_mode = wrapper.get(self.plugin_conf.get('queue_mode')) self.ws = None @@ -81,11 +81,11 @@ class WebService(Plugin): """ name = self.__class__.__name__ if isinstance(self._cache, dict): - self.log.info('{0}: Flushing cache!'.format(name)) + self.log.info('%s: Flushing cache!', name) else: - self.log.info('{0}: Initialising cache!'.format(name)) + self.log.info('%s: Initialising cache!', name) self._cache = {'asearch': dict(), - 'tsearch': dict(),} + 'tsearch': dict()} def _cleanup_cache(self): """Avoid bloated cache @@ -111,6 +111,7 @@ class WebService(Plugin): * not in history * not already in the queue * not blacklisted + Then add to candidates in self.to_add """ artist = tracks[0].artist # In random play mode use complete playlist to filter @@ -125,7 +126,7 @@ class WebService(Plugin): candidate = [] for trk in [_ for _ in not_in_hist if _ not in black_list]: # Should use albumartist heuristic as well - if self.plugin_conf.getboolean('single_album'): # pylint: disable=no-member + if self.plugin_conf.getboolean('single_album'): # pylint: disable=no-member if (trk.album == self.player.current.album or trk.album in [tr.album for tr in black_list]): self.log.debug('Found unplayed track ' + @@ -177,21 +178,21 @@ class WebService(Plugin): # initialize artists deque list to construct from DB as_art = deque() as_artists = self.ws.get_similar(artist=artist) - self.log.debug('Requesting {} for {!r}'.format(self.ws.name, artist)) + self.log.debug('Requesting %s for %r', self.ws.name, artist) try: [as_art.append(art) for art in as_artists] except WSNotFound as err: - self.log.warning('{}: {}'.format(self.ws.name, err)) + self.log.warning('%s: %s', self.ws.name, err) if artist.mbid: self.log.debug('Trying without MusicBrainzID') try: return self.ws_similar_artists(Artist(name=artist.name)) except WSNotFound as err: - self.log.debug('{}: {}'.format(self.ws.name, err)) + self.log.debug('%s: %s', self.ws.name, err) except WSError as err: - self.log.warning('{}: {}'.format(self.ws.name, err)) + self.log.warning('%s: %s', self.ws.name, err) if as_art: - self.log.debug('Fetched {} artist(s)'.format(len(as_art))) + self.log.debug('Fetched %d artist(s)', len(as_art)) return as_art def get_recursive_similar_artist(self): @@ -222,7 +223,7 @@ class WebService(Plugin): self.log.debug('EXTRA ARTS: %s', '/'.join(map(str, extra_arts))) for artist in extra_arts: self.log.debug('Looking for artist similar ' - 'to "{}" as well'.format(artist)) + 'to "%s" as well', artist) similar = self.ws_similar_artists(artist=artist) if not similar: continue @@ -292,7 +293,7 @@ class WebService(Plugin): ret = ret - MetaContainer([current.Artist]) # Move around similars items to get in unplayed|not recently played # artist first. - self.log.info('Got {} artists in library'.format(len(ret))) + self.log.info('Got %d artists in library', len(ret)) candidates = self._get_artists_list_reorg(list(ret)) if candidates: self.log.info(' / '.join(map(str, candidates))) @@ -363,8 +364,8 @@ class WebService(Plugin): nbtracks_target = self.plugin_conf.getint('track_to_add') # pylint: disable=no-member for artist in artists: if len(self.to_add) == nbtracks_target: - return True - self.log.info('Looking for a top track for {0}'.format(artist)) + return + self.log.info('Looking for a top track for %s', artist) titles = deque() try: titles = [t for t in self.ws.get_toptrack(artist)] @@ -372,9 +373,8 @@ class WebService(Plugin): self.log.warning('%s: %s', self.ws.name, err) for trk in titles: found = self.player.search_track(artist, trk.title) - random.shuffle(found) if found: - self.log.debug('%s', found[0]) + random.shuffle(found) if self.filter_track(found): break @@ -412,7 +412,7 @@ class WebService(Plugin): artists = self.get_local_similar_artists() self.find_top(artists) for track in self.to_add: - self.log.info('{1} candidates: {0!s}'.format(track, self.ws.name)) + self.log.info('%s candidates: %s', self.ws.name, track) def callback_need_track(self): self._cleanup_cache() diff --git a/sima/plugins/contrib/placeholder.py b/sima/plugins/contrib/placeholder.py index c03eb91..bd4f19c 100644 --- a/sima/plugins/contrib/placeholder.py +++ b/sima/plugins/contrib/placeholder.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2013, 2014 kaliko +# Copyright (c) 2013, 2014, 2020 kaliko # # This file is part of sima # @@ -34,7 +34,7 @@ class PlaceHolder(Plugin): def callback_player(self): #self.log.info(self.plugin_conf) - #self.log.debug('{0} contrib plugin!!!'.format(self)) + #self.log.debug('%s contrib plugin!!!', self) pass diff --git a/sima/plugins/core/uniq.py b/sima/plugins/core/uniq.py index 5a74170..292de6f 100644 --- a/sima/plugins/core/uniq.py +++ b/sima/plugins/core/uniq.py @@ -71,7 +71,7 @@ class Uniq(Plugin): self.log.warning(' '.join(channels)) def sub_chan(self): - self.log.debug('Registering as {}'.format(self.chan)) + self.log.debug('Registering as %s', self.chan) try: self.player.subscribe(self.chan) self._registred = True diff --git a/sima/plugins/internal/crop.py b/sima/plugins/internal/crop.py index 59e45c2..3f987ce 100644 --- a/sima/plugins/internal/crop.py +++ b/sima/plugins/internal/crop.py @@ -50,7 +50,7 @@ class Crop(Plugin): 'expecting an integer, not "%s"', target) else: self.target = int(target) - self.log.debug('Cropping at 15') + self.log.debug('Cropping at %s', self.target) def callback_next_song(self): if not self.target: diff --git a/sima/plugins/internal/lastfm.py b/sima/plugins/internal/lastfm.py index 613ddc1..81a2b1c 100644 --- a/sima/plugins/internal/lastfm.py +++ b/sima/plugins/internal/lastfm.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2013, 2014 kaliko +# Copyright (c) 2013, 2014, 2020 kaliko # # This file is part of sima # @@ -44,7 +44,7 @@ class Lastfm(WebService): persitent_cache = daemon.config.getboolean('lastfm', 'cache') if persitent_cache: CacheController.CACHE_ANYWAY = True - self.log.debug('Persistant cache enabled in {}'.format(join(vardir, 'http', 'LastFM'))) + self.log.debug('Persistant cache enabled in %s', join(vardir, 'http', 'LastFM')) SimaFM.cache = FileCache(join(vardir, 'http', 'LastFM')) self.ws = SimaFM() diff --git a/sima/plugins/internal/random.py b/sima/plugins/internal/random.py index 936294c..148138b 100644 --- a/sima/plugins/internal/random.py +++ b/sima/plugins/internal/random.py @@ -83,12 +83,12 @@ class Random(Plugin): for art in artists: if self.filtered_artist(art): continue - self.log.debug('Random art: {}'.format(art)) + self.log.debug('Random art: %s', art) trks = self.player.find_tracks(Artist(art)) if trks: trk = random.choice(trks) self.candidates.append(trk) - self.log.info('Random candidate ({}): {}'.format(self.mode, trk)) + self.log.info('Random candidate (%s): %s', self.mode, trk) if len(self.candidates) >= target: break return self.candidates diff --git a/sima/utils/config.py b/sima/utils/config.py index c7dbab6..2778422 100644 --- a/sima/utils/config.py +++ b/sima/utils/config.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2009-2015, 2019 kaliko +# Copyright (c) 2009-2015, 2019-2020 kaliko # Copyright (c) 2019 sacha # # This file is part of sima @@ -130,7 +130,7 @@ class ConfMan(object): # CONFIG MANAGER CLASS # Create directories data_dir = self.config['sima']['var_dir'] if not isdir(data_dir): - self.log.trace('Creating "{}"'.format(data_dir)) + self.log.trace('Creating "%s"', data_dir) makedirs(data_dir) chmod(data_dir, 0o700) -- 2.39.2 From 62332aa0d68f28de54880e96a62b51c6e7bab08a Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 09:48:11 +0200 Subject: [PATCH 09/16] MPD client: Add cache in search_track (speedup top track mode) --- sima/mpdclient.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index bbc7e07..bf3ce5d 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -213,10 +213,12 @@ class MPD(MPDClient): self.log.info('Player: Initialising cache!') self._cache = {'artists': frozenset(), 'nombid_artists': frozenset()} - self._cache['artists'] = frozenset(filter(None, self.list('artist'))) - if Artist.use_mbid: + self._cache['artist_tracks'] = {} + if self.use_mbid: artists = self.list('artist', "(MUSICBRAINZ_ARTISTID == '')") self._cache['nombid_artists'] = frozenset(filter(None, artists)) + else: + self._cache['artists'] = frozenset(filter(None, self.list('artist'))) def _skipped_track(self, previous): if (self.state == 'stop' @@ -226,8 +228,7 @@ class MPD(MPDClient): return self.current.id != previous.id # pylint: disable=no-member def monitor(self): - """OLD socket Idler - Monitor player for change + """Monitor player for change Returns a list a events among: * database player media library has changed @@ -256,7 +257,10 @@ class MPD(MPDClient): self.log.warning('pending commands: %s', self._pending) def add(self, payload): - """Overriding MPD's add method to accept Track objects""" + """Overriding MPD's add method to accept Track objects + + :param Track,list payload: Either a single :py:obj:`Track` or a list of it + """ if isinstance(payload, Track): super().__getattr__('add')(payload.file) elif isinstance(payload, list): @@ -367,9 +371,9 @@ class MPD(MPDClient): self.log.trace('Found mbid "%r" in library', artist) # library could fetch several artist name for a single MUSICBRAINZ_ARTISTID if len(library) > 1: - self.log.warning('I got "%s" searching for %r', library, artist) + self.log.debug('I got "%s" searching for %r', library, artist) elif len(library) == 1 and library[0] != artist.name: - self.log.debug('Update artist name %s->%s', artist, library[0]) + self.log.info('Update artist name %s->%s', artist, library[0]) artist = Artist(name=library[0], mbid=artist.mbid) # Fetches remaining artists for potential match artists = self._cache['nombid_artists'] @@ -379,8 +383,9 @@ class MPD(MPDClient): if not match and not found: return None if len(match) > 1: - self.log.debug('found close match for "%s": %s', artist, '/'.join(match)) - # Forst lowercased comparison + self.log.debug('found close match for "%s": %s', + artist, '/'.join(match)) + # First lowercased comparison for close_art in match: # Regular lowered string comparison if artist.name.lower() == close_art.lower(): @@ -415,8 +420,12 @@ class MPD(MPDClient): def search_track(self, artist, title): """Fuzzy search of title by an artist """ + cache = self._cache.get('artist_tracks').get(artist) # Retrieve all tracks from artist - all_tracks = self.find_tracks(artist) + all_tracks = cache or self.find_tracks(artist) + if not cache: + self._cache['artist_tracks'] = {} # clean up + self._cache.get('artist_tracks')[artist] = all_tracks # Get all titles (filter missing titles set to 'None') all_artist_titles = frozenset([tr.title for tr in all_tracks if tr.title is not None]) -- 2.39.2 From 8870413a27f675d121ed8c841e8673b597ab15c3 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 11:53:04 +0200 Subject: [PATCH 10/16] Get rid of a useless direct db call --- sima/lib/webserv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index f72215c..734a5f0 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -301,9 +301,8 @@ class WebService(Plugin): def _get_album_history(self, artist): """Retrieve album history""" - duration = self.daemon_conf.getint('sima', 'history_duration') albums_list = set() - for trk in self.sdb.get_history(artist=artist.name, duration=duration): + for trk in self.get_history(artist=artist.name): albums_list.add(trk[1]) return albums_list -- 2.39.2 From 1167ed537fc43affb7d6e068092ff920ea0d308b Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 11:55:01 +0200 Subject: [PATCH 11/16] Fixed code smell in webserv/find_top --- sima/lib/webserv.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 734a5f0..078f61d 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -370,6 +370,7 @@ class WebService(Plugin): titles = [t for t in self.ws.get_toptrack(artist)] except WSError as err: self.log.warning('%s: %s', self.ws.name, err) + continue for trk in titles: found = self.player.search_track(artist, trk.title) if found: -- 2.39.2 From c2772b384c9a73397733a81bce3d7f21c52370a7 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 13:40:33 +0200 Subject: [PATCH 12/16] MPD client: Fixed bad changes introduce in 62332aa --- sima/mpdclient.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sima/mpdclient.py b/sima/mpdclient.py index bf3ce5d..abc8282 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -206,19 +206,22 @@ class MPD(MPDClient): def _reset_cache(self): """ Both flushes and instantiates _cache + + * artists: all artists + * nombid_artists: artists with no mbid (set only when self.use_mbid is True) + * artist_tracks: caching last artist tracks, used in search_track """ if isinstance(self._cache, dict): self.log.info('Player: Flushing cache!') else: self.log.info('Player: Initialising cache!') self._cache = {'artists': frozenset(), - 'nombid_artists': frozenset()} - self._cache['artist_tracks'] = {} + 'nombid_artists': frozenset(), + 'artist_tracks': {}} + self._cache['artists'] = frozenset(filter(None, self.list('artist'))) if self.use_mbid: artists = self.list('artist', "(MUSICBRAINZ_ARTISTID == '')") self._cache['nombid_artists'] = frozenset(filter(None, artists)) - else: - self._cache['artists'] = frozenset(filter(None, self.list('artist'))) def _skipped_track(self, previous): if (self.state == 'stop' -- 2.39.2 From 00efe9ba9a9b93a0e1da0f9ee6e9ed8db4b05ccc Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 14:52:24 +0200 Subject: [PATCH 13/16] Making a dev release --- doc/Changelog | 4 ++-- sima/info.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index b2774c1..33913b3 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,10 +1,10 @@ -MPD_sima v0.16.0 UNRELEASED +MPD_sima v0.16.0.dev0 * Major MPD client refactoring * Refactored random plugin * Fixed bug in MPD client reconnection --- kaliko jack + -- kaliko Sat, 16 May 2020 14:49:04 +0200 MPD_sima v0.15.3 diff --git a/sima/info.py b/sima/info.py index 4c29c02..fe1fd6a 100644 --- a/sima/info.py +++ b/sima/info.py @@ -12,7 +12,7 @@ short. """ -__version__ = '0.16.0' +__version__ = '0.16.0.dev0' __author__ = 'kaliko' __email__ = 'kaliko@azylum.org' __url__ = 'git://git.kaliko.me/sima.git' -- 2.39.2 From c65a2e522cae11be9de85b46c6f05ea57b4ea743 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 16 May 2020 15:03:57 +0200 Subject: [PATCH 14/16] Need at least python 3.6 (use of f-strings) --- setup.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 5287da0..8e6246c 100755 --- a/setup.py +++ b/setup.py @@ -26,12 +26,9 @@ classifiers = [ "Topic :: Multimedia :: Sound/Audio", "Topic :: Utilities", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.2", - "Programming Language :: Python :: 3.3", - "Programming Language :: Python :: 3.4", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", ] setup(name='MPD_sima', -- 2.39.2 From 65c8109daf4b649eb1573cfcff18e1f0687ae22d Mon Sep 17 00:00:00 2001 From: kaliko Date: Wed, 20 May 2020 16:10:21 +0200 Subject: [PATCH 15/16] Fixed regression introduced with 8870413 --- sima/lib/webserv.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 078f61d..1f75c20 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -303,7 +303,9 @@ class WebService(Plugin): """Retrieve album history""" albums_list = set() for trk in self.get_history(artist=artist.name): - albums_list.add(trk[1]) + if not trk.album: + continue + albums_list.add(trk.album) return albums_list def find_album(self, artists): -- 2.39.2 From a5aa8c5e03bdb3f82620cc28d4364fb6f4b1db9a Mon Sep 17 00:00:00 2001 From: kaliko Date: Thu, 21 May 2020 18:20:54 +0200 Subject: [PATCH 16/16] Bumped version --- sima/info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sima/info.py b/sima/info.py index fe1fd6a..1651c69 100644 --- a/sima/info.py +++ b/sima/info.py @@ -12,7 +12,7 @@ short. """ -__version__ = '0.16.0.dev0' +__version__ = '0.16.0.dev1' __author__ = 'kaliko' __email__ = 'kaliko@azylum.org' __url__ = 'git://git.kaliko.me/sima.git' -- 2.39.2