]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/plugin.py
Cleanup plugin class
[mpd-sima.git] / sima / lib / plugin.py
index 49cd7f155d9c0ee057e0353aef4e27cf4e28f10f..9a721bbd4630edd2fadbe5a4ecf2f32e91e8ee32 100644 (file)
@@ -23,8 +23,7 @@ Plugin object to derive from
 
 import random
 
-from .track import Track
-from .meta import Album, Artist, MetaContainer
+from .meta import Artist, MetaContainer
 
 
 class Plugin:
@@ -42,7 +41,7 @@ class Plugin:
     def info(cls):
         """self documenting class method
         """
-        doc = 'Undocumented plugin! Fill "{}" docstring'.format(cls.__name__)
+        doc = f'Undocumented plugin! Fill "{cls.__name__}" docstring'
         if cls.__doc__:
             doc = cls.__doc__.strip(' \n').splitlines()[0]
         return {'name': cls.__name__,
@@ -110,12 +109,6 @@ class Plugin:
         Returns a list of Track objects to add
         """
 
-    def callback_need_track_fb(self):
-        """
-        Called when callback_need_track failled to find tracks to queue
-        Returns a list of Track objects to add
-        """
-
     def shutdown(self):
         """Called on application shutdown"""
 
@@ -126,29 +119,15 @@ class AdvancedPlugin(Plugin):
     """
 
     # Query History
-    def get_history(self, artist=False):
-        """Constructs Track list of already played artists.
-
-        :param Artist artist: Artist object to look history for
-        """
+    def get_history(self):
+        """Returns a Track list of already played artists."""
         duration = self.main_conf.getint('sima', 'history_duration')
-        name = None
-        if artist:
-            name = artist.name
-        from_db = self.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
+        return self.sdb.fetch_history(duration=duration)
 
     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
+        duration = self.main_conf.getint('sima', 'history_duration')
+        return self.sdb.fetch_albums_history(needle=artist, duration=duration)
 
     def get_reorg_artists_list(self, alist):
         """
@@ -162,7 +141,7 @@ class AdvancedPlugin(Plugin):
         not_queued_artist = alist - queued_artist
         duration = self.main_conf.getint('sima', 'history_duration')
         hist = []
-        for art in self.sdb.get_artists_history(alist, duration=duration):
+        for art in self.sdb.fetch_artists_history(alist, duration=duration):
             if art not in hist:
                 if art not in queued_artist:
                     hist.insert(0, art)
@@ -181,31 +160,32 @@ class AdvancedPlugin(Plugin):
         :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)
+        self.log.info('Searching an album for "%s"...', artist)
         albums = self.player.search_albums(artist)
         if not albums:
             return None
-        self.log.debug('Albums candidates: %s', albums)
+        self.log.debug('Albums to choose from: %s', albums)
         albums_hist = self.get_album_history(artist)
         self.log.trace('Albums history: %s', [a.name for a in 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)
+            self.log.info('No unplayed album found for "%s"', artist)
             if unplayed:
                 return None
         random.shuffle(albums_not_in_hist)
         albums_not_in_hist.extend(albums_hist)
-        self.log.debug('Albums candidate: %s', albums_not_in_hist)
+        self.log.trace('Album candidates: %s', albums_not_in_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}:
+            if album in {t.Album.name for t in self.player.queue}:
                 self.log.debug('"%s" already queued, skipping!', album)
                 continue
             # In random play mode use complete playlist to filter
+            # Yes indeed, some users play in random with album mode :|
             if self.player.playmode.get('random'):
-                if album in {t.album for t in self.player.playlist}:
+                if album in {t.Album.name for t in self.player.playlist}:
                     self.log.debug('"%s" already in playlist, skipping!',
                                    album)
                     continue
@@ -218,11 +198,17 @@ class AdvancedPlugin(Plugin):
                       self.__class__.__name__, artist, album_to_queue)
         return album_to_queue
 
-    def filter_track(self, tracks, unplayed=False):
+    def filter_track(self, tracks, chosen=None, unplayed=False):
         """
         Extract one unplayed track from a Track object list.
             * not in history
             * not already in the queue
+
+        :param list(Track) tracks: List of tracks to chose from
+        :param list(Track) chosen: List of tracks previously chosen
+        :param bool unplayed: chose only unplayed (honoring history duration setting)
+        :return: A Track
+        :rtype: Track
         """
         artist = tracks[0].Artist
         # In random play mode use complete playlist to filter
@@ -230,7 +216,7 @@ class AdvancedPlugin(Plugin):
             deny_list = self.player.playlist
         else:
             deny_list = self.player.queue
-        not_in_hist = list(set(tracks) - set(self.get_history(artist=artist)))
+        not_in_hist = list(set(tracks) - set(self.sdb.fetch_history(artist=artist)))
         if not not_in_hist:
             self.log.debug('All tracks already played for "%s"', artist)
             if unplayed:
@@ -240,8 +226,10 @@ class AdvancedPlugin(Plugin):
         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', False):  # pylint: disable=no-member
-                if (trk.album == self.player.current.album or
-                        trk.album in [tr.album for tr in deny_list]):
+                albums = [tr.Album for tr in deny_list]
+                albums += [tr.Album for tr in chosen]
+                if (trk.Album == self.player.current.Album or
+                        trk.Album in albums):
                     self.log.debug('Found unplayed track ' +
                                    'but from an album already queued: %s', trk)
                     continue