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