]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/plugin.py
Update simadb API
[mpd-sima.git] / sima / lib / plugin.py
index 58a0e57b783cd0bc10f2d3c983cf2d1b614f9feb..1afafe5455297e5643fb0f977297bf5c3c0cf29c 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2013-2015, 2020 kaliko <kaliko@azylum.org>
+# Copyright (c) 2013-2015, 2020-2021 kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
@@ -126,29 +126,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):
         """
@@ -157,11 +143,12 @@ class AdvancedPlugin(Plugin):
 
         :param {Artist} alist: Artist objects list/container
         """
-        queued_artist = MetaContainer([Artist(_.artist) for _ in self.player.queue])
+        queued_artist = MetaContainer([Artist(_.artist) for _ in
+                                       self.player.queue if _.artist])
         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)
@@ -209,11 +196,12 @@ class AdvancedPlugin(Plugin):
                                    album)
                     continue
             album_to_queue = album
+            break
         if not album_to_queue:
             self.log.info('No album found for "%s"', artist)
             return None
-        self.log.info('%s album candidate: %s - %s', self.__class__.__name__,
-                      artist, album_to_queue)
+        self.log.info('%s plugin chose album: %s - %s',
+                      self.__class__.__name__, artist, album_to_queue)
         return album_to_queue
 
     def filter_track(self, tracks, unplayed=False):
@@ -228,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: