]> kaliko git repositories - mpd-sima.git/blob - sima/mpdclient.py
MPD client: Use tagtypes to control and set tags
[mpd-sima.git] / sima / mpdclient.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009-2020 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 itertools import dropwhile
23
24 # external module
25 from musicpd import MPDClient, MPDError
26
27
28 # local import
29 from .lib.meta import Meta, Artist, Album
30 from .lib.track import Track
31 from .lib.simastr import SimaStr
32 from .utils.leven import levenshtein_ratio
33
34
35 class PlayerError(Exception):
36     """Fatal error in poller."""
37
38
39 # Some decorators
40 def bl_artist(func):
41     def wrapper(*args, **kwargs):
42         cls = args[0]
43         if not cls.database:
44             return func(*args, **kwargs)
45         result = func(*args, **kwargs)
46         if not result:
47             return
48         names = list()
49         for art in result.names:
50             if cls.database.get_bl_artist(art, add_not=True):
51                 cls.log.debug('Blacklisted "%s"', art)
52                 continue
53             names.append(art)
54         if not names:
55             return
56         resp = Artist(name=names.pop(), mbid=result.mbid)
57         for name in names:
58             resp.add_alias(name)
59         return resp
60     return wrapper
61
62 def tracks_wrapper(func):
63     @wraps(func)
64     def wrapper(*args, **kwargs):
65         ret = func(*args, **kwargs)
66         if isinstance(ret, dict):
67             return Track(**ret)
68         elif isinstance(ret, list):
69             return [Track(**t) for t in ret]
70     return wrapper
71 # / decorators
72
73 def blacklist(artist=False, album=False, track=False):
74     #pylint: disable=C0111,W0212
75     field = (album, track)
76     def decorated(func):
77         def wrapper(*args, **kwargs):
78             if not args[0].database:
79                 return func(*args, **kwargs)
80             cls = args[0]
81             boolgen = (bl for bl in field)
82             bl_fun = (cls.database.get_bl_album,
83                       cls.database.get_bl_track,)
84             #bl_getter = next(fn for fn, bl in zip(bl_fun, boolgen) if bl is True)
85             bl_getter = next(dropwhile(lambda _: not next(boolgen), bl_fun))
86             #cls.log.debug('using {0} as bl filter'.format(bl_getter.__name__))
87             results = list()
88             for elem in func(*args, **kwargs):
89                 if bl_getter(elem, add_not=True):
90                     #cls.log.debug('Blacklisted "{0}"'.format(elem))
91                     continue
92                 if track and cls.database.get_bl_album(elem, add_not=True):
93                     # filter album as well in track mode
94                     # (artist have already been)
95                     cls.log.debug('Blacklisted alb. "%s"', elem)
96                     continue
97                 results.append(elem)
98             return results
99         return wrapper
100     return decorated
101
102
103 class MPD(MPDClient):
104     """
105     Player instance inheriting from MPDClient (python-musicpd).
106
107     Some methods are overridden to format objects as sima.lib.Track for
108     instance, other are calling parent class directly through super().
109     cf. MPD.__getattr__
110
111     .. note::
112
113         * find methods are looking for exact match of the object provided attributes in MPD music library
114         * search methods are looking for exact match + fuzzy match.
115     """
116     needed_cmds = ['status', 'stats', 'add', 'find',
117                    'search', 'currentsong', 'ping']
118     needed_mbid_tags = {'Artist', 'Album', 'AlbumArtist',
119                         'Title', 'Track', 'Genre',
120                         'MUSICBRAINZ_ARTISTID', 'MUSICBRAINZ_ALBUMID',
121                         'MUSICBRAINZ_ALBUMARTISTID', 'MUSICBRAINZ_TRACKID'}
122     database = None
123
124     def __init__(self, daemon):
125         super().__init__()
126         self.use_mbid = True
127         self.daemon = daemon
128         self.log = daemon.log
129         self.config = self.daemon.config['MPD']
130         self._cache = None
131
132     # ######### Overriding MPDClient ###########
133     def __getattr__(self, cmd):
134         """Wrapper around MPDClient calls for abstract overriding"""
135         track_wrapped = {
136                          'currentsong',
137                          'find',
138                          'playlistinfo',
139                          }
140         if cmd in track_wrapped:
141             return tracks_wrapper(super().__getattr__(cmd))
142         return super().__getattr__(cmd)
143
144     def disconnect(self):
145         """Overriding explicitly MPDClient.disconnect()"""
146         if self._sock:
147             super().disconnect()
148
149     def connect(self):
150         """Overriding explicitly MPDClient.connect()"""
151         # host, port, password
152         host = self.config.get('host')
153         port = self.config.get('port')
154         password = self.config.get('password', fallback=None)
155         self.disconnect()
156         try:
157             super().connect(host, port)
158         # Catch socket errors
159         except IOError as err:
160             raise PlayerError('Could not connect to "%s:%s": %s' %
161                               (host, port, err.strerror))
162         # Catch all other possible errors
163         # ConnectionError and ProtocolError are always fatal.  Others may not
164         # be, but we don't know how to handle them here, so treat them as if
165         # they are instead of ignoring them.
166         except MPDError as err:
167             raise PlayerError('Could not connect to "%s:%s": %s' %
168                               (host, port, err))
169         if password:
170             try:
171                 self.password(password)
172             except (MPDError, IOError) as err:
173                 raise PlayerError("Could not connect to '%s': %s", (host, err))
174         # Controls we have sufficient rights
175         available_cmd = self.commands()
176         for cmd in MPD.needed_cmds:
177             if cmd not in available_cmd:
178                 self.disconnect()
179                 raise PlayerError('Could connect to "%s", '
180                                   'but command "%s" not available' %
181                                   (host, cmd))
182         # Controls use of MusicBrainzIdentifier
183         self.tagtypes('clear')
184         for tag in MPD.needed_mbid_tags:
185             self.tagtypes('enable', tag)
186         if self.daemon.config.get('sima', 'musicbrainzid'):
187             tt = set(self.tagtypes())
188             if len(MPD.needed_mbid_tags & tt) != len(MPD.needed_mbid_tags):
189                 self.log.warning('Use of MusicBrainzIdentifier is set but MPD '
190                                  'is not providing related metadata')
191                 self.log.info(tt)
192                 self.log.warning('Disabling MusicBrainzIdentifier')
193                 self.use_mbid = Meta.use_mbid = False
194             else:
195                 self.log.debug('Available metadata: %s', tt)
196                 self.use_mbid = Meta.use_mbid = True
197         else:
198             self.log.warning('Use of MusicBrainzIdentifier disabled!')
199             self.log.info('Consider using MusicBrainzIdentifier for your music library')
200             self.use_mbid = Meta.use_mbid = False
201         self._reset_cache()
202     # ######### / Overriding MPDClient #########
203
204     def _reset_cache(self):
205         """
206         Both flushes and instantiates _cache
207         """
208         if isinstance(self._cache, dict):
209             self.log.info('Player: Flushing cache!')
210         else:
211             self.log.info('Player: Initialising cache!')
212         self._cache = {'artists': frozenset(),
213                        'nombid_artists': frozenset()}
214         self._cache['artists'] = frozenset(filter(None, self.list('artist')))
215         if Artist.use_mbid:
216             artists = self.list('artist', "(MUSICBRAINZ_ARTISTID == '')")
217             self._cache['nombid_artists'] = frozenset(filter(None, artists))
218
219     def _skipped_track(self, previous):
220         if (self.state == 'stop'
221                 or not hasattr(previous, 'id')
222                 or not hasattr(self.current, 'id')):
223             return False
224         return self.current.id != previous.id  # pylint: disable=no-member
225
226     def monitor(self):
227         """OLD socket Idler
228         Monitor player for change
229         Returns a list a events among:
230
231             * database  player media library has changed
232             * playlist  playlist modified
233             * options   player options changed: repeat mode, etc…
234             * player    player state changed: paused, stopped, skip track…
235             * skipped   current track skipped
236         """
237         curr = self.current
238         try:
239             ret = self.idle('database', 'playlist', 'player', 'options')
240         except (MPDError, IOError) as err:
241             raise PlayerError("Couldn't init idle: %s" % err)
242         if self._skipped_track(curr):
243             ret.append('skipped')
244         if 'database' in ret:
245             self._reset_cache()
246         return ret
247
248     def clean(self):
249         """Clean blocking event (idle) and pending commands
250         """
251         if 'idle' in self._pending:
252             self.noidle()
253         elif self._pending:
254             self.log.warning('pending commands: %s', self._pending)
255
256     def add(self, payload):
257         """Overriding MPD's add method to accept Track objects"""
258         if isinstance(payload, Track):
259             super().__getattr__('add')(payload.file)
260         elif isinstance(payload, list):
261             self.command_list_ok_begin()
262             for tr in payload:
263                 self.add(tr)
264             self.command_list_end()
265         else:
266             self.log.error('Cannot add %s', payload)
267
268     # ######### Properties #####################
269     @property
270     def current(self):
271         return self.currentsong()
272
273     @property
274     def playlist(self):
275         """
276         Override deprecated MPD playlist command
277         """
278         return self.playlistinfo()
279
280     @property
281     def playmode(self):
282         plm = {'repeat': None, 'single': None,
283                'random': None, 'consume': None, }
284         for key, val in self.status().items():
285             if key in plm.keys():
286                 plm.update({key: bool(int(val))})
287         return plm
288
289     @property
290     def queue(self):
291         plst = self.playlist
292         curr_position = int(self.current.pos)
293         plst.reverse()
294         return [trk for trk in plst if int(trk.pos) > curr_position]
295
296     @property
297     def state(self):
298         """Returns (play|stop|pause)"""
299         return str(self.status().get('state'))
300     # ######### / Properties ###################
301
302 # #### find_tracks ####
303     def find_tracks(self, what):
304         """Find tracks for a specific artist or album
305             >>> player.find_tracks(Artist('Nirvana'))
306             >>> player.find_tracks(Album('In Utero', artist=(Artist('Nirvana'))
307
308         :param Artist,Album what: Artist or Album to fetch track from
309
310         Returns a list of :py:obj:Track objects
311         """
312         if isinstance(what, Artist):
313             return self._find_art(what)
314         elif isinstance(what, Album):
315             return self._find_alb(what)
316         elif isinstance(what, str):
317             return self.find_tracks(Artist(name=what))
318
319     def _find_art(self, artist):
320         tracks = set()
321         if artist.mbid:
322             tracks |= set(self.find('musicbrainz_artistid', artist.mbid))
323         for name in artist.names_sz:
324             tracks |= set(self.find('artist', name))
325         return list(tracks)
326
327     def _find_alb(self, album):
328         if not hasattr(album, 'artist'):
329             raise PlayerError('Album object have no artist attribute')
330         albums = []
331         if self.use_mbid and album.mbid:
332             filt = f'(MUSICBRAINZ_ALBUMID == {album.mbid})'
333             albums = self.find(filt)
334         # Now look for album with no MusicBrainzIdentifier
335         if not albums and album.artist.mbid and self.use_mbid:  # Use album artist MBID if possible
336             filt = f"((MUSICBRAINZ_ALBUMARTISTID == '{album.artist.mbid}') AND (album == '{album.name_sz}'))"
337             albums = self.find(filt)
338         if not albums:  # Falls back to (album)?artist/album name
339             filt = f"((albumartist == '{album.artist!s}') AND (album == '{album.name_sz}'))"
340             albums = self.find(filt)
341         return albums
342 # #### / find_tracks ##
343
344 # #### Search Methods #####
345     @bl_artist
346     def search_artist(self, artist):
347         """
348         Search artists based on a fuzzy search in the media library
349             >>> art = Artist(name='the beatles', mbid=<UUID4>) # mbid optional
350             >>> bea = player.search_artist(art)c
351             >>> print(bea.names)
352             >>> ['The Beatles', 'Beatles', 'the beatles']
353
354         :param Artist artist: Artist to look for in MPD music library
355
356         Returns an Artist object
357         """
358         found = False
359         if self.use_mbid and artist.mbid:
360             # look for exact search w/ musicbrainz_artistid
361             found = bool(self.list('artist', f"(MUSICBRAINZ_ARTISTID == '{artist.mbid}')"))
362             if found:
363                 self.log.trace('Found mbid "%r" in library', artist)
364             # Fetches remaining artists for potential match
365             artists = self._cache['nombid_artists']
366         else:  # not using MusicBrainzIDs
367             artists = self._cache['artists']
368         match = get_close_matches(artist.name, artists, 50, 0.73)
369         if not match and not found:
370             return None
371         if len(match) > 1:
372             self.log.debug('found close match for "%s": %s', artist, '/'.join(match))
373         # Forst lowercased comparison
374         for close_art in match:
375             # Regular lowered string comparison
376             if artist.name.lower() == close_art.lower():
377                 artist.add_alias(close_art)
378                 found = True
379                 if artist.name != close_art:
380                     self.log.debug('"%s" matches "%s".', close_art, artist)
381         # Does not perform fuzzy matching on short and single word strings
382         # Only lowercased comparison
383         if ' ' not in artist.name and len(artist.name) < 8:
384             self.log.trace('no fuzzy matching for %r', artist)
385             if found:
386                 return artist
387         # Now perform fuzzy search
388         for fuzz in match:
389             if fuzz in artist.names:  # Already found in lower cased comparison
390                 continue
391             # SimaStr string __eq__ (not regular string comparison here)
392             if SimaStr(artist.name) == fuzz:
393                 found = True
394                 artist.add_alias(fuzz)
395                 self.log.info('"%s" quite probably matches "%s" (SimaStr)',
396                               fuzz, artist)
397         if found:
398             if artist.aliases:
399                 self.log.debug('Found aliases: %s', '/'.join(artist.names))
400             return artist
401         return None
402
403     @blacklist(track=True)
404     def search_track(self, artist, title):
405         """Fuzzy search of title by an artist
406         """
407         # Retrieve all tracks from artist
408         all_tracks = self.find_tracks(artist)
409         # Get all titles (filter missing titles set to 'None')
410         all_artist_titles = frozenset([tr.title for tr in all_tracks
411                                        if tr.title is not None])
412         match = get_close_matches(title, all_artist_titles, 50, 0.78)
413         if not match:
414             return []
415         for mtitle in match:
416             leven = levenshtein_ratio(title.lower(), mtitle.lower())
417             if leven == 1:
418                 pass
419             elif leven >= 0.79:  # PARAM
420                 self.log.debug('title: "%s" should match "%s" (lr=%1.3f)',
421                                mtitle, title, leven)
422             else:
423                 self.log.debug('title: "%s" does not match "%s" (lr=%1.3f)',
424                                mtitle, title, leven)
425                 return []
426             return self.find('artist', artist, 'title', mtitle)
427
428     @blacklist(album=True)
429     def search_albums(self, artist):
430         """Find potential albums for "artist"
431
432         * Fetch all albums for "AlbumArtist" == artist
433           → falls back to "Artist" == artist when no "AlbumArtist" tag is set
434         * Tries to filter some mutli-artists album
435           For instance an album by Artist_A may have a track by Artist_B. Then
436           looking for albums for Artist_B returns wrongly this album.
437         """
438         # First, look for all potential albums
439         self.log.debug('Searching album for "%s"', artist)
440         if artist.aliases:
441             self.log.debug('Searching album for %s aliases: "%s"',
442                            artist, artist.aliases)
443         for name_sz in artist.names_sz:
444             raw_albums = self.list('album', f"( albumartist == '{name_sz}')")
445             albums = [Album(a, albumartist=artist.name, artist=artist) for a in raw_albums if a]
446         candidates = []
447         for album in albums:
448             album_trks = self.find_tracks(album)
449             album_artists = {tr.albumartist for tr in album_trks if tr.albumartist}
450             if album.artist.names & album_artists:
451                 candidates.append(album)
452                 continue
453             if 'Various Artists' in album_artists:
454                 self.log.debug('Discarding %s ("Various Artists" set)', album)
455                 continue
456             if album_artists and album.artist.name not in album_artists:
457                 self.log.debug('Discarding "%s", "%s" not set as albumartist', album, album.artist)
458                 continue
459             # Attempt to detect false positive
460             # Avoid selecting albums where artist is credited for a single
461             # track of the album
462             album_trks = self.find(f"(album == '{album.name_sz}')")
463             arts = [trk.artist for trk in album_trks]  # Artists in the album
464             # count artist occurences
465             ratio = arts.count(album.artist.name)/len(arts)
466             if ratio >= 0.8:
467                 candidates.append(album)
468             else:
469                 self.log.debug('"%s" probably not an album of "%s" (ratio=%.2f)',
470                                album, artist, ratio)
471             continue
472         return candidates
473 # #### / Search Methods ###
474
475 # VIM MODLINE
476 # vim: ai ts=4 sw=4 sts=4 expandtab