]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/randomfallback.py
Random fallback plugin, honoring MPD/host, more robust plugin
[mpd-sima.git] / sima / plugins / randomfallback.py
1 # -*- coding: utf-8 -*-
2 """
3 Fetching similar artists from last.fm web services
4 """
5
6 # standart library import
7 import random
8
9 # third parties componants
10
11 # local import
12 from ..lib.plugin import Plugin
13 from ..lib.track import Track
14
15
16 class RandomFallBack(Plugin):
17
18     def __init__(self, daemon):
19         Plugin.__init__(self, daemon)
20         self.player = daemon.player
21         self.daemon = daemon
22         ##
23         self.to_add = list()
24
25     def get_history(self):
26         """Constructs list of Track for already played titles.
27         """
28         duration = self.daemon.config.getint('sima', 'history_duration')
29         tracks_from_db = self.daemon.sdb.get_history(duration=duration,)
30         # Construct Track() objects list from database history
31         played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
32                                file=tr[3]) for tr in tracks_from_db]
33         return played_tracks
34
35     def callback_need_track_fb(self):
36         mode = self.plugin_conf.get('flavour')
37         art = random.choice(self.player.list('artist'))
38         self.log.debug('Random art: {}'.format(art))
39         trk  = random.choice(self.player.find_track(art))
40         self.log.info('random fallback ({}): {}'.format(mode, trk))
41         return [trk]
42
43
44
45 # VIM MODLINE
46 # vim: ai ts=4 sw=4 sts=4 expandtab