]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/lastfm.py
c5a772f5b763552961854bda1787a90a9aad0867
[mpd-sima.git] / sima / plugins / internal / lastfm.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 from collections import deque
10 from itertools import dropwhile
11 from hashlib import md5
12
13 # third parties componants
14
15 # local import
16 from ...lib.plugin import Plugin
17 from ...lib.simafm import SimaFM, XmlFMHTTPError, XmlFMNotFound, XmlFMError
18 from ...lib.track import Track
19
20
21 def cache(func):
22     """Caching decorator"""
23     def wrapper(*args, **kwargs):
24         #pylint: disable=W0212,C0111
25         cls = args[0]
26         similarities = [art + str(match) for art, match in args[1]]
27         hashedlst = md5(''.join(similarities).encode('utf-8')).hexdigest()
28         if hashedlst in cls._cache.get('asearch'):
29             cls.log.debug('cached request')
30             results = cls._cache.get('asearch').get(hashedlst)
31         else:
32             results = func(*args, **kwargs)
33             cls._cache.get('asearch').update({hashedlst:list(results)})
34         random.shuffle(results)
35         return results
36     return wrapper
37
38
39 def blacklist(artist=False, album=False, track=False):
40     #pylint: disable=C0111,W0212
41     field = (artist, album, track)
42     def decorated(func):
43         def wrapper(*args, **kwargs):
44             cls = args[0]
45             boolgen = (bl for bl in field)
46             bl_fun = (cls._Plugin__daemon.sdb.get_bl_artist,
47                       cls._Plugin__daemon.sdb.get_bl_album,
48                       cls._Plugin__daemon.sdb.get_bl_track,)
49             #bl_getter = next(fn for fn, bl in zip(bl_fun, boolgen) if bl is True)
50             bl_getter = next(dropwhile(lambda _: not next(boolgen), bl_fun))
51             cls.log.debug('using {0} as bl filter'.format(bl_getter.__name__))
52             if artist:
53                 results = func(*args, **kwargs)
54                 for elem in results:
55                     if bl_getter(elem, add_not=True):
56                         cls.log.info('Blacklisted: {0}'.format(elem))
57                         results.remove(elem)
58                 return results
59             if track:
60                 for elem in args[1]:
61                     if bl_getter(elem, add_not=True):
62                         cls.log.info('Blacklisted: {0}'.format(elem))
63                         args[1].remove(elem)
64                 return func(*args, **kwargs)
65         return wrapper
66     return decorated
67
68
69 class Lastfm(Plugin):
70     """last.fm similar artists
71     """
72
73     def __init__(self, daemon):
74         Plugin.__init__(self, daemon)
75         self.daemon_conf = daemon.config
76         self.sdb = daemon.sdb
77         self.history = daemon.short_history
78         ##
79         self.to_add = list()
80         self._cache = None
81         self._flush_cache()
82         wrapper = {
83                 'track': self._track,
84                 'top': self._top,
85                 'album': self._album,
86                 }
87         self.queue_mode = wrapper.get(self.plugin_conf.get('queue_mode'))
88
89     def _flush_cache(self):
90         """
91         Both flushes and instanciates _cache
92         """
93         if isinstance(self._cache, dict):
94             self.log.info('Lastfm: Flushing cache!')
95         else:
96             self.log.info('Lastfm: Initialising cache!')
97         self._cache = {
98                 'artists': None,
99                 'asearch': dict(),
100                 'tsearch': dict(),
101                 }
102         self._cache['artists'] = frozenset(self.player.list('artist'))
103
104     def _cleanup_cache(self):
105         """Avoid bloated cache
106         """
107         for _ , val in self._cache.items():
108             if isinstance(val, dict):
109                 while len(val) > 150:
110                     val.popitem()
111
112     def get_history(self, artist):
113         """Constructs list of Track for already played titles for an artist.
114         """
115         duration = self.daemon_conf.getint('sima', 'history_duration')
116         tracks_from_db = self.sdb.get_history(duration=duration, artist=artist)
117         # Construct Track() objects list from database history
118         played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
119                                file=tr[3]) for tr in tracks_from_db]
120         return played_tracks
121
122     def filter_track(self, tracks):
123         """
124         Extract one unplayed track from a Track object list.
125             * not in history
126             * not already in the queue
127             * not blacklisted
128         """
129         artist = tracks[0].artist
130         black_list = self.player.queue + self.to_add
131         not_in_hist = list(set(tracks) - set(self.get_history(artist=artist)))
132         if not not_in_hist:
133             self.log.debug('All tracks already played for "{}"'.format(artist))
134         random.shuffle(not_in_hist)
135         #candidate = [ trk for trk in not_in_hist if trk not in black_list
136                       #if not self.sdb.get_bl_track(trk, add_not=True)]
137         candidate = []
138         for trk in [_ for _ in not_in_hist if _ not in black_list]:
139             if self.sdb.get_bl_track(trk, add_not=True):
140                 self.log.info('Blacklisted: {0}: '.format(trk))
141                 continue
142             if self.sdb.get_bl_album(trk, add_not=True):
143                 self.log.info('Blacklisted album: {0}: '.format(trk))
144                 continue
145             # Should use albumartist heuristic as well
146             if self.plugin_conf.getboolean('single_album'):
147                 if (trk.album == self.player.current.album or
148                     trk.album in [trk.alb for trk in self.to_add]):
149                     self.log.debug('Found unplayed track ' +
150                                'but from an album already queued: %s' % (trk))
151                     continue
152             candidate.append(trk)
153         if not candidate:
154             self.log.debug('Unable to find title to add' +
155                            ' for "%s".' % artist)
156             return None
157         self.to_add.append(random.choice(candidate))
158
159     def _get_artists_list_reorg(self, alist):
160         """
161         Move around items in artists_list in order to play first not recently
162         played artists
163         """
164         # TODO: move to utils as a decorator
165         duration = self.daemon_conf.getint('sima', 'history_duration')
166         art_in_hist = list()
167         for trk in self.sdb.get_history(duration=duration,
168                                         artists=alist):
169             if trk[0] not in art_in_hist:
170                 art_in_hist.append(trk[0])
171         art_in_hist.reverse()
172         art_not_in_hist = [ ar for ar in alist if ar not in art_in_hist ]
173         random.shuffle(art_not_in_hist)
174         art_not_in_hist.extend(art_in_hist)
175         self.log.debug('history ordered: {}'.format(
176                        ' / '.join(art_not_in_hist)))
177         return art_not_in_hist
178
179     @blacklist(artist=True)
180     @cache
181     def get_artists_from_player(self, similarities):
182         """
183         Look in player library for availability of similar artists in
184         similarities
185         """
186         dynamic = int(self.plugin_conf.get('dynamic'))
187         if dynamic <= 0:
188             dynamic = 100
189         similarity = int(self.plugin_conf.get('similarity'))
190         results = list()
191         similarities.reverse()
192         while (len(results) < dynamic
193             and len(similarities) > 0):
194             art_pop, match = similarities.pop()
195             if match < similarity:
196                 break
197             results.extend(self.player.fuzzy_find(art_pop))
198         results and self.log.debug('Similarity: %d%%' % match) # pylint: disable=w0106
199         return results
200
201     def lfm_similar_artists(self, artist=None):
202         """
203         Retrieve similar artists on last.fm server.
204         """
205         if artist is None:
206             current = self.player.current
207         else:
208             current = artist
209         simafm = SimaFM()
210         # initialize artists deque list to construct from DB
211         as_art = deque()
212         as_artists = simafm.get_similar(artist=current.artist)
213         self.log.debug('Requesting last.fm for "{0.artist}"'.format(current))
214         try:
215             [as_art.append((a, m)) for a, m in as_artists]
216         except XmlFMHTTPError as err:
217             self.log.warning('last.fm http error: %s' % err)
218         except XmlFMNotFound as err:
219             self.log.warning("last.fm: %s" % err)
220         except XmlFMError as err:
221             self.log.warning('last.fm module error: %s' % err)
222         if as_art:
223             self.log.debug('Fetched %d artist(s) from last.fm' % len(as_art))
224         return as_art
225
226     def get_recursive_similar_artist(self):
227         ret_extra = list()
228         history = deque(self.history)
229         history.popleft()
230         depth = 0
231         current = self.player.current
232         extra_arts = list()
233         while depth < int(self.plugin_conf.get('depth')):
234             if len(history) == 0:
235                 break
236             trk = history.popleft()
237             if (trk.artist in [trk.artist for trk in extra_arts]
238                 or trk.artist == current.artist):
239                 continue
240             extra_arts.append(trk)
241             depth += 1
242         self.log.info('EXTRA ARTS: {}'.format(
243             '/'.join([trk.artist for trk in extra_arts])))
244         for artist in extra_arts:
245             self.log.debug('Looking for artist similar to "{0.artist}" as well'.format(artist))
246             similar = self.lfm_similar_artists(artist=artist)
247             similar = sorted(similar, key=lambda sim: sim[1], reverse=True)
248             ret_extra.extend(self.get_artists_from_player(similar))
249             if current.artist in ret_extra:
250                 ret_extra.remove(current.artist)
251         return ret_extra
252
253     def get_local_similar_artists(self):
254         """Check against local player for similar artists fetched from last.fm
255         """
256         current = self.player.current
257         self.log.info('Looking for artist similar to "{0.artist}"'.format(current))
258         similar = self.lfm_similar_artists()
259         if not similar:
260             self.log.info('Got nothing from last.fm!')
261             return []
262         similar = sorted(similar, key=lambda sim: sim[1], reverse=True)
263         self.log.info('First five similar artist(s): {}...'.format(
264                       ' / '.join([a for a, m in similar[0:5]])))
265         self.log.info('Looking availability in music library')
266         ret = self.get_artists_from_player(similar)
267         ret_extra = None
268         if len(self.history) >= 2:
269             ret_extra = self.get_recursive_similar_artist()
270         if not ret:
271             self.log.warning('Got nothing from music library.')
272             self.log.warning('Try running in debug mode to guess why...')
273             return []
274         if ret_extra:
275             ret = list(set(ret) | set(ret_extra))
276         self.log.info('Got {} artists in library'.format(len(ret)))
277         self.log.info(' / '.join(ret))
278         # Move around similars items to get in unplayed|not recently played
279         # artist first.
280         return self._get_artists_list_reorg(ret)
281
282     def _detects_var_artists_album(self, album, artist):
283         """Detects either an album is a "Various Artists" or a
284         single artist release."""
285         art_first_track = None
286         for track in self.player.find_album(artist, album):
287             if not art_first_track:  # set artist for the first track
288                 art_first_track = track.artist
289             alb_art = track.albumartist
290             #  Special heuristic used when AlbumArtist is available
291             if (alb_art):
292                 if artist == alb_art:
293                     # When album artist field is similar to the artist we're
294                     # looking an album for, the album is considered good to
295                     # queue
296                     return False
297                 else:
298                     self.log.debug(track)
299                     self.log.debug('album art says "%s", looking for "%s",'
300                                    ' not queueing this album' %
301                                    (alb_art, artist))
302                     return True
303         return False
304
305     def _get_album_history(self, artist=None):
306         """Retrieve album history"""
307         duration = self.daemon_conf.getint('sima', 'history_duration')
308         albums_list = set()
309         for trk in self.sdb.get_history(artist=artist, duration=duration):
310             albums_list.add(trk[1])
311         return albums_list
312
313     def find_album(self, artists):
314         """Find albums to queue.
315         """
316         self.to_add = list()
317         nb_album_add = 0
318         target_album_to_add = int(self.plugin_conf.get('album_to_add'))
319         for artist in artists:
320             self.log.info('Looking for an album to add for "%s"...' % artist)
321             albums = set(self.player.find_albums(artist))
322             # albums yet in history for this artist
323             albums_yet_in_hist = albums & self._get_album_history(artist=artist)
324             albums_not_in_hist = list(albums - albums_yet_in_hist)
325             # Get to next artist if there are no unplayed albums
326             if not albums_not_in_hist:
327                 self.log.info('No album found for "%s"' % artist)
328                 continue
329             album_to_queue = str()
330             random.shuffle(albums_not_in_hist)
331             for album in albums_not_in_hist:
332                 tracks = self.player.find('album', album)
333                 if self._detects_var_artists_album(album, artist):
334                     continue
335                 if tracks and self.sdb.get_bl_album(tracks[0], add_not=True):
336                     self.log.info('Blacklisted album: "%s"' % album)
337                     self.log.debug('using track: "%s"' % tracks[0])
338                     continue
339                 # Look if one track of the album is already queued
340                 # Good heuristic, at least enough to guess if the whole album is
341                 # already queued.
342                 if tracks[0] in self.player.queue:
343                     self.log.debug('"%s" already queued, skipping!' %
344                             tracks[0].album)
345                     continue
346                 album_to_queue = album
347             if not album_to_queue:
348                 self.log.info('No album found for "%s"' % artist)
349                 continue
350             self.log.info('last.fm album candidate: {0} - {1}'.format(
351                            artist, album_to_queue))
352             nb_album_add += 1
353             self.to_add.extend(self.player.find_album(artist, album_to_queue))
354             if nb_album_add == target_album_to_add:
355                 return True
356
357     def _track(self):
358         """Get some tracks for track queue mode
359         """
360         artists = self.get_local_similar_artists()
361         nbtracks_target = int(self.plugin_conf.get('track_to_add'))
362         for artist in artists:
363             self.log.debug('Trying to find titles to add for "{}"'.format(
364                            artist))
365             found = self.player.find_track(artist)
366             # find tracks not in history for artist
367             self.filter_track(found)
368             if len(self.to_add) == nbtracks_target:
369                 break
370         if not self.to_add:
371             self.log.debug('Found no tracks to queue, is your ' +
372                             'history getting too large?')
373             return None
374         for track in self.to_add:
375             self.log.info('last.fm candidate: {0!s}'.format(track))
376
377     def _album(self):
378         """Get albums for album queue mode
379         """
380         artists = self.get_local_similar_artists()
381         self.find_album(artists)
382
383     def _top(self):
384         """Get some tracks for top track queue mode
385         """
386         #artists = self.get_local_similar_artists()
387         pass
388
389     def callback_need_track(self):
390         self._cleanup_cache()
391         if not self.player.current:
392             self.log.info('Not currently playing track, cannot queue')
393             return None
394         self.queue_mode()
395         candidates = self.to_add
396         self.to_add = list()
397         if self.plugin_conf.get('queue_mode') != 'album':
398             random.shuffle(candidates)
399         return candidates
400
401     def callback_player_database(self):
402         self._flush_cache()
403
404 # VIM MODLINE
405 # vim: ai ts=4 sw=4 sts=4 expandtab