X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flib%2Fplugin.py;h=3839f8754505591550f632cee6a31ab27db1b59d;hb=35012360f9d528bca294f6bbe2ca3ad843d09630;hp=5f709422410754a5be0a3d0dcfbcd5142bd92f22;hpb=6d8430680bdea646cd17f27e3ec58c5f9ee1d629;p=mpd-sima.git diff --git a/sima/lib/plugin.py b/sima/lib/plugin.py index 5f70942..3839f87 100644 --- a/sima/lib/plugin.py +++ b/sima/lib/plugin.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2013, 2014 Jack Kaliko +# Copyright (c) 2013-2015, 2020 kaliko # # This file is part of sima # @@ -21,13 +21,19 @@ Plugin object to derive from """ +import random + +from .track import Track +from .meta import Album, Artist + + class Plugin: """ First non-empty line of the docstring is used as description Rest of the docstring at your convenience. - The plugin Name MUST be the same as the module (file name), case - insensitive: for instance plugin.py → Plugin + The lowercased plugin Name MUST be the same as the module (file name), + for instance Plugin → plugin.py It eases plugins discovery and simplifies the code to handle them, IMHO, it's a fair trade-off. """ @@ -40,8 +46,7 @@ class Plugin: if cls.__doc__: doc = cls.__doc__.strip(' \n').splitlines()[0] return {'name': cls.__name__, - 'doc': doc, - } + 'doc': doc,} def __init__(self, daemon): self.log = daemon.log @@ -57,12 +62,26 @@ class Plugin: """Get plugin's specific configuration from global applications's config """ conf = self.__daemon.config - for sec in conf.sections(): + for sec in conf.sections(): # Discovering plugin conf if sec == self.__class__.__name__.lower(): self.plugin_conf = conf[sec] + if 'priority' not in self.plugin_conf: + self.plugin_conf['priority'] = '80' + 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): + return self.plugin_conf.get('priority') + + def start(self): + """ + Called when the daemon().run() is called and + right after the player has connected successfully. + """ + pass def callback_player(self): """ @@ -79,24 +98,26 @@ class Plugin: def callback_playlist(self): """ Called on playlist changes - Not returning data """ pass def callback_next_song(self): - """Not returning data, + """ Could be use to scrobble, maintain an history… + Not returning data, """ pass def callback_need_track(self): - """Returns a list of Track objects to add + """ + Returns a list of Track objects to add """ pass def callback_need_track_fb(self): - """Called when callback_next_song failled to find tracks to queue + """ + Called when callback_need_track failled to find tracks to queue Returns a list of Track objects to add """ pass @@ -106,5 +127,128 @@ class Plugin: pass +class AdvancedLookUp: + """Object to derive from for plugins + Exposes advanced music library look up with use of play history + """ + + def __init__(self, daemon): + self.log = daemon.log + self.daemon = daemon + self.player = daemon.player + + # Query History + def get_history(self, artist=False): + """Constructs list of already played artists. + """ + duration = self.daemon.config.getint('sima', 'history_duration') + name = None + if artist: + name = artist.name + from_db = self.daemon.sdb.get_history(duration=duration, artist=name) + hist = [Track(artist=tr[0], album=tr[1], title=tr[2], + file=tr[3]) for tr in from_db] + return hist + + def get_album_history(self, artist): + """Retrieve album history""" + hist = [] + tracks_from_db = self.get_history(artist=artist) + for trk in tracks_from_db: + if trk.album and trk.album in hist: + continue + hist.append(Album(name=trk.album, artist=Artist(trk.artist))) + return hist + + def get_reorg_artists_list(self, alist): + """ + Move around items in artists_list in order to play first not recently + played artists + + :param list(str) alist: + """ + hist = list() + duration = self.daemon.config.getint('sima', 'history_duration') + for art in self.daemon.sdb.get_artists_history(alist, duration=duration): + if art not in hist: + hist.insert(0, art) + reorg = [art for art in alist if art not in hist] + reorg.extend(hist) + return reorg + # /Query History + + # Find not recently played/unplayed + def album_candidate(self, artist, unplayed=True): + """ + :param Artist artist: Artist to fetch an album for + :param bool unplayed: Fetch only unplayed album + """ + self.log.info('Searching an album for "%s"...' % artist) + albums = self.player.search_albums(artist) + if not albums: + return [] + self.log.debug('Albums candidate: %s', albums) + albums_hist = self.get_album_history(artist) + self.log.debug('Albums history: %s', albums_hist) + 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) + if unplayed: + return [] + random.shuffle(albums_not_in_hist) + albums_not_in_hist.extend(albums_hist) + album_to_queue = [] + for album in albums_not_in_hist: + # Controls the album found is not already queued + if album in {t.album for t in self.player.queue}: + self.log.debug('"%s" already queued, skipping!', album) + return [] + # In random play mode use complete playlist to filter + if self.player.playmode.get('random'): + if album in {t.album for t in self.player.playlist}: + self.log.debug('"%s" already in playlist, skipping!', + album) + return [] + album_to_queue = album + if not album_to_queue: + self.log.info('No album found for "%s"', artist) + return [] + self.log.info('%s album candidate: %s - %s', self.__class__.__name__, + artist, album_to_queue) + return album_to_queue + + def filter_track(self, tracks, unplayed=False): + """ + Extract one unplayed track from a Track object list. + * not in history + * not already in the queue + """ + artist = tracks[0].Artist + # In random play mode use complete playlist to filter + if self.player.playmode.get('random'): + deny_list = self.player.playlist + else: + deny_list = self.player.queue + not_in_hist = list(set(tracks) - set(self.get_history(artist=artist))) + if not not_in_hist: + self.log.debug('All tracks already played for "%s"', artist) + if unplayed: + return None + random.shuffle(not_in_hist) + candidates = [_ for _ in not_in_hist if _ not in deny_list] + # for trk in [_ for _ in not_in_hist if _ not in deny_list]: + # # Should use albumartist heuristic as well + # 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 ' + + # 'but from an album already queued: %s', trk) + # continue + # candidates.append(trk) + if not candidates: + return None + return random.choice(candidates) + # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab