]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/randomfallback.py
f2eaac4dc9eb412819c64b16a8c5068793697f2a
[mpd-sima.git] / sima / plugins / internal / 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         super().__init__(daemon)
20         self.daemon = daemon
21         if not self.plugin_conf:
22             return
23         self.mode = self.plugin_conf.get('flavour', None)
24         if self.mode not in ['pure', 'sensible', 'genre']:
25             self.log.warning('Bad value for flavour, '
26                     '{} not in ["pure", "sensible", "genre"]'.format(self.mode))
27             self.mode = 'pure'
28
29     def get_played_artist(self,):
30         """Constructs list of already played artists.
31         """
32         duration = self.daemon.config.getint('sima', 'history_duration')
33         tracks_from_db = self.daemon.sdb.get_history(duration=duration)
34         # Construct Track() objects list from database history
35         artists = [ tr[-1] for tr in tracks_from_db ]
36         return set(artists)
37
38     def callback_need_track_fb(self):
39         art = random.choice(self.player.list('artist'))
40         self.log.debug('Random art: {}'.format(art))
41         if self.mode == 'sensitive':
42             played_art = self.get_played_artist()
43             while 42:
44                 art = random.choice(self.player.list('artist'))
45                 if art not in played_art:
46                     break
47         trk  = random.choice(self.player.find_track(art))
48         self.log.info('random fallback ({}): {}'.format(self.mode, trk))
49         return [trk]
50
51
52
53 # VIM MODLINE
54 # vim: ai ts=4 sw=4 sts=4 expandtab