]> kaliko git repositories - mpd-sima.git/blob - sima/mpdclient.py
Deal with lost connection (Closes #53)
[mpd-sima.git] / sima / mpdclient.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009-2021 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 # standard library import
20 from difflib import get_close_matches
21 from functools import wraps
22 from logging import getLogger
23 from select import select
24
25 # external module
26 from musicpd import MPDClient, MPDError as PlayerError
27
28
29 # local import
30 from .lib.meta import Meta, Artist, Album
31 from .lib.track import Track
32 from .lib.simastr import SimaStr
33 from .utils.leven import levenshtein_ratio
34
35
36 # Some decorators
37 def bl_artist(func):
38     def wrapper(*args, **kwargs):
39         cls = args[0]
40         if not cls.database:
41             return func(*args, **kwargs)
42         result = func(*args, **kwargs)
43         if not result:
44             return None
45         for art in result.names:
46             artist = Artist(name=art, mbid=result.mbid)
47             if cls.database.get_bl_artist(artist, add=False):
48                 cls.log.debug('Artist in blocklist: %s', artist)
49                 return None
50         return result
51     return wrapper
52
53 def set_artist_mbid(func):
54     def wrapper(*args, **kwargs):
55         cls = args[0]
56         result = func(*args, **kwargs)
57         if Meta.use_mbid:
58             if result and not result.mbid:
59                 mbid = cls._find_musicbrainz_artistid(result)
60                 artist = Artist(name=result.name, mbid=mbid)
61                 artist.add_alias(result)
62                 return artist
63         return result
64     return wrapper
65
66 def tracks_wrapper(func):
67     """Convert plain track mapping as returned by MPDClient into :py:obj:`sima.lib.track.Track`
68     objects. This decorator accepts single track or list of tracks as input.
69     """
70     @wraps(func)
71     def wrapper(*args, **kwargs):
72         ret = func(*args, **kwargs)
73         if isinstance(ret, dict):
74             return Track(**ret)
75         return [Track(**t) for t in ret]
76     return wrapper
77 # / decorators
78
79
80 class MPD(MPDClient):
81     """
82     Player instance inheriting from MPDClient (python-musicpd).
83
84     Some methods are overridden to format objects as :py:obj:`sima.lib.track.Track` for
85     instance, other are calling parent class directly through super().
86     cf. MPD.__getattr__
87
88     .. note::
89
90         * find methods are looking for exact match of the object provided
91           attributes in MPD music library
92         * search methods are looking for exact match + fuzzy match.
93     """
94     needed_cmds = ['status', 'stats', 'add', 'find',
95                    'search', 'currentsong', 'ping']
96     needed_tags = {'Artist', 'Album', 'AlbumArtist', 'Title', 'Track'}
97     needed_mbid_tags = {'MUSICBRAINZ_ARTISTID', 'MUSICBRAINZ_ALBUMID',
98                         'MUSICBRAINZ_ALBUMARTISTID', 'MUSICBRAINZ_TRACKID'}
99     MPD_supported_tags = {'Artist', 'ArtistSort', 'Album', 'AlbumSort', 'AlbumArtist',
100                           'AlbumArtistSort', 'Title', 'Track', 'Name', 'Genre',
101                           'Date', 'OriginalDate', 'Composer', 'Performer',
102                           'Conductor', 'Work', 'Grouping', 'Disc', 'Label',
103                           'MUSICBRAINZ_ARTISTID', 'MUSICBRAINZ_ALBUMID',
104                           'MUSICBRAINZ_ALBUMARTISTID', 'MUSICBRAINZ_TRACKID',
105                           'MUSICBRAINZ_RELEASETRACKID', 'MUSICBRAINZ_WORKID'}
106     database = None
107
108     def __init__(self, config):
109         super().__init__()
110         self.socket_timeout = 10
111         self.use_mbid = True
112         self.log = getLogger('sima')
113         self.config = config
114         self._cache = None
115
116     # ######### Overriding MPDClient ###########
117     def __getattr__(self, cmd):
118         """Wrapper around MPDClient calls for abstract overriding"""
119         track_wrapped = {'currentsong', 'find', 'playlistinfo', }
120         try:
121             if cmd in track_wrapped:
122                 return tracks_wrapper(super().__getattr__(cmd))
123             return super().__getattr__(cmd)
124         except OSError as err:
125             raise PlayerError(err)
126
127     def disconnect(self):
128         """Overriding explicitly MPDClient.disconnect()"""
129         if self._sock:
130             super().disconnect()
131
132     def connect(self):
133         """Overriding explicitly MPDClient.connect()"""
134         mpd_config = self.config['MPD']
135         # host, port, password
136         host = mpd_config.get('host')
137         port = mpd_config.get('port')
138         password = mpd_config.get('password', fallback=None)
139         self.disconnect()
140         try:
141             super().connect(host, port)
142         # Catch socket errors
143         except OSError as err:
144             raise PlayerError('Could not connect to "%s:%s": %s' %
145                               (host, port, err.strerror))
146         # Catch all other possible errors
147         # ConnectionError and ProtocolError are always fatal.  Others may not
148         # be, but we don't know how to handle them here, so treat them as if
149         # they are instead of ignoring them.
150         except PlayerError as err:
151             raise PlayerError('Could not connect to "%s:%s": %s' %
152                               (host, port, err))
153         if password:
154             try:
155                 self.password(password)
156             except (PlayerError, OSError) as err:
157                 raise PlayerError("Could not connect to '%s': %s" % (host, err))
158         # Controls we have sufficient rights
159         available_cmd = self.commands()
160         for cmd in MPD.needed_cmds:
161             if cmd not in available_cmd:
162                 self.disconnect()
163                 raise PlayerError('Could connect to "%s", '
164                                   'but command "%s" not available' %
165                                   (host, cmd))
166         self.tagtypes('clear')
167         for tag in MPD.needed_tags:
168             self.tagtypes('enable', tag)
169         tt = set(map(str.lower, self.tagtypes()))
170         needed_tags = set(map(str.lower, MPD.needed_tags))
171         if len(needed_tags & tt) != len(MPD.needed_tags):
172             self.log.warning('MPD exposes: %s', tt)
173             self.log.warning('Tags needed: %s', needed_tags)
174             raise PlayerError('Missing mandatory metadata!')
175         for tag in MPD.needed_mbid_tags:
176             self.tagtypes('enable', tag)
177         # Controls use of MusicBrainzIdentifier
178         if self.config.getboolean('sima', 'musicbrainzid'):
179             tt = set(self.tagtypes())
180             if len(MPD.needed_mbid_tags & tt) != len(MPD.needed_mbid_tags):
181                 self.log.warning('Use of MusicBrainzIdentifier is set but MPD '
182                                  'is not providing related metadata')
183                 self.log.info(tt)
184                 self.log.warning('Disabling MusicBrainzIdentifier')
185                 self.use_mbid = Meta.use_mbid = False
186             else:
187                 self.log.debug('Available metadata: %s', tt)
188                 self.use_mbid = Meta.use_mbid = True
189         else:
190             self.log.warning('Use of MusicBrainzIdentifier disabled!')
191             self.log.info('Consider using MusicBrainzIdentifier for your music library')
192             self.use_mbid = Meta.use_mbid = False
193         self._reset_cache()
194     # ######### / Overriding MPDClient #########
195
196     def _reset_cache(self):
197         """
198         Both flushes and instantiates _cache
199
200         * artists: all artists
201         * nombid_artists: artists with no mbid (set only when self.use_mbid is True)
202         * artist_tracks: caching last artist tracks, used in search_track
203         """
204         if isinstance(self._cache, dict):
205             self.log.info('Player: Flushing cache!')
206         else:
207             self.log.info('Player: Initialising cache!')
208         self._cache = {'artists': frozenset(),
209                        'nombid_artists': frozenset(),
210                        'artist_tracks': {}}
211         self._cache['artists'] = frozenset(filter(None, self.list('artist')))
212         if self.use_mbid:
213             artists = self.list('artist', "(MUSICBRAINZ_ARTISTID == '')")
214             self._cache['nombid_artists'] = frozenset(filter(None, artists))
215
216     def _skipped_track(self, previous):
217         if (self.state == 'stop'
218                 or not hasattr(previous, 'id')
219                 or not hasattr(self.current, 'id')):
220             return False
221         return self.current.id != previous.id  # pylint: disable=no-member
222
223     def monitor(self):
224         """Monitor player for change
225         Returns a list a events among:
226
227             * database  player media library has changed
228             * playlist  playlist modified
229             * options   player options changed: repeat mode, etc…
230             * player    player state changed: paused, stopped, skip track…
231             * skipped   current track skipped
232         """
233         curr = self.current
234         select_timeout = 5
235         while True:
236             self.send_idle('database', 'playlist', 'player', 'options')
237             _read, _, _ = select([self], [], [], select_timeout)
238             if _read:  # tries to read response
239                 ret = self.fetch_idle()
240                 if self._skipped_track(curr):
241                     ret.append('skipped')
242                 if 'database' in ret:
243                     self._reset_cache()
244                 return ret
245             else:
246                 try:  # noidle cmd does not go through __getattr__, need to catch OSError then
247                     self.noidle()
248                 except OSError as err:
249                     raise PlayerError(err)
250
251     def clean(self):
252         """Clean blocking event (idle) and pending commands
253         """
254         if 'idle' in self._pending:
255             self.noidle()
256         elif self._pending:
257             self.log.warning('pending commands: %s', self._pending)
258
259     def add(self, payload):
260         """Overriding MPD's add method to accept Track objects
261
262         :param Track,list payload: Either a single track or a list of it
263         """
264         if isinstance(payload, Track):
265             super().__getattr__('add')(payload.file)
266         elif isinstance(payload, list):
267             self.command_list_ok_begin()
268             map(self.add, payload)
269             self.command_list_end()
270         else:
271             self.log.error('Cannot add %s', payload)
272
273     # ######### Properties #####################
274     @property
275     def current(self):
276         return self.currentsong()
277
278     @property
279     def playlist(self):
280         """
281         Override deprecated MPD playlist command
282         """
283         return self.playlistinfo()
284
285     @property
286     def playmode(self):
287         plm = {'repeat': None, 'single': None,
288                'random': None, 'consume': None, }
289         for key, val in self.status().items():
290             if key in plm.keys():
291                 plm.update({key: bool(int(val))})
292         return plm
293
294     @property
295     def queue(self):
296         plst = self.playlist
297         curr_position = int(self.current.pos)
298         plst.reverse()
299         return [trk for trk in plst if int(trk.pos) > curr_position]
300
301     @property
302     def state(self):
303         """Returns (play|stop|pause)"""
304         return str(self.status().get('state'))
305     # ######### / Properties ###################
306
307 # #### find_tracks ####
308     def find_tracks(self, what):
309         """Find tracks for a specific artist or album
310             >>> player.find_tracks(Artist('Nirvana'))
311             >>> player.find_tracks(Album('In Utero', artist=Artist('Nirvana'))
312
313         :param Artist,Album what: Artist or Album to fetch track from
314         :return: A list of track objects
315         :rtype: list(Track)
316         """
317         if isinstance(what, Artist):
318             return self._find_art(what)
319         if isinstance(what, Album):
320             return self._find_alb(what)
321         if isinstance(what, str):
322             return self.find_tracks(Artist(name=what))
323         raise PlayerError('Bad input argument')
324
325     def _find_art(self, artist):
326         tracks = set()
327         # artist blocklist
328         if self.database.get_bl_artist(artist, add=False):
329             self.log.info('Artist in blocklist: %s', artist)
330             return []
331         if artist.mbid:
332             tracks |= set(self.find('musicbrainz_artistid', artist.mbid))
333         for name in artist.names:
334             tracks |= set(self.find('artist', name))
335         # album blocklist
336         albums = {Album(trk.Album.name, mbid=trk.musicbrainz_albumid)
337                   for trk in tracks}
338         bl_albums = {Album(a.get('album'), mbid=a.get('musicbrainz_album'))
339                      for a in self.database.view_bl() if a.get('album')}
340         if albums & bl_albums:
341             self.log.info('Albums in blocklist for %s: %s', artist, albums & bl_albums)
342             tracks = {trk for trk in tracks if trk.Album not in bl_albums}
343         # track blocklist
344         bl_tracks = {Track(title=t.get('title'), file=t.get('file'))
345                      for t in self.database.view_bl() if t.get('title')}
346         if tracks & bl_tracks:
347             self.log.info('Tracks in blocklist for %s: %s',
348                           artist, tracks & bl_tracks)
349             tracks = {trk for trk in tracks if trk not in bl_tracks}
350         return list(tracks)
351
352     def _find_alb(self, album):
353         if not hasattr(album, 'artist'):
354             raise PlayerError('Album object have no artist attribute')
355         if self.database.get_bl_album(album, add=False):
356             self.log.info('Album in blocklist: %s', album)
357             return []
358         albums = []
359         if album.mbid:
360             filt = f"(MUSICBRAINZ_ALBUMID == '{album.mbid}')"
361             albums = self.find(filt)
362         # Now look for album with no MusicBrainzIdentifier
363         if not albums and album.Artist.mbid:  # Use album artist MBID if possible
364             filt = f"((MUSICBRAINZ_ALBUMARTISTID == '{album.Artist.mbid}') AND (album == '{album.name_sz}'))"
365             albums = self.find(filt)
366         if not albums:  # Falls back to (album)?artist/album name
367             for artist in album.Artist.names_sz:
368                 filt = f"((albumartist == '{artist}') AND (album == '{album.name_sz}'))"
369                 albums.extend(self.find(filt))
370         return albums
371 # #### / find_tracks ##
372
373 # #### Search Methods #####
374     def _find_musicbrainz_artistid(self, artist):
375         """Find MusicBrainzArtistID when possible.
376         """
377         if not self.use_mbid:
378             return None
379         mbids = None
380         for name in artist.names_sz:
381             filt = f'((artist == "{name}") AND (MUSICBRAINZ_ARTISTID != ""))'
382             mbids = self.list('MUSICBRAINZ_ARTISTID', filt)
383             if mbids:
384                 break
385         if not mbids:
386             return None
387         if len(mbids) > 1:
388             self.log.debug("Got multiple MBID for artist: %r", artist)
389             return None
390         if artist.mbid:
391             if artist.mbid != mbids[0]:
392                 self.log('MBID discrepancy, %s found with %s (instead of %s)',
393                          artist.name, mbids[0], artist.mbid)
394         else:
395             return mbids[0]
396
397     @bl_artist
398     @set_artist_mbid
399     def search_artist(self, artist):
400         """
401         Search artists based on a fuzzy search in the media library
402             >>> art = Artist(name='the beatles', mbid=<UUID4>) # mbid optional
403             >>> bea = player.search_artist(art)
404             >>> print(bea.names)
405             >>> ['The Beatles', 'Beatles', 'the beatles']
406
407         :param Artist artist: Artist to look for in MPD music library
408         :return: Artist object
409         :rtype: Artist
410         """
411         found = False
412         if artist.mbid:
413             # look for exact search w/ musicbrainz_artistid
414             library = self.list('artist', f"(MUSICBRAINZ_ARTISTID == '{artist.mbid}')")
415             if library:
416                 found = True
417                 self.log.trace('Found mbid "%r" in library', artist)
418                 # library could fetch several artist name for a single MUSICBRAINZ_ARTISTID
419                 if len(library) > 1:
420                     self.log.debug('I got "%s" searching for %r', library, artist)
421                     for name in library:
422                         if SimaStr(artist.name) == name and name != artist.name:
423                             self.log.debug('add alias for %s: %s', artist, name)
424                             artist.add_alias(name)
425                 elif len(library) == 1 and library[0] != artist.name:
426                     new_alias = artist.name
427                     self.log.info('Update artist name %s->%s', artist, library[0])
428                     self.log.debug('Also add alias for %s: %s', artist, new_alias)
429                     artist = Artist(name=library[0], mbid=artist.mbid)
430                     artist.add_alias(new_alias)
431             # Fetches remaining artists for potential match
432             artists = self._cache['nombid_artists']
433         else:  # not using MusicBrainzIDs
434             artists = self._cache['artists']
435         match = get_close_matches(artist.name, artists, 50, 0.73)
436         if not match and not found:
437             return None
438         if len(match) > 1:
439             self.log.debug('found close match for "%s": %s',
440                            artist, '/'.join(match))
441         # First lowercased comparison
442         for close_art in match:
443             # Regular lowered string comparison
444             if artist.name.lower() == close_art.lower():
445                 artist.add_alias(close_art)
446                 found = True
447                 if artist.name != close_art:
448                     self.log.debug('"%s" matches "%s".', close_art, artist)
449         # Does not perform fuzzy matching on short and single word strings
450         # Only lowercased comparison
451         if ' ' not in artist.name and len(artist.name) < 8:
452             self.log.trace('no fuzzy matching for %r', artist)
453             if found:
454                 return artist
455             return None
456         # Now perform fuzzy search
457         for fuzz in match:
458             if fuzz in artist.names:  # Already found in lower cased comparison
459                 continue
460             # SimaStr string __eq__ (not regular string comparison here)
461             if SimaStr(artist.name) == fuzz:
462                 found = True
463                 artist.add_alias(fuzz)
464                 self.log.debug('"%s" quite probably matches "%s" (SimaStr)',
465                                fuzz, artist)
466         if found:
467             if artist.aliases:
468                 self.log.info('Found aliases: %s', '/'.join(artist.names))
469             return artist
470         return None
471
472     def search_track(self, artist, title):
473         """Fuzzy search of title by an artist
474         """
475         cache = self._cache.get('artist_tracks').get(artist)
476         # Retrieve all tracks from artist
477         all_tracks = cache or self.find_tracks(artist)
478         if not cache:
479             self._cache['artist_tracks'] = {}  # clean up
480             self._cache.get('artist_tracks')[artist] = all_tracks
481         # Get all titles (filter missing titles set to 'None')
482         all_artist_titles = frozenset([tr.title for tr in all_tracks
483                                        if tr.title is not None])
484         match = get_close_matches(title, all_artist_titles, 50, 0.78)
485         tracks = []
486         if not match:
487             return []
488         for mtitle in match:
489             leven = levenshtein_ratio(title, mtitle)
490             if leven == 1:
491                 tracks.extend([t for t in all_tracks if t.title == mtitle])
492             elif leven >= 0.77:
493                 self.log.debug('title: "%s" should match "%s" (lr=%1.3f)',
494                                mtitle, title, leven)
495                 tracks.extend([t for t in all_tracks if t.title == mtitle])
496             else:
497                 self.log.debug('title: "%s" does not match "%s" (lr=%1.3f)',
498                                mtitle, title, leven)
499         return tracks
500
501     def search_albums(self, artist):
502         """Find potential albums for "artist"
503
504         * Fetch all albums for "AlbumArtist" == artist
505           → falls back to "Artist" == artist when no "AlbumArtist" tag is set
506         * Tries to filter some mutli-artists album
507           For instance an album by Artist_A may have a track by Artist_B. Then
508           looking for albums for Artist_B wrongly returns this album.
509         """
510         # First, look for all potential albums
511         self.log.debug('Searching album for "%r"', artist)
512         if artist.aliases:
513             self.log.debug('Searching album for %s aliases: "%s"',
514                            artist, artist.aliases)
515         albums = set()
516         if self.use_mbid and artist.mbid:
517             mpd_filter = f"((musicbrainz_albumartistid == '{artist.mbid}') AND ( album != ''))"
518             raw_album_id = self.list('musicbrainz_albumid', mpd_filter)
519             for albumid in raw_album_id:
520                 mpd_filter = f"((musicbrainz_albumid == '{albumid}') AND ( album != ''))"
521                 album_name = self.list('album', mpd_filter)
522                 if not album_name:  # something odd here
523                     continue
524                 albums.add(Album(album_name[0], artist=artist.name,
525                                  Artist=artist, mbid=albumid))
526         for name_sz in artist.names_sz:
527             mpd_filter = f"((albumartist == '{name_sz}') AND ( album != ''))"
528             raw_albums = self.list('album', mpd_filter)
529             for alb in raw_albums:
530                 if alb in [a.name for a in albums]:
531                     continue
532                 mbid = None
533                 if self.use_mbid:
534                     _ = Album(alb)
535                     mpd_filter = f"((albumartist == '{artist.name_sz}') AND ( album == '{_.name_sz}'))"
536                     mbids = self.list('MUSICBRAINZ_ALBUMID', mpd_filter)
537                     if mbids:
538                         mbid = mbids[0]
539                 albums.add(Album(alb, artist=artist.name,
540                                  Artist=artist, mbid=mbid))
541         candidates = []
542         for album in albums:
543             album_trks = self.find_tracks(album)
544             if not album_trks:  # find_track result can be empty, blocklist applied
545                 continue
546             album_artists = {tr.albumartist for tr in album_trks if tr.albumartist}
547             if album.Artist.names & album_artists:
548                 candidates.append(album)
549                 continue
550             if self.use_mbid and artist.mbid:
551                 if artist.mbid == album_trks[0].musicbrainz_albumartistid:
552                     candidates.append(album)
553                     continue
554                 else:
555                     self.log.debug('Discarding "%s", "%r" not set as musicbrainz_albumartistid', album, album.Artist)
556                     continue
557             if 'Various Artists' in album_artists:
558                 self.log.debug('Discarding %s ("Various Artists" set)', album)
559                 continue
560             if album_artists and album.Artist.name not in album_artists:
561                 self.log.debug('Discarding "%s", "%s" not set as albumartist', album, album.Artist)
562                 continue
563             # Attempt to detect false positive (especially when no
564             # AlbumArtist/MBIDs tag ar set)
565             # Avoid selecting albums where artist is credited for a single
566             # track of the album
567             album_trks = self.find(f"(album == '{album.name_sz}')")
568             arts = [trk.artist for trk in album_trks]  # Artists in the album
569             # count artist occurences
570             ratio = arts.count(album.Artist.name)/len(arts)
571             if ratio >= 0.8:
572                 candidates.append(album)
573             else:
574                 self.log.debug('"%s" probably not an album of "%s" (ratio=%.2f)',
575                                album, artist, ratio)
576             continue
577         return candidates
578 # #### / Search Methods ###
579
580 # VIM MODLINE
581 # vim: ai ts=4 sw=4 sts=4 expandtab