# -*- 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
super().__init__(daemon)
self.history = daemon.short_history
##
- self.to_add = list()
self._cache = None
self._flush_cache()
wrapper = {'track': self._track,
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
dynamic = self.plugin_conf.getint('max_art')
if dynamic <= 0:
dynamic = 100
- results = list()
+ results = []
similarities.reverse()
while (len(results) < dynamic and similarities):
art_pop = similarities.pop()
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:
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:
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
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
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:
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 plugin chose: %s', self.ws.name, track)
+ 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()
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)
- if not self.to_add:
+ if not candidates:
self.log.info('%s plugin found nothing to queue', self.ws.name)
- candidates = self.to_add
- self.to_add = list()
if self.plugin_conf.get('queue_mode') != 'album':
random.shuffle(candidates)
return candidates