]> kaliko git repositories - mpd-sima.git/blob - sima/lib/webserv.py
Get rid of a useless direct db call
[mpd-sima.git] / sima / lib / webserv.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009-2020 kaliko <kaliko@azylum.org>
3 # Copyright (c) 2019 sacha <sachahony@gmail.com>
4 #
5 #  This file is part of sima
6 #
7 #  sima is free software: you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation, either version 3 of the License, or
10 #  (at your option) any later version.
11 #
12 #  sima is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 #
21 """
22 Fetching similar artists from last.fm web services
23 """
24
25 # standard library import
26 import random
27
28 from collections import deque
29 from hashlib import md5
30
31 # third parties components
32
33 # local import
34 from .plugin import Plugin
35 from .track import Track
36 from .meta import Artist, MetaContainer
37 from ..utils.utils import WSError, WSNotFound
38
39 def cache(func):
40     """Caching decorator"""
41     def wrapper(*args, **kwargs):
42         #pylint: disable=W0212,C0111
43         cls = args[0]
44         similarities = [art.name for art in args[1]]
45         hashedlst = md5(''.join(similarities).encode('utf-8')).hexdigest()
46         if hashedlst in cls._cache.get('asearch'):
47             cls.log.debug('cached request')
48             results = cls._cache.get('asearch').get(hashedlst)
49         else:
50             results = func(*args, **kwargs)
51             cls.log.debug('caching request')
52             cls._cache.get('asearch').update({hashedlst:list(results)})
53         random.shuffle(results)
54         return results
55     return wrapper
56
57
58 class WebService(Plugin):
59     """similar artists webservice
60     """
61     # pylint: disable=bad-builtin
62
63     def __init__(self, daemon):
64         Plugin.__init__(self, daemon)
65         self.daemon_conf = daemon.config
66         self.sdb = daemon.sdb
67         self.history = daemon.short_history
68         ##
69         self.to_add = list()
70         self._cache = None
71         self._flush_cache()
72         wrapper = {'track': self._track,
73                    'top': self._top,
74                    'album': self._album}
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('%s: Flushing cache!', name)
85         else:
86             self.log.info('%s: Initialising cache!', name)
87         self._cache = {'asearch': dict(),
88                        'tsearch': dict()}
89
90     def _cleanup_cache(self):
91         """Avoid bloated cache
92         """
93         for _, val in self._cache.items():
94             if isinstance(val, dict):
95                 while len(val) > 150:
96                     val.popitem()
97
98     def get_history(self, artist):
99         """Constructs list of Track for already played titles for an artist.
100         """
101         duration = self.daemon_conf.getint('sima', 'history_duration')
102         tracks_from_db = self.sdb.get_history(duration=duration, artist=artist)
103         # Construct Track() objects list from database history
104         played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
105                                file=tr[3]) for tr in tracks_from_db]
106         return played_tracks
107
108     def filter_track(self, tracks):
109         """
110         Extract one unplayed track from a Track object list.
111             * not in history
112             * not already in the queue
113             * not blacklisted
114         Then add to candidates in self.to_add
115         """
116         artist = tracks[0].artist
117         # In random play mode use complete playlist to filter
118         if self.player.playmode.get('random'):
119             black_list = self.player.playlist + self.to_add
120         else:
121             black_list = self.player.queue + self.to_add
122         not_in_hist = list(set(tracks) - set(self.get_history(artist=artist)))
123         if self.plugin_conf.get('queue_mode') != 'top' and not not_in_hist:
124             self.log.debug('All tracks already played for "%s"', artist)
125         random.shuffle(not_in_hist)
126         candidate = []
127         for trk in [_ for _ in not_in_hist if _ not in black_list]:
128             # Should use albumartist heuristic as well
129             if self.plugin_conf.getboolean('single_album'):  # pylint: disable=no-member
130                 if (trk.album == self.player.current.album or
131                         trk.album in [tr.album for tr in black_list]):
132                     self.log.debug('Found unplayed track ' +
133                                    'but from an album already queued: %s', trk)
134                     continue
135             candidate.append(trk)
136         if not candidate:
137             return False
138         self.to_add.append(random.choice(candidate))
139         return True
140
141     def _get_artists_list_reorg(self, alist):
142         """
143         Move around items in artists_list in order to play first not recently
144         played artists
145         """
146         hist = list()
147         duration = self.daemon_conf.getint('sima', 'history_duration')
148         for art in self.sdb.get_artists_history(alist, duration=duration):
149             if art not in hist:
150                 hist.insert(0, art)
151         reorg = [art for art in alist if art not in hist]
152         reorg.extend(hist)
153         return reorg
154
155     @cache
156     def get_artists_from_player(self, similarities):
157         """
158         Look in player library for availability of similar artists in
159         similarities
160         """
161         dynamic = self.plugin_conf.getint('max_art') # pylint: disable=no-member
162         if dynamic <= 0:
163             dynamic = 100
164         results = list()
165         similarities.reverse()
166         while (len(results) < dynamic
167                and len(similarities) > 0):
168             art_pop = similarities.pop()
169             res = self.player.search_artist(art_pop)
170             if res:
171                 results.append(res)
172         return results
173
174     def ws_similar_artists(self, artist):
175         """
176         Retrieve similar artists from WebServive.
177         """
178         # initialize artists deque list to construct from DB
179         as_art = deque()
180         as_artists = self.ws.get_similar(artist=artist)
181         self.log.debug('Requesting %s for %r', self.ws.name, artist)
182         try:
183             [as_art.append(art) for art in as_artists]
184         except WSNotFound as err:
185             self.log.warning('%s: %s', self.ws.name, err)
186             if artist.mbid:
187                 self.log.debug('Trying without MusicBrainzID')
188                 try:
189                     return self.ws_similar_artists(Artist(name=artist.name))
190                 except WSNotFound as err:
191                     self.log.debug('%s: %s', self.ws.name, err)
192         except WSError as err:
193             self.log.warning('%s: %s', self.ws.name, err)
194         if as_art:
195             self.log.debug('Fetched %d artist(s)', len(as_art))
196         return as_art
197
198     def get_recursive_similar_artist(self):
199         """Check against local player for similar artists (recursive w/ history)
200         """
201         if not self.player.playlist:
202             return
203         history = list(self.history)
204         # In random play mode use complete playlist to filter
205         if self.player.playmode.get('random'):
206             history = self.player.playlist + history
207         else:
208             history = self.player.queue + history
209         history = deque(history)
210         last_trk = history.popleft() # remove
211         extra_arts = list()
212         ret_extra = list()
213         depth = 0
214         while depth < self.plugin_conf.getint('depth'): # pylint: disable=no-member
215             if len(history) == 0:
216                 break
217             trk = history.popleft()
218             if (trk.Artist in extra_arts
219                     or trk.Artist == last_trk.Artist):
220                 continue
221             extra_arts.append(trk.Artist)
222             depth += 1
223         self.log.debug('EXTRA ARTS: %s', '/'.join(map(str, extra_arts)))
224         for artist in extra_arts:
225             self.log.debug('Looking for artist similar '
226                            'to "%s" as well', artist)
227             similar = self.ws_similar_artists(artist=artist)
228             if not similar:
229                 continue
230             ret_extra.extend(self.get_artists_from_player(similar))
231
232         if last_trk.Artist in ret_extra:
233             ret_extra.remove(last_trk.Artist)
234         if ret_extra:
235             self.log.debug('similar artist(s) found: %s',
236                            ' / '.join(map(str, MetaContainer(ret_extra))))
237         return ret_extra
238
239     def get_local_similar_artists(self):
240         """Check against local player for similar artists
241         """
242         if not self.player.playlist:
243             return []
244         tolookfor = self.player.playlist[-1].Artist
245         self.log.info('Looking for artist similar to "%s"', tolookfor)
246         self.log.debug(repr(tolookfor))
247         similar = self.ws_similar_artists(tolookfor)
248         if not similar:
249             self.log.info('Got nothing from %s!', self.ws.name)
250             return []
251         self.log.info('First five similar artist(s): %s...',
252                       ' / '.join(map(str, list(similar)[:5])))
253         self.log.info('Looking availability in music library')
254         ret = MetaContainer(self.get_artists_from_player(similar))
255         if ret:
256             self.log.debug('regular found in library: %s',
257                            ' / '.join(map(str, ret)))
258         else:
259             self.log.debug('Got nothing similar from library!')
260         ret_extra = None
261         if len(self.history) >= 2:
262             if self.plugin_conf.getint('depth') > 1: # pylint: disable=no-member
263                 ret_extra = self.get_recursive_similar_artist()
264         if ret_extra:
265             # get them reorg to pick up best element
266             ret_extra = self._get_artists_list_reorg(ret_extra)
267             # tries to pickup less artist from extra art
268             if len(ret) < 4:
269                 ret_extra = MetaContainer(ret_extra)
270             else:
271                 ret_extra = MetaContainer(ret_extra[:max(4, len(ret))//2])
272             if ret_extra:
273                 self.log.debug('extra found in library: %s',
274                                ' / '.join(map(str, ret_extra)))
275             ret = ret | ret_extra
276         if not ret:
277             self.log.warning('Got nothing from music library.')
278             return []
279         # In random play mode use complete playlist to filter
280         if self.player.playmode.get('random'):
281             queued_artists = MetaContainer([trk.Artist for trk in self.player.playlist])
282         else:
283             queued_artists = MetaContainer([trk.Artist for trk in self.player.queue])
284         self.log.trace('Already queued: %s', queued_artists)
285         self.log.trace('Candidate: %s', ret)
286         if ret & queued_artists:
287             self.log.debug('Removing already queued artists: '
288                            '%s', '/'.join(map(str, ret & queued_artists)))
289             ret = ret - queued_artists
290         current = self.player.current
291         if current and current.Artist in ret:
292             self.log.debug('Removing current artist: %s', current.Artist)
293             ret = ret - MetaContainer([current.Artist])
294         # Move around similars items to get in unplayed|not recently played
295         # artist first.
296         self.log.info('Got %d artists in library', len(ret))
297         candidates = self._get_artists_list_reorg(list(ret))
298         if candidates:
299             self.log.info(' / '.join(map(str, candidates)))
300         return candidates
301
302     def _get_album_history(self, artist):
303         """Retrieve album history"""
304         albums_list = set()
305         for trk in self.get_history(artist=artist.name):
306             albums_list.add(trk[1])
307         return albums_list
308
309     def find_album(self, artists):
310         """Find albums to queue.
311         """
312         self.to_add = list()
313         nb_album_add = 0
314         target_album_to_add = self.plugin_conf.getint('album_to_add')  # pylint: disable=no-member
315         for artist in artists:
316             self.log.info('Looking for an album to add for "%s"...' % artist)
317             albums = self.player.search_albums(artist)
318             if not albums:
319                 continue
320             self.log.debug('Albums candidate: %s', albums)
321             albums_hist = self._get_album_history(artist)
322             albums_not_in_hist = [a for a in albums if a.name not in albums_hist]
323             # Get to next artist if there are no unplayed albums
324             if not albums_not_in_hist:
325                 self.log.info('No unplayed album found for "%s"' % artist)
326                 continue
327             album_to_queue = []
328             random.shuffle(albums_not_in_hist)
329             for album in albums_not_in_hist:
330                 # Controls the album found is not already queued
331                 if album in {t.album for t in self.player.queue}:
332                     self.log.debug('"%s" already queued, skipping!', album)
333                     continue
334                 # In random play mode use complete playlist to filter
335                 if self.player.playmode.get('random'):
336                     if album in {t.album for t in self.player.playlist}:
337                         self.log.debug('"%s" already in playlist, skipping!', album)
338                         continue
339                 album_to_queue = album
340             if not album_to_queue:
341                 self.log.info('No album found for "%s"', artist)
342                 continue
343             self.log.info('%s album candidate: %s - %s', self.ws.name,
344                           artist, album_to_queue)
345             nb_album_add += 1
346             candidates = self.player.find_tracks(album)
347             if self.plugin_conf.getboolean('shuffle_album'):
348                 random.shuffle(candidates)
349             # this allows to select a maximum number of track from the album
350             # a value of 0 (default) means keep all
351             nbtracks = self.plugin_conf.getint('track_to_add_from_album')
352             if nbtracks > 0:
353                 candidates = candidates[0:nbtracks]
354             self.to_add.extend(candidates)
355             if nb_album_add == target_album_to_add:
356                 return True
357
358     def find_top(self, artists):
359         """
360         find top tracks for artists in artists list.
361         """
362         self.to_add = list()
363         nbtracks_target = self.plugin_conf.getint('track_to_add') # pylint: disable=no-member
364         for artist in artists:
365             if len(self.to_add) == nbtracks_target:
366                 return
367             self.log.info('Looking for a top track for %s', artist)
368             titles = deque()
369             try:
370                 titles = [t for t in self.ws.get_toptrack(artist)]
371             except WSError as err:
372                 self.log.warning('%s: %s', self.ws.name, err)
373             for trk in titles:
374                 found = self.player.search_track(artist, trk.title)
375                 if found:
376                     random.shuffle(found)
377                     if self.filter_track(found):
378                         break
379
380     def _track(self):
381         """Get some tracks for track queue mode
382         """
383         artists = self.get_local_similar_artists()
384         nbtracks_target = self.plugin_conf.getint('track_to_add')  # pylint: disable=no-member
385         for artist in artists:
386             self.log.debug('Trying to find titles to add for "%r"', artist)
387             found = self.player.find_tracks(artist)
388             random.shuffle(found)
389             if not found:
390                 self.log.debug('Found nothing to queue for %s', artist)
391                 continue
392             # find tracks not in history for artist
393             self.filter_track(found)
394             if len(self.to_add) == nbtracks_target:
395                 break
396         if not self.to_add:
397             self.log.debug('Found no tracks to queue!')
398             return None
399         for track in self.to_add:
400             self.log.info('%s candidates: %s', track, self.ws.name)
401
402     def _album(self):
403         """Get albums for album queue mode
404         """
405         artists = self.get_local_similar_artists()
406         self.find_album(artists)
407
408     def _top(self):
409         """Get some tracks for top track queue mode
410         """
411         artists = self.get_local_similar_artists()
412         self.find_top(artists)
413         for track in self.to_add:
414             self.log.info('%s candidates: %s', self.ws.name, track)
415
416     def callback_need_track(self):
417         self._cleanup_cache()
418         if len(self.player.playlist) == 0:
419             self.log.info('No last track, cannot queue')
420             return None
421         if not self.player.playlist[-1].artist:
422             self.log.warning('No artist set for the last track in queue')
423             self.log.debug(repr(self.player.current))
424             return None
425         self.queue_mode()
426         msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for
427                         k, v in sorted(self.ws.stats.items())])
428         self.log.debug('http stats: ' + msg)
429         candidates = self.to_add
430         self.to_add = list()
431         if self.plugin_conf.get('queue_mode') != 'album':
432             random.shuffle(candidates)
433         return candidates
434
435     def callback_player_database(self):
436         self._flush_cache()
437
438 # VIM MODLINE
439 # vim: ai ts=4 sw=4 sts=4 expandtab