]> kaliko git repositories - mpd-sima.git/blob - sima/lib/webserv.py
96aacc9653352ff140eab9bd30cb3bdc00206833
[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 AdvancedPlugin
35 from .meta import Artist, MetaContainer
36 from ..utils.utils import WSError, WSNotFound, WSTimeout
37
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(AdvancedPlugin):
59     """similar artists webservice
60     """
61
62     def __init__(self, daemon):
63         super().__init__(daemon)
64         self.history = daemon.short_history
65         ##
66         self.to_add = list()
67         self._cache = None
68         self._flush_cache()
69         wrapper = {'track': self._track,
70                    'top': self._top,
71                    'album': self._album}
72         self.queue_mode = wrapper.get(self.plugin_conf.get('queue_mode'))
73         self.ws = None
74         self.ws_retry = 0
75
76     def _flush_cache(self):
77         """
78         Both flushes and instanciates _cache
79         """
80         name = self.__class__.__name__
81         if isinstance(self._cache, dict):
82             self.log.info('%s: Flushing cache!', name)
83         else:
84             self.log.info('%s: Initialising cache!', name)
85         self._cache = {'asearch': dict(),
86                        'tsearch': dict()}
87
88     def _cleanup_cache(self):
89         """Avoid bloated cache
90         """
91         for _, val in self._cache.items():
92             if isinstance(val, dict):
93                 while len(val) > 150:
94                     val.popitem()
95
96     @cache
97     def get_artists_from_player(self, similarities):
98         """
99         Look in player library for availability of similar artists in
100         similarities
101         """
102         dynamic = self.plugin_conf.getint('max_art')
103         if dynamic <= 0:
104             dynamic = 100
105         results = list()
106         similarities.reverse()
107         while (len(results) < dynamic and similarities):
108             art_pop = similarities.pop()
109             res = self.player.search_artist(art_pop)
110             if res:
111                 results.append(res)
112         return results
113
114     def ws_similar_artists(self, artist):
115         """
116         Retrieve similar artists from WebServive.
117         """
118         # initialize artists deque list to construct from DB
119         as_art = deque()
120         as_artists = self.ws.get_similar(artist=artist)
121         self.log.debug('Requesting %s for %r', self.ws.name, artist)
122         try:
123             [as_art.append(art) for art in as_artists]
124         except WSNotFound as err:
125             self.log.warning('%s: %s', self.ws.name, err)
126             if artist.mbid:
127                 self.log.debug('Trying without MusicBrainzID')
128                 try:
129                     return self.ws_similar_artists(Artist(name=artist.name))
130                 except WSNotFound as err:
131                     self.log.debug('%s: %s', self.ws.name, err)
132         except WSTimeout as err:
133             self.log.warning('%s: %s', self.ws.name, err)
134             if self.ws_retry < 3:
135                 self.ws_retry += 1
136                 self.log.warning('%s: retrying', self.ws.name)
137                 as_art = self.ws_similar_artists(artist)
138             else:
139                 self.log.warning('%s: stop retrying', self.ws.name)
140             self.ws_retry = 0
141         except WSError as err:
142             self.log.warning('%s: %s', self.ws.name, err)
143         if as_art:
144             self.log.debug('Fetched %d artist(s)', len(as_art))
145         return as_art
146
147     def get_recursive_similar_artist(self):
148         """Check against local player for similar artists (recursive w/ history)
149         """
150         if not self.player.playlist:
151             return []
152         history = list(self.history)
153         # In random play mode use complete playlist to filter
154         if self.player.playmode.get('random'):
155             history = self.player.playlist + history
156         else:
157             history = self.player.queue + history
158         history = deque(history)
159         last_trk = history.popleft()  # remove
160         extra_arts = list()
161         ret_extra = list()
162         depth = 0
163         while depth < self.plugin_conf.getint('depth'):
164             if not history:
165                 break
166             trk = history.popleft()
167             if (trk.Artist in extra_arts
168                     or trk.Artist == last_trk.Artist):
169                 continue
170             extra_arts.append(trk.Artist)
171             depth += 1
172         self.log.debug('EXTRA ARTS: %s', '/'.join(map(str, extra_arts)))
173         for artist in extra_arts:
174             self.log.debug('Looking for artist similar '
175                            'to "%s" as well', artist)
176             similar = self.ws_similar_artists(artist=artist)
177             if not similar:
178                 continue
179             ret_extra.extend(self.get_artists_from_player(similar))
180
181         if last_trk.Artist in ret_extra:
182             ret_extra.remove(last_trk.Artist)
183         if ret_extra:
184             self.log.debug('similar artist(s) found: %s',
185                            ' / '.join(map(str, MetaContainer(ret_extra))))
186         return ret_extra
187
188     def get_local_similar_artists(self):
189         """Check against local player for similar artists
190         """
191         if not self.player.playlist:
192             return []
193         tolookfor = self.player.playlist[-1].Artist
194         self.log.info('Looking for artist similar to "%s"', tolookfor)
195         self.log.debug('%r', tolookfor)
196         similar = self.ws_similar_artists(tolookfor)
197         if not similar:
198             self.log.info('Got nothing from %s!', self.ws.name)
199             return []
200         self.log.info('First five similar artist(s): %s...',
201                       ' / '.join(map(str, list(similar)[:5])))
202         self.log.info('Looking availability in music library')
203         ret = MetaContainer(self.get_artists_from_player(similar))
204         if ret:
205             self.log.debug('regular found in library: %s',
206                            ' / '.join(map(str, ret)))
207         else:
208             self.log.debug('Got nothing similar from library!')
209         ret_extra = None
210         if len(self.history) >= 2:
211             if self.plugin_conf.getint('depth') > 1:
212                 ret_extra = self.get_recursive_similar_artist()
213         if ret_extra:
214             # get them reorg to pick up best element
215             ret_extra = self.get_reorg_artists_list(ret_extra)
216             # tries to pickup less artist from extra art
217             if len(ret) > 4:
218                 ret_extra = MetaContainer(ret_extra[:max(4, len(ret))//2])
219             if ret_extra:
220                 self.log.debug('extra found in library: %s',
221                                ' / '.join(map(str, ret_extra)))
222             ret = ret | ret_extra
223         if not ret:
224             self.log.warning('Got nothing from music library.')
225             return []
226         # In random play mode use complete playlist to filter
227         if self.player.playmode.get('random'):
228             queued_artists = MetaContainer([trk.Artist for trk
229                                             in self.player.playlist])
230         else:
231             queued_artists = MetaContainer([trk.Artist for trk
232                                             in self.player.queue])
233         self.log.trace('Already queued: %s', queued_artists)
234         self.log.trace('Candidate: %s', ret)
235         if ret & queued_artists:
236             self.log.debug('Removing already queued artists: '
237                            '%s', '/'.join(map(str, ret & queued_artists)))
238             ret = ret - queued_artists
239         current = self.player.current
240         if current and current.Artist in ret:
241             self.log.debug('Removing current artist: %s', current.Artist)
242             ret = ret - MetaContainer([current.Artist])
243         # Move around similars items to get in unplayed|not recently played
244         # artist first.
245         self.log.info('Got %d artists in library', len(ret))
246         candidates = self.get_reorg_artists_list(ret)
247         if candidates:
248             self.log.info(' / '.join(map(str, candidates)))
249         return candidates
250
251     def find_album(self, artists):
252         """Find albums to queue.
253         """
254         self.to_add = list()
255         nb_album_add = 0
256         target_album_to_add = self.plugin_conf.getint('album_to_add')
257         for artist in artists:
258             album = self.album_candidate(artist, unplayed=True)
259             if not album:
260                 continue
261             nb_album_add += 1
262             candidates = self.player.find_tracks(album)
263             if self.plugin_conf.getboolean('shuffle_album'):
264                 random.shuffle(candidates)
265             # this allows to select a maximum number of track from the album
266             # a value of 0 (default) means keep all
267             nbtracks = self.plugin_conf.getint('track_to_add_from_album')
268             if nbtracks > 0:
269                 candidates = candidates[0:nbtracks]
270             self.to_add.extend(candidates)
271             if nb_album_add == target_album_to_add:
272                 return
273
274     def find_top(self, artists):
275         """
276         find top tracks for artists in artists list.
277         """
278         self.to_add = list()
279         nbtracks_target = self.plugin_conf.getint('track_to_add')
280         for artist in artists:
281             if len(self.to_add) == nbtracks_target:
282                 return
283             self.log.info('Looking for a top track for %s', artist)
284             titles = deque()
285             try:
286                 titles = [t for t in self.ws.get_toptrack(artist)]
287             except WSError as err:
288                 self.log.warning('%s: %s', self.ws.name, err)
289                 continue
290             for trk in titles:
291                 found = self.player.search_track(artist, trk.title)
292                 if found:
293                     random.shuffle(found)
294                     top_trk = self.filter_track(found)
295                     if top_trk:
296                         self.to_add.append(top_trk)
297                         break
298
299     def _track(self):
300         """Get some tracks for track queue mode
301         """
302         artists = self.get_local_similar_artists()
303         nbtracks_target = self.plugin_conf.getint('track_to_add')
304         for artist in artists:
305             self.log.debug('Trying to find titles to add for "%r"', artist)
306             found = self.player.find_tracks(artist)
307             if not found:
308                 self.log.debug('Found nothing to queue for %s', artist)
309                 continue
310             random.shuffle(found)
311             # find tracks not in history for artist
312             track_candidate = self.filter_track(found)
313             if track_candidate:
314                 self.to_add.append(track_candidate)
315             if len(self.to_add) == nbtracks_target:
316                 break
317         if not self.to_add:
318             self.log.debug('Found no tracks to queue!')
319             return
320         for track in self.to_add:
321             self.log.info('%s plugin chose: %s', self.ws.name, 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         self.find_top(artists)
334         for track in self.to_add:
335             self.log.info('%s candidates: %s', self.ws.name, track)
336
337     def callback_need_track(self):
338         self._cleanup_cache()
339         if not self.player.playlist:
340             self.log.info('No last track, cannot queue')
341             return None
342         if not self.player.playlist[-1].artist:
343             self.log.warning('No artist set for the last track in queue')
344             self.log.debug(repr(self.player.current))
345             return None
346         self.queue_mode()
347         msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for
348                         k, v in sorted(self.ws.stats.items())])
349         self.log.debug('http stats: ' + msg)
350         if not self.to_add:
351             self.log.info('%s plugin found nothing to queue', self.ws.name)
352         candidates = self.to_add
353         self.to_add = list()
354         if self.plugin_conf.get('queue_mode') != 'album':
355             random.shuffle(candidates)
356         return candidates
357
358     def callback_player_database(self):
359         self._flush_cache()
360
361 # VIM MODLINE
362 # vim: ai ts=4 sw=4 sts=4 expandtab