]> kaliko git repositories - mpd-sima.git/blob - sima/lib/webserv.py
291c4c4e3cccdb2c097f73ef04a14be908dd7ed8
[mpd-sima.git] / sima / lib / webserv.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009-2014 Jack Kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 """
21 Fetching similar artists from last.fm web services
22 """
23
24 # standard library import
25 import random
26
27 from collections import deque
28 from hashlib import md5
29
30 # third parties components
31
32 # local import
33 from .plugin import Plugin
34 from .track import Track
35 from .meta import Artist
36 from ..utils.utils import WSError
37
38 def cache(func):
39     """Caching decorator"""
40     def wrapper(*args, **kwargs):
41         #pylint: disable=W0212,C0111
42         cls = args[0]
43         similarities = [art for art in args[1]]
44         hashedlst = md5(''.join(similarities).encode('utf-8')).hexdigest()
45         if hashedlst in cls._cache.get('asearch'):
46             cls.log.debug('cached request')
47             results = cls._cache.get('asearch').get(hashedlst)
48         else:
49             results = func(*args, **kwargs)
50             cls.log.debug('caching request')
51             cls._cache.get('asearch').update({hashedlst:list(results)})
52         random.shuffle(results)
53         return results
54     return wrapper
55
56
57 class WebService(Plugin):
58     """similar artists webservice
59     """
60
61     def __init__(self, daemon):
62         Plugin.__init__(self, daemon)
63         self.daemon_conf = daemon.config
64         self.sdb = daemon.sdb
65         self.history = daemon.short_history
66         ##
67         self.to_add = list()
68         self._cache = None
69         self._flush_cache()
70         wrapper = {
71                 'track': self._track,
72                 'top': self._top,
73                 'album': self._album,
74                 }
75         self.queue_mode = wrapper.get(self.plugin_conf.get('queue_mode'))
76         self.ws = None
77
78     def _flush_cache(self):
79         """
80         Both flushes and instanciates _cache
81         """
82         name = self.__class__.__name__
83         if isinstance(self._cache, dict):
84             self.log.info('{0}: Flushing cache!'.format(name))
85         else:
86             self.log.info('{0}: Initialising cache!'.format(name))
87         self._cache = {
88                 'asearch': dict(),
89                 'tsearch': dict(),
90                 }
91
92     def _cleanup_cache(self):
93         """Avoid bloated cache
94         """
95         for _ , val in self._cache.items():
96             if isinstance(val, dict):
97                 while len(val) > 150:
98                     val.popitem()
99
100     def get_history(self, artist):
101         """Constructs list of Track for already played titles for an artist.
102         """
103         duration = self.daemon_conf.getint('sima', 'history_duration')
104         tracks_from_db = self.sdb.get_history(duration=duration, artist=artist)
105         # Construct Track() objects list from database history
106         played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
107                                file=tr[3]) for tr in tracks_from_db]
108         return played_tracks
109
110     def filter_track(self, tracks):
111         """
112         Extract one unplayed track from a Track object list.
113             * not in history
114             * not already in the queue
115             * not blacklisted
116         """
117         artist = tracks[0].artist
118         black_list = self.player.queue + self.to_add
119         not_in_hist = list(set(tracks) - set(self.get_history(artist=artist)))
120         if not not_in_hist:
121             self.log.debug('All tracks already played for "{}"'.format(artist))
122         random.shuffle(not_in_hist)
123         #candidate = [ trk for trk in not_in_hist if trk not in black_list
124                       #if not self.sdb.get_bl_track(trk, add_not=True)]
125         candidate = []
126         for trk in [_ for _ in not_in_hist if _ not in black_list]:
127             if self.sdb.get_bl_track(trk, add_not=True):
128                 self.log.info('Blacklisted: {0}: '.format(trk))
129                 continue
130             if self.sdb.get_bl_album(trk, add_not=True):
131                 self.log.info('Blacklisted album: {0}: '.format(trk))
132                 continue
133             # Should use albumartist heuristic as well
134             if self.plugin_conf.getboolean('single_album'):
135                 if (trk.album == self.player.current.album or
136                     trk.album in [tr.album for tr in self.to_add]):
137                     self.log.debug('Found unplayed track ' +
138                                'but from an album already queued: %s' % (trk))
139                     continue
140             candidate.append(trk)
141         if not candidate:
142             self.log.debug('Unable to find title to add' +
143                            ' for "%s".' % artist)
144             return None
145         self.to_add.append(random.choice(candidate))
146
147     def _get_artists_list_reorg(self, alist):
148         """
149         Move around items in artists_list in order to play first not recently
150         played artists
151         """
152         # TODO: move to utils as a decorator
153         duration = self.daemon_conf.getint('sima', 'history_duration')
154         art_in_hist = list()
155         for trk in self.sdb.get_history(duration=duration,
156                                         artists=alist):
157             if trk[0] not in art_in_hist:
158                 art_in_hist.append(trk[0])
159         art_in_hist.reverse()
160         art_not_in_hist = [ ar for ar in alist if ar not in art_in_hist ]
161         random.shuffle(art_not_in_hist)
162         art_not_in_hist.extend(art_in_hist)
163         self.log.debug('history ordered: {}'.format(
164                        ' / '.join(art_not_in_hist)))
165         return art_not_in_hist
166
167     @cache
168     def get_artists_from_player(self, similarities):
169         """
170         Look in player library for availability of similar artists in
171         similarities
172         """
173         dynamic = self.plugin_conf.getint('max_art')
174         if dynamic <= 0:
175             dynamic = 100
176         results = list()
177         similarities.reverse()
178         while (len(results) < dynamic
179             and len(similarities) > 0):
180             art_pop = similarities.pop()
181             results.extend(self.player.fuzzy_find_artist(art_pop))
182         return results
183
184     def lfm_similar_artists(self, artist=None):
185         """
186         Retrieve similar artists from WebServive.
187         """
188         if artist is None:
189             curr = self.player.current.__dict__
190             name = curr.get('artist')
191             mbid = curr.get('musicbrainz_artistid', None)
192             current = Artist(name=name, mbid=mbid)
193         else:
194             current = artist
195         # initialize artists deque list to construct from DB
196         as_art = deque()
197         as_artists = self.ws().get_similar(artist=current)
198         self.log.debug('Requesting {1} for "{0}"'.format(current,
199                         self.ws.name))
200         try:
201             # TODO: let's propagate Artist type
202             [as_art.append(str(art)) for art in as_artists]
203         except WSError as err:
204             self.log.warning('{0}: {1}'.format(self.ws.name, err))
205         if as_art:
206             self.log.debug('Fetched {0} artist(s)'.format(len(as_art)))
207         if self.ws.ratelimit:
208             self.log.info('{0.name} ratelimit: {0.ratelimit}'.format(self.ws))
209         return as_art
210
211     def get_recursive_similar_artist(self):
212         ret_extra = list()
213         history = deque(self.history)
214         history.popleft()
215         depth = 0
216         current = self.player.current
217         extra_arts = list()
218         while depth < self.plugin_conf.getint('depth'):
219             if len(history) == 0:
220                 break
221             trk = history.popleft()
222             if (trk.artist in [trk.artist for trk in extra_arts]
223                 or trk.artist == current.artist):
224                 continue
225             extra_arts.append(trk)
226             depth += 1
227         self.log.info('EXTRA ARTS: {}'.format(
228             '/'.join([trk.artist for trk in extra_arts])))
229         for artist in extra_arts:
230             self.log.debug(
231                     'Looking for artist similar to "{0.artist}" as well'.format(
232                         artist))
233             similar = self.lfm_similar_artists(artist=artist)
234             if not similar:
235                 return ret_extra
236             ret_extra.extend(self.get_artists_from_player(similar))
237             if current.artist in ret_extra:
238                 ret_extra.remove(current.artist)
239         return ret_extra
240
241     def get_local_similar_artists(self):
242         """Check against local player for similar artists
243         """
244         current = self.player.current
245         self.log.info('Looking for artist similar to "{0.artist}"'.format(current))
246         similar = self.lfm_similar_artists()
247         if not similar:
248             self.log.info('Got nothing from {0}!'.format(self.ws.name))
249             return []
250         self.log.info('First five similar artist(s): {}...'.format(
251                       ' / '.join([a for a in list(similar)[0:5]])))
252         self.log.info('Looking availability in music library')
253         ret = self.get_artists_from_player(similar)
254         ret_extra = None
255         if len(self.history) >= 2:
256             if self.plugin_conf.getint('depth') > 1:
257                 ret_extra = self.get_recursive_similar_artist()
258         if ret_extra:
259             ret = list(set(ret) | set(ret_extra))
260         if not ret:
261             self.log.warning('Got nothing from music library.')
262             self.log.warning('Try running in debug mode to guess why...')
263             return []
264         self.log.info('Got {} artists in library'.format(len(ret)))
265         self.log.info(' / '.join(ret))
266         # Move around similars items to get in unplayed|not recently played
267         # artist first.
268         return self._get_artists_list_reorg(ret)
269
270     def _get_album_history(self, artist=None):
271         """Retrieve album history"""
272         duration = self.daemon_conf.getint('sima', 'history_duration')
273         albums_list = set()
274         for trk in self.sdb.get_history(artist=artist, duration=duration):
275             albums_list.add(trk[1])
276         return albums_list
277
278     def find_album(self, artists):
279         """Find albums to queue.
280         """
281         self.to_add = list()
282         nb_album_add = 0
283         target_album_to_add = self.plugin_conf.getint('album_to_add')
284         for artist in artists:
285             self.log.info('Looking for an album to add for "%s"...' % artist)
286             albums = self.player.find_albums(artist)
287             # str conversion while Album type is not propagated
288             albums = [ str(album) for album in albums]
289             if albums:
290                 self.log.debug('Albums candidate: {0:s}'.format(
291                                ' / '.join(albums)))
292             else: continue
293             # albums yet in history for this artist
294             albums = set(albums)
295             albums_yet_in_hist = albums & self._get_album_history(artist=artist)
296             albums_not_in_hist = list(albums - albums_yet_in_hist)
297             # Get to next artist if there are no unplayed albums
298             if not albums_not_in_hist:
299                 self.log.info('No album found for "%s"' % artist)
300                 continue
301             album_to_queue = str()
302             random.shuffle(albums_not_in_hist)
303             for album in albums_not_in_hist:
304                 tracks = self.player.find_album(artist, album)
305                 # Look if one track of the album is already queued
306                 # Good heuristic, at least enough to guess if the whole album is
307                 # already queued.
308                 if tracks[0] in self.player.queue:
309                     self.log.debug('"%s" already queued, skipping!' %
310                             tracks[0].album)
311                     continue
312                 album_to_queue = album
313             if not album_to_queue:
314                 self.log.info('No album found for "%s"' % artist)
315                 continue
316             self.log.info('{2} album candidate: {0} - {1}'.format(
317                            artist, album_to_queue, self.ws.name))
318             nb_album_add += 1
319             self.to_add.extend(self.player.find_album(artist, album_to_queue))
320             if nb_album_add == target_album_to_add:
321                 return True
322
323     def _track(self):
324         """Get some tracks for track queue mode
325         """
326         artists = self.get_local_similar_artists()
327         nbtracks_target = self.plugin_conf.getint('track_to_add')
328         for artist in artists:
329             self.log.debug('Trying to find titles to add for "{}"'.format(
330                            artist))
331             found = self.player.find_track(artist)
332             # find tracks not in history for artist
333             self.filter_track(found)
334             if len(self.to_add) == nbtracks_target:
335                 break
336         if not self.to_add:
337             self.log.debug('Found no tracks to queue, is your ' +
338                             'history getting too large?')
339             return None
340         for track in self.to_add:
341             self.log.info('{1} candidates: {0!s}'.format(track, self.ws.name))
342
343     def _album(self):
344         """Get albums for album queue mode
345         """
346         artists = self.get_local_similar_artists()
347         self.find_album(artists)
348
349     def _top(self):
350         """Get some tracks for top track queue mode
351         """
352         #artists = self.get_local_similar_artists()
353         pass
354
355     def callback_need_track(self):
356         self._cleanup_cache()
357         if not self.player.current:
358             self.log.info('No current track, cannot queue')
359             return None
360         if not self.player.current.artist:
361             self.log.warning('No artist set for the current track')
362             self.log.debug(repr(self.player.current))
363             return None
364         self.queue_mode()
365         candidates = self.to_add
366         self.to_add = list()
367         if self.plugin_conf.get('queue_mode') != 'album':
368             random.shuffle(candidates)
369         return candidates
370
371     def callback_player_database(self):
372         self._flush_cache()
373
374 # VIM MODLINE
375 # vim: ai ts=4 sw=4 sts=4 expandtab