]> kaliko git repositories - mpd-sima.git/blob - sima/lib/webserv.py
Fixed sqlite sqlite3.OperationalError VACUUM Error
[mpd-sima.git] / sima / lib / webserv.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009-2015 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, MetaContainer
36 from ..utils.utils import WSError, WSNotFound
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.name 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     # pylint: disable=bad-builtin
61
62     def __init__(self, daemon):
63         Plugin.__init__(self, daemon)
64         self.daemon_conf = daemon.config
65         self.sdb = daemon.sdb
66         self.history = daemon.short_history
67         ##
68         self.to_add = list()
69         self._cache = None
70         self._flush_cache()
71         wrapper = {'track': self._track,
72                    'top': self._top,
73                    'album': self._album,}
74         self.queue_mode = wrapper.get(self.plugin_conf.get('queue_mode'))
75         self.ws = None
76
77     def _flush_cache(self):
78         """
79         Both flushes and instanciates _cache
80         """
81         name = self.__class__.__name__
82         if isinstance(self._cache, dict):
83             self.log.info('{0}: Flushing cache!'.format(name))
84         else:
85             self.log.info('{0}: Initialising cache!'.format(name))
86         self._cache = {'asearch': dict(),
87                        'tsearch': dict(),}
88
89     def _cleanup_cache(self):
90         """Avoid bloated cache
91         """
92         for _, val in self._cache.items():
93             if isinstance(val, dict):
94                 while len(val) > 150:
95                     val.popitem()
96
97     def get_history(self, artist):
98         """Constructs list of Track for already played titles for an artist.
99         """
100         duration = self.daemon_conf.getint('sima', 'history_duration')
101         tracks_from_db = self.sdb.get_history(duration=duration, artist=artist)
102         # Construct Track() objects list from database history
103         played_tracks = [Track(artist=tr[-1], album=tr[1], title=tr[2],
104                                file=tr[3]) for tr in tracks_from_db]
105         return played_tracks
106
107     def filter_track(self, tracks):
108         """
109         Extract one unplayed track from a Track object list.
110             * not in history
111             * not already in the queue
112             * not blacklisted
113         """
114         artist = tracks[0].artist
115         # In random play mode use complete playlist to filter
116         if self.player.playmode.get('random'):
117             black_list = self.player.playlist + self.to_add
118         else:
119             black_list = self.player.queue + self.to_add
120         not_in_hist = list(set(tracks) - set(self.get_history(artist=artist)))
121         if self.plugin_conf.get('queue_mode') != 'top' and not not_in_hist:
122             self.log.debug('All tracks already played for "%s"', artist)
123         random.shuffle(not_in_hist)
124         candidate = []
125         for trk in [_ for _ in not_in_hist if _ not in black_list]:
126             # Should use albumartist heuristic as well
127             if self.plugin_conf.getboolean('single_album'): # pylint: disable=no-member
128                 if (trk.album == self.player.current.album or
129                         trk.album in [tr.album for tr in black_list]):
130                     self.log.debug('Found unplayed track ' +
131                                    'but from an album already queued: %s', trk)
132                     continue
133             candidate.append(trk)
134         if not candidate:
135             return False
136         self.to_add.append(random.choice(candidate))
137         return True
138
139     def _get_artists_list_reorg(self, alist):
140         """
141         Move around items in artists_list in order to play first not recently
142         played artists
143         """
144         hist = list()
145         duration = self.daemon_conf.getint('sima', 'history_duration')
146         for art in self.sdb.get_artists_history(alist, duration=duration):
147             if art not in hist:
148                 hist.insert(0, art)
149         reorg = [art for art in alist if art not in hist]
150         reorg.extend(hist)
151         return reorg
152
153     @cache
154     def get_artists_from_player(self, similarities):
155         """
156         Look in player library for availability of similar artists in
157         similarities
158         """
159         dynamic = self.plugin_conf.getint('max_art') # pylint: disable=no-member
160         if dynamic <= 0:
161             dynamic = 100
162         results = list()
163         similarities.reverse()
164         while (len(results) < dynamic
165                and len(similarities) > 0):
166             art_pop = similarities.pop()
167             res = self.player.search_artist(art_pop)
168             if res:
169                 results.append(res)
170         return results
171
172     def ws_similar_artists(self, artist):
173         """
174         Retrieve similar artists from WebServive.
175         """
176         # initialize artists deque list to construct from DB
177         as_art = deque()
178         as_artists = self.ws.get_similar(artist=artist)
179         self.log.debug('Requesting {} for {!r}'.format(self.ws.name, artist))
180         try:
181             [as_art.append(art) for art in as_artists]
182         except WSNotFound as err:
183             self.log.warning('{}: {}'.format(self.ws.name, err))
184             if artist.mbid:
185                 self.log.debug('Trying without MusicBrainzID')
186                 try:
187                     return self.ws_similar_artists(Artist(name=artist.name))
188                 except WSNotFound as err:
189                     self.log.debug('{}: {}'.format(self.ws.name, err))
190         except WSError as err:
191             self.log.warning('{}: {}'.format(self.ws.name, err))
192         if as_art:
193             self.log.debug('Fetched {} artist(s)'.format(len(as_art)))
194         return as_art
195
196     def get_recursive_similar_artist(self):
197         """Check against local player for similar artists (recursive w/ history)
198         """
199         if not self.player.playlist:
200             return
201         history = list(self.history)
202         # In random play mode use complete playlist to filter
203         if self.player.playmode.get('random'):
204             history = self.player.playlist + history
205         else:
206             history = self.player.queue + history
207         history = deque(history)
208         last_trk = history.popleft() # remove
209         extra_arts = list()
210         ret_extra = list()
211         depth = 0
212         while depth < self.plugin_conf.getint('depth'): # pylint: disable=no-member
213             if len(history) == 0:
214                 break
215             trk = history.popleft()
216             if (trk.Artist in extra_arts
217                     or trk.Artist == last_trk.Artist):
218                 continue
219             extra_arts.append(trk.Artist)
220             depth += 1
221         self.log.debug('EXTRA ARTS: %s', '/'.join(map(str, extra_arts)))
222         for artist in extra_arts:
223             self.log.debug('Looking for artist similar '
224                            'to "{}" as well'.format(artist))
225             similar = self.ws_similar_artists(artist=artist)
226             if not similar:
227                 continue
228             ret_extra.extend(self.get_artists_from_player(similar))
229
230         if last_trk.Artist in ret_extra:
231             ret_extra.remove(last_trk.Artist)
232         if ret_extra:
233             self.log.debug('similar artist(s) found: %s',
234                            ' / '.join(map(str, MetaContainer(ret_extra))))
235         return ret_extra
236
237     def get_local_similar_artists(self):
238         """Check against local player for similar artists
239         """
240         if not self.player.playlist:
241             return []
242         tolookfor = self.player.playlist[-1].Artist
243         self.log.info('Looking for artist similar to "{}"'.format(tolookfor))
244         self.log.debug(repr(tolookfor))
245         similar = self.ws_similar_artists(tolookfor)
246         if not similar:
247             self.log.info('Got nothing from {0}!'.format(self.ws.name))
248             return []
249         self.log.info('First five similar artist(s): %s...',
250                       ' / '.join(map(str, list(similar)[:5])))
251         self.log.info('Looking availability in music library')
252         ret = MetaContainer(self.get_artists_from_player(similar))
253         if ret:
254             self.log.debug('regular found in library: %s',
255                            ' / '.join(map(str, ret)))
256         else:
257             self.log.debug('Got nothing similar from library!')
258         ret_extra = None
259         if len(self.history) >= 2:
260             if self.plugin_conf.getint('depth') > 1: # pylint: disable=no-member
261                 ret_extra = self.get_recursive_similar_artist()
262         if ret_extra:
263             # get them reorg to pick up best element
264             ret_extra = self._get_artists_list_reorg(ret_extra)
265             # tries to pickup less artist from extra art
266             if len(ret) < 4:
267                 ret_extra = MetaContainer(ret_extra)
268             else:
269                 ret_extra = MetaContainer(ret_extra[:max(4, len(ret))//2])
270             if ret_extra:
271                 self.log.debug('extra found in library: %s',
272                                ' / '.join(map(str, ret_extra)))
273             ret = ret | ret_extra
274         if not ret:
275             self.log.warning('Got nothing from music library.')
276             return []
277         # In random play mode use complete playlist to filter
278         if self.player.playmode.get('random'):
279             queued_artists = MetaContainer([trk.Artist for trk in self.player.playlist])
280         else:
281             queued_artists = MetaContainer([trk.Artist for trk in self.player.queue])
282         self.log.trace('Already queued: {}'.format(queued_artists))
283         self.log.trace('Candidate: {}'.format(ret))
284         if ret & queued_artists:
285             self.log.debug('Removing already queued artists: '
286                            '{0}'.format('/'.join(map(str, ret & queued_artists))))
287             ret = ret - queued_artists
288         if self.player.current and self.player.current.Artist in ret:
289             self.log.debug('Removing current artist: {0}'.format(self.player.current.Artist))
290             ret = ret -  MetaContainer([self.player.current.Artist])
291         # Move around similars items to get in unplayed|not recently played
292         # artist first.
293         self.log.info('Got {} artists in library'.format(len(ret)))
294         candidates = self._get_artists_list_reorg(list(ret))
295         if candidates:
296             self.log.info(' / '.join(map(str, candidates)))
297         return candidates
298
299     def _get_album_history(self, artist=None):
300         """Retrieve album history"""
301         duration = self.daemon_conf.getint('sima', 'history_duration')
302         albums_list = set()
303         for trk in self.sdb.get_history(artist=artist.name, duration=duration):
304             albums_list.add(trk[1])
305         return albums_list
306
307     def find_album(self, artists):
308         """Find albums to queue.
309         """
310         self.to_add = list()
311         nb_album_add = 0
312         target_album_to_add = self.plugin_conf.getint('album_to_add') # pylint: disable=no-member
313         for artist in artists:
314             self.log.info('Looking for an album to add for "%s"...' % artist)
315             albums = self.player.search_albums(artist)
316             # str conversion while Album type is not propagated
317             albums = [str(album) for album in albums]
318             if albums:
319                 self.log.debug('Albums candidate: %s', ' / '.join(albums))
320             else: continue
321             # albums yet in history for this artist
322             albums = set(albums)
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 unplayed 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                 # Controls the album found is not already queued
333                 if album in {t.album for t in self.player.queue}:
334                     self.log.debug('"%s" already queued, skipping!', album)
335                     continue
336                 # In random play mode use complete playlist to filter
337                 if self.player.playmode.get('random'):
338                     if album in {t.album for t in self.player.playlist}:
339                         self.log.debug('"%s" already in playlist, skipping!', album)
340                         continue
341                 album_to_queue = album
342             if not album_to_queue:
343                 self.log.info('No album found for "%s"', artist)
344                 continue
345             self.log.info('%s album candidate: %s - %s', self.ws.name, artist, album_to_queue)
346             nb_album_add += 1
347             self.to_add.extend(self.player.find_album(artist, album_to_queue))
348             if nb_album_add == target_album_to_add:
349                 return True
350
351     def find_top(self, artists):
352         """
353         find top tracks for artists in artists list.
354         """
355         self.to_add = list()
356         nbtracks_target = self.plugin_conf.getint('track_to_add') # pylint: disable=no-member
357         for artist in artists:
358             if len(self.to_add) == nbtracks_target:
359                 return True
360             self.log.info('Looking for a top track for {0}'.format(artist))
361             titles = deque()
362             try:
363                 titles = [t for t in self.ws.get_toptrack(artist)]
364             except WSError as err:
365                 self.log.warning('%s: %s', self.ws.name, err)
366             for trk in titles:
367                 found = self.player.fuzzy_find_track(artist, trk.title)
368                 random.shuffle(found)
369                 if found:
370                     self.log.debug('%s', found[0])
371                     if self.filter_track(found):
372                         break
373
374     def _track(self):
375         """Get some tracks for track queue mode
376         """
377         artists = self.get_local_similar_artists()
378         nbtracks_target = self.plugin_conf.getint('track_to_add') # pylint: disable=no-member
379         for artist in artists:
380             self.log.debug('Trying to find titles to add for "%r"', artist)
381             found = self.player.find_track(artist)
382             random.shuffle(found)
383             if not found:
384                 self.log.debug('Found nothing to queue for {0}'.format(artist))
385                 continue
386             # find tracks not in history for artist
387             self.filter_track(found)
388             if len(self.to_add) == nbtracks_target:
389                 break
390         if not self.to_add:
391             self.log.debug('Found no tracks to queue!')
392             return None
393         for track in self.to_add:
394             self.log.info('{1} candidates: {0!s}'.format(track, self.ws.name))
395
396     def _album(self):
397         """Get albums for album queue mode
398         """
399         artists = self.get_local_similar_artists()
400         self.find_album(artists)
401
402     def _top(self):
403         """Get some tracks for top track queue mode
404         """
405         artists = self.get_local_similar_artists()
406         self.find_top(artists)
407         for track in self.to_add:
408             self.log.info('{1} candidates: {0!s}'.format(track, self.ws.name))
409
410     def callback_need_track(self):
411         self._cleanup_cache()
412         if len(self.player.playlist) == 0:
413             self.log.info('No last track, cannot queue')
414             return None
415         if not self.player.playlist[-1].artist:
416             self.log.warning('No artist set for the last track in queue')
417             self.log.debug(repr(self.player.current))
418             return None
419         self.queue_mode()
420         msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for
421                         k, v in sorted(self.ws.stats.items())])
422         self.log.debug('http stats: ' + msg)
423         candidates = self.to_add
424         self.to_add = list()
425         if self.plugin_conf.get('queue_mode') != 'album':
426             random.shuffle(candidates)
427         return candidates
428
429     def callback_player_database(self):
430         self._flush_cache()
431
432 # VIM MODLINE
433 # vim: ai ts=4 sw=4 sts=4 expandtab