]> kaliko git repositories - mpd-sima.git/blobdiff - sima/plugins/internal/random.py
Update simadb API
[mpd-sima.git] / sima / plugins / internal / random.py
index 4d9bb805124cef4c23388c7763334d36f0659003..5af0ba1a42e509a90dc460da5b4eb12568d002bd 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2013, 2014, 2015 Jack Kaliko <kaliko@azylum.org>
+# Copyright (c) 2013-2015, 2020-2021 kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
@@ -37,7 +37,6 @@ class Random(Plugin):
 
     def __init__(self, daemon):
         super().__init__(daemon)
-        self.daemon = daemon
         self.mode = self.plugin_conf.get('flavour', None)
         if self.mode not in ['pure', 'sensible']:
             self.log.warning('Bad value for flavour, '
@@ -47,12 +46,10 @@ class Random(Plugin):
         self.candidates = []
 
     def get_played_artist(self,):
-        """Constructs list of already played artists.
-        """
-        duration = self.daemon.config.getint('sima', 'history_duration')
-        tracks_from_db = self.daemon.sdb.get_history(duration=duration)
-        artists = [tr[0] for tr in tracks_from_db]
-        return set(artists)
+        """Constructs list of already played artists."""
+        duration = self.main_conf.getint('sima', 'history_duration')
+        artists = self.sdb.fetch_artists_history(duration=duration)
+        return artists
 
     def filtered_artist(self, artist):
         """Filters artists:
@@ -60,11 +57,11 @@ class Random(Plugin):
 
         If sensible random is set:
          * not in recent history
-         * not blacklisted
+         * not in blocklist
         """
         if self.mode == 'sensible':
-            if self.daemon.sdb.get_bl_artist(artist, add_not=True):
-                self.log.debug('Random: Blacklisted "%s"', artist)
+            if self.sdb.get_bl_artist(Artist(artist), add=False):
+                self.log.debug('Random plugin: Blacklisted "%s"', artist)
                 return True
             if artist in self.get_played_artist():
                 return True
@@ -78,17 +75,17 @@ class Random(Plugin):
         self.candidates = []
         trks = []
         target = self.plugin_conf.getint('track_to_add')
-        artists = list(self.player.artists)
+        artists = self.player.list('artist', '( artist != "")')
         random.shuffle(artists)
-        for art in artists:
+        for art in artists:  # artists is a list of strings here
             if self.filtered_artist(art):
                 continue
-            self.log.debug('Random art: {}'.format(art))
-            trks = self.player.find_track(Artist(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 plugin chose (%s): %s', self.mode, trk)
             if len(self.candidates) >= target:
                 break
         return self.candidates