mpd: playlist name disambiguation
This commit is contained in:
parent
837db09aff
commit
87ce309749
@ -232,9 +232,33 @@ class MpdContext(object):
|
|||||||
#: The subsytems that we want to be notified about in idle mode.
|
#: The subsytems that we want to be notified about in idle mode.
|
||||||
subscriptions = None
|
subscriptions = None
|
||||||
|
|
||||||
|
#: a map from playlists printing names to uris (necessary as mpd requires
|
||||||
|
#: playlists names to be unique)
|
||||||
|
to_uri = None
|
||||||
|
|
||||||
|
#: the invert map (uri to printing name)
|
||||||
|
from_uri = None
|
||||||
|
|
||||||
def __init__(self, dispatcher, session=None, core=None):
|
def __init__(self, dispatcher, session=None, core=None):
|
||||||
self.dispatcher = dispatcher
|
self.dispatcher = dispatcher
|
||||||
self.session = session
|
self.session = session
|
||||||
self.core = core
|
self.core = core
|
||||||
self.events = set()
|
self.events = set()
|
||||||
self.subscriptions = set()
|
self.subscriptions = set()
|
||||||
|
self.to_uri = dict()
|
||||||
|
self.from_uri = dict()
|
||||||
|
self.refresh_maps()
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@ -381,9 +381,8 @@ def searchaddpl(context, playlist_name, mpd_query):
|
|||||||
return
|
return
|
||||||
results = context.core.library.search(**query).get()
|
results = context.core.library.search(**query).get()
|
||||||
|
|
||||||
playlists = context.core.playlists.filter(name=playlist_name).get()
|
if playlist_name in context.to_uri:
|
||||||
if playlists:
|
playlist = context.core.playlists.lookup(context.to_uri[playlist_name])
|
||||||
playlist = playlists[0]
|
|
||||||
else:
|
else:
|
||||||
playlist = context.core.playlists.create(playlist_name).get()
|
playlist = context.core.playlists.create(playlist_name).get()
|
||||||
tracks = list(playlist.tracks) + _get_tracks(results)
|
tracks = list(playlist.tracks) + _get_tracks(results)
|
||||||
|
|||||||
@ -80,7 +80,9 @@ def listplaylists(context):
|
|||||||
for playlist in context.core.playlists.playlists.get():
|
for playlist in context.core.playlists.playlists.get():
|
||||||
if not playlist.name:
|
if not playlist.name:
|
||||||
continue
|
continue
|
||||||
result.append(('playlist', playlist.name))
|
if playlist.uri not in context.from_uri:
|
||||||
|
context.refresh_maps() # the maps are not synced, we refresh them
|
||||||
|
result.append(('playlist', context.from_uri[playlist.uri]))
|
||||||
last_modified = (
|
last_modified = (
|
||||||
playlist.last_modified or dt.datetime.utcnow()).isoformat()
|
playlist.last_modified or dt.datetime.utcnow()).isoformat()
|
||||||
# Remove microseconds
|
# Remove microseconds
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user