]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/webserv.py
Set max_art default value to 20
[mpd-sima.git] / sima / lib / webserv.py
index 0770ea8ae0afe4b84d96e3df0cdd13923b412016..8201964bdf1da71dbb9fa33e16fa590c497bb0bc 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2009-2020 kaliko <kaliko@azylum.org>
+# Copyright (c) 2009-2021 kaliko <kaliko@azylum.org>
 # Copyright (c) 2019 sacha <sachahony@gmail.com>
 #
 #  This file is part of sima
@@ -63,7 +63,6 @@ class WebService(AdvancedPlugin):
         super().__init__(daemon)
         self.history = daemon.short_history
         ##
-        self.to_add = list()
         self._cache = None
         self._flush_cache()
         wrapper = {'track': self._track,
@@ -82,8 +81,8 @@ class WebService(AdvancedPlugin):
             self.log.info('%s: Flushing cache!', name)
         else:
             self.log.info('%s: Initialising cache!', name)
-        self._cache = {'asearch': dict(),
-                       'tsearch': dict()}
+        self._cache = {'asearch': {},
+                       'tsearch': {}}
 
     def _cleanup_cache(self):
         """Avoid bloated cache
@@ -102,9 +101,9 @@ class WebService(AdvancedPlugin):
         dynamic = self.plugin_conf.getint('max_art')
         if dynamic <= 0:
             dynamic = 100
-        results = list()
+        results = []
         similarities.reverse()
-        while (len(results) < dynamic and similarities):
+        while (len(results) < dynamic+1 and similarities):
             art_pop = similarities.pop()
             res = self.player.search_artist(art_pop)
             if res:
@@ -157,8 +156,8 @@ class WebService(AdvancedPlugin):
             history = self.player.queue + history
         history = deque(history)
         last_trk = history.popleft()  # remove
-        extra_arts = list()
-        ret_extra = list()
+        extra_arts = []
+        ret_extra = []
         depth = 0
         while depth < self.plugin_conf.getint('depth'):
             if not history:
@@ -248,19 +247,10 @@ class WebService(AdvancedPlugin):
             self.log.info(' / '.join(map(str, candidates)))
         return candidates
 
-    def _get_album_history(self, artist):
-        """Retrieve album history"""
-        albums_list = set()
-        for trk in self.get_history(artist=artist.name):
-            if not trk.album:
-                continue
-            albums_list.add(trk.album)
-        return albums_list
-
     def find_album(self, artists):
         """Find albums to queue.
         """
-        self.to_add = list()
+        to_add = []
         nb_album_add = 0
         target_album_to_add = self.plugin_conf.getint('album_to_add')
         for artist in artists:
@@ -269,6 +259,8 @@ class WebService(AdvancedPlugin):
                 continue
             nb_album_add += 1
             candidates = self.player.find_tracks(album)
+            if not candidates:
+                continue
             if self.plugin_conf.getboolean('shuffle_album'):
                 random.shuffle(candidates)
             # this allows to select a maximum number of track from the album
@@ -276,23 +268,24 @@ class WebService(AdvancedPlugin):
             nbtracks = self.plugin_conf.getint('track_to_add_from_album')
             if nbtracks > 0:
                 candidates = candidates[0:nbtracks]
-            self.to_add.extend(candidates)
+            to_add.extend(candidates)
             if nb_album_add == target_album_to_add:
-                return
+                return to_add
+        return to_add
 
     def find_top(self, artists):
         """
         find top tracks for artists in artists list.
         """
-        self.to_add = list()
+        to_add = []
         nbtracks_target = self.plugin_conf.getint('track_to_add')
         for artist in artists:
-            if len(self.to_add) == nbtracks_target:
-                return
+            if len(to_add) == nbtracks_target:
+                break
             self.log.info('Looking for a top track for %s', artist)
             titles = deque()
             try:
-                titles = [t for t in self.ws.get_toptrack(artist)]
+                titles = list(self.ws.get_toptrack(artist))
             except WSError as err:
                 self.log.warning('%s: %s', self.ws.name, err)
                 continue
@@ -300,14 +293,18 @@ class WebService(AdvancedPlugin):
                 found = self.player.search_track(artist, trk.title)
                 if found:
                     random.shuffle(found)
-                    top_trk = self.filter_track(found)
+                    top_trk = self.filter_track(found, to_add)
                     if top_trk:
-                        self.to_add.append(top_trk)
+                        to_add.append(top_trk)
                         break
+        return to_add
 
     def _track(self):
         """Get some tracks for track queue mode
+
+        :return: list of Tracks
         """
+        to_add = []
         artists = self.get_local_similar_artists()
         nbtracks_target = self.plugin_conf.getint('track_to_add')
         for artist in artists:
@@ -318,30 +315,33 @@ class WebService(AdvancedPlugin):
                 continue
             random.shuffle(found)
             # find tracks not in history for artist
-            track_candidate = self.filter_track(found)
+            track_candidate = self.filter_track(found, to_add)
             if track_candidate:
-                self.to_add.append(track_candidate)
-            if len(self.to_add) == nbtracks_target:
+                to_add.append(track_candidate)
+                self.log.info('%s plugin chose: %s',
+                              self.ws.name, track_candidate)
+            if len(to_add) == nbtracks_target:
                 break
-        if not self.to_add:
-            self.log.debug('Found no tracks to queue!')
-            return
-        for track in self.to_add:
-            self.log.info('%s candidates: %s', track, self.ws.name)
+        return to_add
 
     def _album(self):
         """Get albums for album queue mode
+
+        :return: list of Tracks
         """
         artists = self.get_local_similar_artists()
-        self.find_album(artists)
+        return self.find_album(artists)
 
     def _top(self):
         """Get some tracks for top track queue mode
+
+        :return: list of Tracks
         """
         artists = self.get_local_similar_artists()
-        self.find_top(artists)
-        for track in self.to_add:
+        chosen = self.find_top(artists)
+        for track in chosen:
             self.log.info('%s candidates: %s', self.ws.name, track)
+        return chosen
 
     def callback_need_track(self):
         self._cleanup_cache()
@@ -352,12 +352,12 @@ class WebService(AdvancedPlugin):
             self.log.warning('No artist set for the last track in queue')
             self.log.debug(repr(self.player.current))
             return None
-        self.queue_mode()
-        msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for
+        candidates = self.queue_mode()
+        msg = ' '.join([f'{k}: {v:>3d}' for
                         k, v in sorted(self.ws.stats.items())])
         self.log.debug('http stats: ' + msg)
-        candidates = self.to_add
-        self.to_add = list()
+        if not candidates:
+            self.log.info('%s plugin found nothing to queue', self.ws.name)
         if self.plugin_conf.get('queue_mode') != 'album':
             random.shuffle(candidates)
         return candidates