From 80e88928180bc0ef0bbe94beda5d7ef337c40fb0 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Mon, 25 Jul 2022 21:47:52 +0200 Subject: [PATCH] Fix several issues in Browse feature (#439) - fix sort order of items in browse - fix parsing item that has not (yet_ been added to the db - fix relative file paths within playlist --- music_assistant/models/player_queue.py | 5 ++- music_assistant/music_providers/filesystem.py | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/music_assistant/models/player_queue.py b/music_assistant/models/player_queue.py index ff5ae60d..8fea1673 100644 --- a/music_assistant/models/player_queue.py +++ b/music_assistant/models/player_queue.py @@ -687,9 +687,8 @@ class PlayerQueue: return # queue exhausted or player turned off/stopped - if ( - self.stream - and new_state[0] in (PlayerState.IDLE, PlayerState.OFF) + if self.stream and ( + new_state[0] in (PlayerState.IDLE, PlayerState.OFF) or not self.player.available ): self.stream.signal_next = None diff --git a/music_assistant/music_providers/filesystem.py b/music_assistant/music_providers/filesystem.py index 590cf782..72580d92 100644 --- a/music_assistant/music_providers/filesystem.py +++ b/music_assistant/music_providers/filesystem.py @@ -180,12 +180,27 @@ class FileSystemProvider(MusicProvider): _, ext = filename.rsplit(".", 1) if ext in TRACK_EXTENSIONS: - if track := await self._parse_track(full_path): - subitems.append(track) + item_id = self._get_item_id(full_path) + if db_item := await self.mass.music.tracks.get_db_item_by_prov_id( + item_id, provider_id=self.id + ): + subitems.append(db_item) + elif track := await self._parse_track(full_path): + # make sure that the item exists + # https://github.com/music-assistant/hass-music-assistant/issues/707 + db_item = await self.mass.music.tracks.add_db_item(track) + subitems.append(db_item) continue if ext in PLAYLIST_EXTENSIONS: - if playlist := await self._parse_playlist(full_path): - subitems.append(playlist) + if db_item := await self.mass.music.playlists.get_db_item_by_prov_id( + item_id, provider_id=self.id + ): + subitems.append(db_item) + elif playlist := await self._parse_playlist(full_path): + # make sure that the item exists + # https://github.com/music-assistant/hass-music-assistant/issues/707 + db_item = await self.mass.music.playlists.add_db_item(playlist) + subitems.append(db_item) continue return BrowseFolder( @@ -193,7 +208,8 @@ class FileSystemProvider(MusicProvider): provider=self.type, path=path, name=sub_path or self.name, - items=subitems, + # make sure to sort the resulting listing + items=sorted(subitems, key=lambda x: x.name), ) async def sync_library( @@ -242,13 +258,15 @@ class FileSystemProvider(MusicProvider): playlist = await self._parse_playlist(entry.path) # add/update] playlist to db playlist.metadata.checksum = checksum + # playlist is always in-library + playlist.in_library = True await self.mass.music.playlists.add_db_item(playlist) except Exception as err: # pylint: disable=broad-except # we don't want the whole sync to crash on one file so we catch all exceptions here self.logger.exception("Error processing %s - %s", entry.path, str(err)) # save checksums every 100 processed items - # this allows us to pickup where we leftoff when initial scan gets intterrupted + # this allows us to pickup where we leftoff when initial scan gets interrupted if save_checksum_interval == 100: await self.mass.cache.set(cache_key, cur_checksums, SCHEMA_VERSION) save_checksum_interval = 0 @@ -765,8 +783,7 @@ class FileSystemProvider(MusicProvider): playlist = Playlist(playlist_item_id, provider=self.type, name=name) playlist.is_editable = True - # playlist is always in-library - playlist.in_library = True + playlist.add_provider_id( MediaItemProviderId( item_id=playlist_item_id, @@ -787,9 +804,6 @@ class FileSystemProvider(MusicProvider): if not file_path: return False # guard file_path = await self.resolve(file_path) - if self.config.path not in file_path: - # additional guard (needed for files within m3u files) - return False _exists = wrap(os.path.exists) return await _exists(file_path) -- 2.34.1