[placeholder]
key = Value
+[RandomFallback]
+# random falvour :
+# * pure: complete ramdom choice among all tracks available in the player media library
+# * sensible: use play history to filter chosen tracks
+# * genre: chose among the same genre as current track (tagged genre). If
+# no genre tag is available "sensible" flavour is used instead
+flavour=pure
+
[lastfm]
depth = 3
import sys
from importlib import __import__
-from os.path import isfile, basename
+from os.path import isfile
##
# third parties components
from sima.plugins.addhist import History
from sima.plugins.lastfm import Lastfm
from sima.plugins.mpd import MpdOptions
+from sima.plugins.randomfallback import RandomFallBack
# official plugins to start
PLUGINS = (Crop, History, MpdOptions,
- Lastfm)
+ Lastfm, RandomFallBack)
def load_contrib_plugins(sima):
self.sdb = SimaDB(db_path=dbfile)
self.log = getLogger('sima')
self.plugins = list()
- self.player = PlayerClient() # Player client
+ self.player = self._get_player() # Player client
try:
self.player.connect()
except (PlayerError, PlayerUnHandledError) as err:
self.log.error('Fails to connect player: {}'.format(err))
self.shutdown()
- self.short_history = deque(maxlen=40)
+ self.short_history = deque(maxlen=60)
+
+ def _get_player(self):
+ """Instanciate the player"""
+ host = self.config.get('MPD', 'host')
+ port = self.config.get('MPD', 'port')
+ pswd = self.config.get('MPD', 'password', fallback=None)
+ return PlayerClient(host, port, pswd)
def add_history(self):
self.short_history.appendleft(self.player.current)
pl_callback = getattr(plugin, 'callback_need_track')()
if pl_callback:
to_add.extend(pl_callback)
+ if not to_add:
+ self.log.warning('Queue plugins returned anything!')
+ for plugin in self.plugins:
+ self.log.info('calling fb for {}'.format(plugin))
+ pl_callback = getattr(plugin, 'callback_need_track_fb')()
+ if pl_callback:
+ to_add.extend(pl_callback)
for track in to_add:
self.player.add(track)
def info(cls):
"""self documenting class method
"""
+ doc = 'Undocumented plugin! Fill "{}" docstring'.format(cls.__name__)
+ if cls.__doc__:
+ doc = cls.__doc__.strip(' \n').splitlines()[0]
return {'name': cls.__name__,
- 'doc': cls.__doc__.strip(' \n').splitlines()[0]
+ 'doc': doc,
}
def __init__(self, daemon):
"""
pass
+ def callback_need_track_fb(self):
+ """Called when callback_next_song failled to find tracks to queue
+ Returns a list of Track objects to add
+ """
+ pass
+
def shutdown(self):
pass
"""
for _ , val in self._cache.items():
if isinstance(val, dict):
- while len(val) > 100:
+ while len(val) > 150:
val.popitem()
def get_history(self, artist):
- """Check against history for tracks already in history for a specific
- artist.
+ """Constructs list of Track for already played titles for an artist.
"""
duration = self.daemon_conf.getint('sima', 'history_duration')
tracks_from_db = self.sdb.get_history(duration=duration, artist=artist)
def _album(self):
"""Get albums for album queue mode
"""
- artists = self.get_local_similar_artists()
+ #artists = self.get_local_similar_artists()
+ pass
def _top(self):
"""Get some tracks for top track queue mode
"""
- artists = self.get_local_similar_artists()
+ #artists = self.get_local_similar_artists()
+ pass
def callback_need_track(self):
self._cleanup_cache()
--- /dev/null
+# -*- coding: utf-8 -*-
+"""
+Fetching similar artists from last.fm web services
+"""
+
+# standart library import
+import random
+
+# third parties componants
+
+# local import
+from ..lib.plugin import Plugin
+from ..lib.track import Track
+
+
+class RandomFallBack(Plugin):
+
+ def __init__(self, daemon):
+ Plugin.__init__(self, daemon)
+ self.player = daemon.player
+ self.daemon = daemon
+ ##
+ self.to_add = list()
+
+ def get_history(self):
+ """Constructs list of Track for already played titles.
+ """
+ duration = self.daemon.config.getint('sima', 'history_duration')
+ tracks_from_db = self.daemon.sdb.get_history(duration=duration,)
+ # Construct Track() objects list from database history
+ played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
+ file=tr[3]) for tr in tracks_from_db]
+ return played_tracks
+
+ def callback_need_track_fb(self):
+ mode = self.plugin_conf.get('flavour')
+ art = random.choice(self.player.list('artist'))
+ self.log.debug('Random art: {}'.format(art))
+ trk = random.choice(self.player.find_track(art))
+ self.log.info('random fallback ({}): {}'.format(mode, trk))
+ return [trk]
+
+
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab
DEFAULT_CONF = {
'MPD': {
'host': "localhost",
- 'password': "false",
+ #'password': "",
'port': "6600"},
'sima': {
'user_db': "false",
'verbosity': "info"},
'lastfm': {
'dynamic': "10",
- 'similarity': "18",
+ 'similarity': "20",
'queue_mode': "track", #TODO control values
'single_album': "false",
'track_to_add': "1",
'album_to_add': "1",
'depth': "1",
+ },
+ 'randomfallback': {
+ 'flavour': "sensible", # in pure, sensible, genre
+ 'track_to_add': "1",
}
}
#