diff --git a/mopidy_subidy/library.py b/mopidy_subidy/library.py index bc5e7c2..c7a0dc7 100644 --- a/mopidy_subidy/library.py +++ b/mopidy_subidy/library.py @@ -15,6 +15,7 @@ class SubidyLibraryProvider(backend.LibraryProvider): dict(id="albums", name="Albums"), dict(id="rootdirs", name="Directories"), dict(id="random", name="Random"), + dict(id="randomalbum", name="Random Albums"), ] # Create a dict with the keys being the `id`s in `vdir_templates` # and the values being objects containing the vdir `id`, @@ -56,6 +57,9 @@ class SubidyLibraryProvider(backend.LibraryProvider): def browse_random_songs(self): return self.subsonic_api.get_random_songs_as_refs() + def browse_random_albums(self): + return self.subsonic_api.get_random_albums_as_refs() + def browse_diritems(self, directory_id): return self.subsonic_api.get_diritems_as_refs(directory_id) @@ -86,7 +90,7 @@ class SubidyLibraryProvider(backend.LibraryProvider): def browse(self, browse_uri): if browse_uri == uri.get_vdir_uri("root"): - root_vdir_names = ["rootdirs", "artists", "albums", "random"] + root_vdir_names = ["rootdirs", "artists", "albums", "random", "randomalbum"] root_vdirs = [ self._vdirs[vdir_name] for vdir_name in root_vdir_names ] @@ -102,6 +106,8 @@ class SubidyLibraryProvider(backend.LibraryProvider): return self.browse_albums() elif browse_uri == uri.get_vdir_uri("random"): return self.browse_random_songs() + elif browse_uri == uri.get_vdir_uri("randomalbum"): + return self.browse_random_albums() else: uri_type = uri.get_type(browse_uri) diff --git a/mopidy_subidy/subsonic_api.py b/mopidy_subidy/subsonic_api.py index 50aacf7..077f0e4 100644 --- a/mopidy_subidy/subsonic_api.py +++ b/mopidy_subidy/subsonic_api.py @@ -429,6 +429,27 @@ class SubsonicApi: return songs return [] + def get_raw_random_album(self, size=MAX_LIST_RESULTS): + try: + response = self.connection.getAlbumList2( + ltype='random', size=size, offset=0 + ) + except Exception: + logger.warning( + "Connecting to subsonic failed when loading random album list." + ) + return [] + if response.get("status") != RESPONSE_OK: + logger.warning( + "Got non-okay status code from subsonic: %s" + % response.get("status") + ) + return [] + albums = response.get("albumList2").get("album") + if albums is not None: + return albums + return [] + def get_more_albums(self, ltype, size=MAX_LIST_RESULTS, offset=0): try: response = self.connection.getAlbumList2( @@ -519,6 +540,11 @@ class SubsonicApi: self.raw_song_to_ref(song) for song in self.get_raw_random_song(75) ] + def get_random_albums_as_refs(self): + return [ + self.raw_album_to_ref_with_artist(album) for album in self.get_raw_random_album(20) + ] + def get_random_songs_as_tracks(self): return [ self.raw_song_to_track(song) for song in self.get_raw_random_song() @@ -615,6 +641,16 @@ class SubsonicApi: uri=uri.get_album_uri(album.get("id")), ) + def raw_album_to_ref_with_artist(self, album): + if album is None: + return None + return Ref.album( + name=album.get("artist") + " - " + album.get("title") + or album.get("artist") + " - " + album.get("name") + or UNKNOWN_ALBUM, + uri=uri.get_album_uri(album.get("id")), + ) + def raw_album_to_album(self, album): if album is None: return None