diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index 96a7794e..f0842fa7 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -245,20 +245,21 @@ class MpdContext(object): self.core = core self.events = set() self.subscriptions = set() - self.to_uri = dict() - self.from_uri = dict() - self.refresh_maps() + self.to_uri = {} + self.from_uri = {} + self.refresh_playlists_mapping() - def refresh_maps(self): - self.to_uri.clear() - self.from_uri.clear() - for playlist in self.core.playlists.playlists.get(): - if not playlist.name: - continue - name = playlist.name - i = 1 - while name in self.to_uri: - name = '%s [%d]' % playlist.name % i - i += 1 - self.to_uri[name] = playlist.uri - self.from_uri[playlist.uri] = name + def refresh_playlists_mapping(self): + if self.core is not None: + self.to_uri.clear() + self.from_uri.clear() + for playlist in self.core.playlists.playlists.get(): + if not playlist.name: + continue + name = playlist.name + i = 1 + while name in self.to_uri: + name = '%s [%d]' % playlist.name % i + i += 1 + self.to_uri[name] = playlist.uri + self.from_uri[playlist.uri] = name diff --git a/mopidy/frontends/mpd/protocol/music_db.py b/mopidy/frontends/mpd/protocol/music_db.py index efb9a0fd..be641467 100644 --- a/mopidy/frontends/mpd/protocol/music_db.py +++ b/mopidy/frontends/mpd/protocol/music_db.py @@ -381,8 +381,11 @@ def searchaddpl(context, playlist_name, mpd_query): return results = context.core.library.search(**query).get() + if len(context.to_uri) == 0: + context.refresh_playlists_mapping() + if playlist_name in context.to_uri: - playlist = context.core.playlists.lookup(context.to_uri[playlist_name]) + playlist = context.core.playlists.lookup(context.to_uri[playlist_name]).get() else: playlist = context.core.playlists.create(playlist_name).get() tracks = list(playlist.tracks) + _get_tracks(results) diff --git a/mopidy/frontends/mpd/protocol/stored_playlists.py b/mopidy/frontends/mpd/protocol/stored_playlists.py index 4ec2f40c..5d43bc3c 100644 --- a/mopidy/frontends/mpd/protocol/stored_playlists.py +++ b/mopidy/frontends/mpd/protocol/stored_playlists.py @@ -81,7 +81,7 @@ def listplaylists(context): if not playlist.name: continue if playlist.uri not in context.from_uri: - context.refresh_maps() # the maps are not synced, we refresh them + context.refresh_playlists_mapping() # the maps are not synced, we refresh them result.append(('playlist', context.from_uri[playlist.uri])) last_modified = ( playlist.last_modified or dt.datetime.utcnow()).isoformat()