From de69905c54b468cfa4642bdb09d690ae2928d164 Mon Sep 17 00:00:00 2001 From: hhm Date: Thu, 18 May 2017 23:25:44 -0400 Subject: [PATCH 1/6] B"H add playlist writing code --- mopidy_subidy/playlists.py | 28 ++++++++++++++++++++++++---- mopidy_subidy/subsonic_api.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/mopidy_subidy/playlists.py b/mopidy_subidy/playlists.py index d752131..338fd28 100644 --- a/mopidy_subidy/playlists.py +++ b/mopidy_subidy/playlists.py @@ -1,5 +1,6 @@ from mopidy import backend from mopidy_subidy import uri +from mopidy.models import Playlist import logging logger = logging.getLogger(__name__) @@ -15,10 +16,22 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): return self.playlists def create(self, name): - pass + result = self.subsonic_api.create_playlist_raw(name) + if result is None: + return None + playlist = result.get('playlist') + if playlist is None: + self.refresh() + for pl in self.playlists: + if pl.name == name: + playlist = pl + return playlist + else: + return self.subsonic_api.raw_playlist_to_playlist(playlist) - def delete(self, uri): - pass + def delete(self, playlist_uri): + playlist_id = uri.get_playlist_id(playlist_uri) + self.subsonic_api.delete_playlist_raw(playlist_id) def get_items(self, items_uri): #logger.info('ITEMS %s: %s' % (lookup_uri, self.subsonic_api.get_playlist_songs_as_refs(uri.get_playlist_id(items_uri)))) @@ -32,4 +45,11 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): self.playlists = self.subsonic_api.get_playlists_as_refs() def save(self, playlist): - pass + playlist_id = uri.get_playlist_id(playlist.uri) + track_ids = [] + for trk in playlist.tracks: + track_ids.append(uri.get_song_id(trk.uri)) + result = self.subsonic_api.update_playlist_raw(playlist_id, track_ids) + if result is None: + return None + return playlist diff --git a/mopidy_subidy/subsonic_api.py b/mopidy_subidy/subsonic_api.py index 6ff7d42..25b2f5b 100644 --- a/mopidy_subidy/subsonic_api.py +++ b/mopidy_subidy/subsonic_api.py @@ -101,6 +101,39 @@ class SubsonicApi(): albums=[self.raw_album_to_album(album) for album in result.get('album') or []], tracks=[self.raw_song_to_track(song) for song in result.get('song') or []]) + def create_playlist_raw(self, name): + try: + response = self.connection.createPlaylist(name=name) + except Exception as e: + logger.warning('Connecting to subsonic failed when creating playlist.') + return None + if response.get('status') != RESPONSE_OK: + logger.warning('Got non-okay status code from subsonic: %s' % response.get('status')) + return None + return response + + def delete_playlist_raw(self, playlist_id): + try: + response = self.connection.deletePlaylist(playlist_id) + except Exception as e: + logger.warning('Connecting to subsonic failed when deleting playlist.') + return None + if response.get('status') != RESPONSE_OK: + logger.warning('Got non-okay status code from subsonic: %s' % response.get('status')) + return None + return response + + def update_playlist_raw(self, playlist_id, song_ids): + try: + response = self.connection.updatePlaylist(playlist_id, songIdsToAdd=song_ids) + except Exception as e: + logger.warning('Connecting to subsonic failed when creating playlist.') + return None + if response.get('status') != RESPONSE_OK: + logger.warning('Got non-okay status code from subsonic: %s' % response.get('status')) + return None + return response + def get_raw_artists(self): try: response = self.connection.getArtists() From a2f23c209529de66c3e0de55afd4ad4acacfcdac Mon Sep 17 00:00:00 2001 From: hhm Date: Thu, 18 May 2017 23:42:18 -0400 Subject: [PATCH 2/6] B"H use playlists instead of playlist refs --- mopidy_subidy/playlists.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mopidy_subidy/playlists.py b/mopidy_subidy/playlists.py index 338fd28..4bfde39 100644 --- a/mopidy_subidy/playlists.py +++ b/mopidy_subidy/playlists.py @@ -21,8 +21,7 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): return None playlist = result.get('playlist') if playlist is None: - self.refresh() - for pl in self.playlists: + for pl in self.subsonic_api.get_playlists_as_playlists(): if pl.name == name: playlist = pl return playlist From 364352a7656bdc7055f604fe102649bf4bfa561d Mon Sep 17 00:00:00 2001 From: hhm Date: Thu, 18 May 2017 23:45:46 -0400 Subject: [PATCH 3/6] B"H get list of playlists immediately instead of caching them --- mopidy_subidy/playlists.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mopidy_subidy/playlists.py b/mopidy_subidy/playlists.py index 4bfde39..6a942ad 100644 --- a/mopidy_subidy/playlists.py +++ b/mopidy_subidy/playlists.py @@ -13,7 +13,7 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): self.refresh() def as_list(self): - return self.playlists + return self.subsonic_api.get_playlists_as_refs() def create(self, name): result = self.subsonic_api.create_playlist_raw(name) @@ -41,7 +41,7 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): return self.subsonic_api.get_playlist_as_playlist(uri.get_playlist_id(lookup_uri)) def refresh(self): - self.playlists = self.subsonic_api.get_playlists_as_refs() + pass def save(self, playlist): playlist_id = uri.get_playlist_id(playlist.uri) From 7592036c7fff8319a7c9b0e807eef67539fc26d3 Mon Sep 17 00:00:00 2001 From: hhm Date: Fri, 19 May 2017 02:18:42 -0400 Subject: [PATCH 4/6] B"H rewrite playlist from scratch instead of adding more tracks --- mopidy_subidy/playlists.py | 2 +- mopidy_subidy/subsonic_api.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mopidy_subidy/playlists.py b/mopidy_subidy/playlists.py index 6a942ad..0bb0321 100644 --- a/mopidy_subidy/playlists.py +++ b/mopidy_subidy/playlists.py @@ -48,7 +48,7 @@ class SubidyPlaylistsProvider(backend.PlaylistsProvider): track_ids = [] for trk in playlist.tracks: track_ids.append(uri.get_song_id(trk.uri)) - result = self.subsonic_api.update_playlist_raw(playlist_id, track_ids) + result = self.subsonic_api.save_playlist_raw(playlist_id, track_ids) if result is None: return None return playlist diff --git a/mopidy_subidy/subsonic_api.py b/mopidy_subidy/subsonic_api.py index 25b2f5b..fb82627 100644 --- a/mopidy_subidy/subsonic_api.py +++ b/mopidy_subidy/subsonic_api.py @@ -123,9 +123,9 @@ class SubsonicApi(): return None return response - def update_playlist_raw(self, playlist_id, song_ids): + def save_playlist_raw(self, playlist_id, song_ids): try: - response = self.connection.updatePlaylist(playlist_id, songIdsToAdd=song_ids) + response = self.connection.createPlaylist(playlist_id, songIds=song_ids) except Exception as e: logger.warning('Connecting to subsonic failed when creating playlist.') return None From a7039ba53ca0592169ec15fac55c8945ee7dff4c Mon Sep 17 00:00:00 2001 From: hhm Date: Sat, 20 May 2017 23:35:58 -0400 Subject: [PATCH 5/6] B"H add readme item supported --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 791da2c..c01722f 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,10 @@ The following things are supported: * Searching for any terms * Browsing playlists * Searching explicitly for one of: artists, albums, tracks + * Creating, editing and deleting playlists The following things are **not** supported: - * Creating, editing and deleting playlists * Subsonics smart playlists * Searching for a combination of filters (artist and album, artist and track, etc.) From f70ec43cfbf26a76518041033c5756ccd73bf223 Mon Sep 17 00:00:00 2001 From: hhm Date: Sat, 20 May 2017 23:38:50 -0400 Subject: [PATCH 6/6] B"H readme item merge --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index c01722f..57357b9 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,8 @@ The following things are supported: * Browsing all artists/albums/tracks * Searching for any terms - * Browsing playlists + * Browsing, creating, editing and deleting playlists * Searching explicitly for one of: artists, albums, tracks - * Creating, editing and deleting playlists The following things are **not** supported: