From c1ef83430d9b2db58e49164860e9ae19c6b1422d Mon Sep 17 00:00:00 2001 From: Fabian Munkes <105975993+fmunkes@users.noreply.github.com> Date: Fri, 21 Feb 2025 00:17:28 +0100 Subject: [PATCH] A bit of cleanup in ABS Provider (#1970) * move code to constants.py and helpers.py * remove obsolete comments and reduce api calls for podcast Since we do not use caching anymore for podcast, we can take advantage of the minified or normal version. * remove two code changes --- .../providers/audiobookshelf/__init__.py | 89 ++++--------------- .../providers/audiobookshelf/constants.py | 53 +++++++++++ .../providers/audiobookshelf/helpers.py | 24 +++++ 3 files changed, 92 insertions(+), 74 deletions(-) create mode 100644 music_assistant/providers/audiobookshelf/constants.py create mode 100644 music_assistant/providers/audiobookshelf/helpers.py diff --git a/music_assistant/providers/audiobookshelf/__init__.py b/music_assistant/providers/audiobookshelf/__init__.py index 8131bedc..a760bd6b 100644 --- a/music_assistant/providers/audiobookshelf/__init__.py +++ b/music_assistant/providers/audiobookshelf/__init__.py @@ -3,8 +3,6 @@ from __future__ import annotations from collections.abc import AsyncGenerator, Sequence -from dataclasses import dataclass, field -from enum import StrEnum from typing import TYPE_CHECKING import aioaudiobookshelf as aioabs @@ -23,7 +21,6 @@ from aioaudiobookshelf.schema.library import ( LibraryItemExpandedPodcast, ) from aioaudiobookshelf.schema.library import LibraryMediaType as AbsLibraryMediaType -from mashumaro.mixins.dict import DataClassDictMixin from music_assistant_models.config_entries import ConfigEntry, ConfigValueType, ProviderConfig from music_assistant_models.enums import ( ConfigEntryType, @@ -44,6 +41,21 @@ from music_assistant.providers.audiobookshelf.parsers import ( parse_podcast_episode, ) +from .constants import ( + ABSBROWSEITEMSTOPATH, + CACHE_CATEGORY_LIBRARIES, + CACHE_KEY_LIBRARIES, + CONF_HIDE_EMPTY_PODCASTS, + CONF_PASSWORD, + CONF_URL, + CONF_USERNAME, + CONF_VERIFY_SSL, + AbsBrowseItemsBook, + AbsBrowseItemsPodcast, + AbsBrowsePaths, +) +from .helpers import LibrariesHelper, LibraryHelper + if TYPE_CHECKING: from aioaudiobookshelf.schema.events_socket import LibraryItemRemoved from aioaudiobookshelf.schema.media_progress import MediaProgress @@ -53,77 +65,6 @@ if TYPE_CHECKING: from music_assistant.mass import MusicAssistant from music_assistant.models import ProviderInstanceType -CONF_URL = "url" -CONF_USERNAME = "username" -CONF_PASSWORD = "password" -CONF_VERIFY_SSL = "verify_ssl" -# optionally hide podcasts with no episodes -CONF_HIDE_EMPTY_PODCASTS = "hide_empty_podcasts" - -# We do _not_ store the full library, just the helper classes LibrariesHelper/ LibraryHelper, -# see below, i.e. only uuids and the lib's name. -# Caching these can be removed, but I'd then have to iterate the full item list -# within the browse function if the user wishes to see all audiobooks/ podcasts -# of a library. -CACHE_CATEGORY_LIBRARIES = 0 -CACHE_KEY_LIBRARIES = "libraries" - - -class AbsBrowsePaths(StrEnum): - """Path prefixes for browse view.""" - - LIBRARIES_BOOK = "lb" - LIBRARIES_PODCAST = "lp" - AUTHORS = "a" - NARRATORS = "n" - SERIES = "s" - COLLECTIONS = "c" - AUDIOBOOKS = "b" - - -class AbsBrowseItemsBook(StrEnum): - """Folder names in browse view for books.""" - - AUTHORS = "Authors" - NARRATORS = "Narrators" - SERIES = "Series" - COLLECTIONS = "Collections" - AUDIOBOOKS = "Audiobooks" - - -class AbsBrowseItemsPodcast(StrEnum): - """Folder names in browse view for podcasts.""" - - PODCASTS = "Podcasts" - - -@dataclass(kw_only=True) -class LibraryHelper(DataClassDictMixin): - """Lib name + media items' uuids.""" - - name: str - item_ids: set[str] = field(default_factory=set) - - -@dataclass(kw_only=True) -class LibrariesHelper(DataClassDictMixin): - """Helper class to store ABSLibrary name, id and the uuids of its media items. - - Dictionary is lib_id:AbsLibraryWithItemIDs. - """ - - audiobooks: dict[str, LibraryHelper] = field(default_factory=dict) - podcasts: dict[str, LibraryHelper] = field(default_factory=dict) - - -ABSBROWSEITEMSTOPATH: dict[str, str] = { - AbsBrowseItemsBook.AUTHORS: AbsBrowsePaths.AUTHORS, - AbsBrowseItemsBook.NARRATORS: AbsBrowsePaths.NARRATORS, - AbsBrowseItemsBook.SERIES: AbsBrowsePaths.SERIES, - AbsBrowseItemsBook.COLLECTIONS: AbsBrowsePaths.COLLECTIONS, - AbsBrowseItemsBook.AUDIOBOOKS: AbsBrowsePaths.AUDIOBOOKS, -} - async def setup( mass: MusicAssistant, manifest: ProviderManifest, config: ProviderConfig diff --git a/music_assistant/providers/audiobookshelf/constants.py b/music_assistant/providers/audiobookshelf/constants.py new file mode 100644 index 00000000..e3663b4e --- /dev/null +++ b/music_assistant/providers/audiobookshelf/constants.py @@ -0,0 +1,53 @@ +"""Constants for Audiobookshelf provider.""" + +from enum import StrEnum + +# CONFIG +CONF_URL = "url" +CONF_USERNAME = "username" +CONF_PASSWORD = "password" +CONF_VERIFY_SSL = "verify_ssl" +# optionally hide podcasts with no episodes +CONF_HIDE_EMPTY_PODCASTS = "hide_empty_podcasts" + +# CACHE +CACHE_CATEGORY_LIBRARIES = 0 +CACHE_KEY_LIBRARIES = "libraries" + + +# BROWSE +class AbsBrowsePaths(StrEnum): + """Path prefixes for browse view.""" + + LIBRARIES_BOOK = "lb" + LIBRARIES_PODCAST = "lp" + AUTHORS = "a" + NARRATORS = "n" + SERIES = "s" + COLLECTIONS = "c" + AUDIOBOOKS = "b" + + +class AbsBrowseItemsBook(StrEnum): + """Folder names in browse view for books.""" + + AUTHORS = "Authors" + NARRATORS = "Narrators" + SERIES = "Series" + COLLECTIONS = "Collections" + AUDIOBOOKS = "Audiobooks" + + +class AbsBrowseItemsPodcast(StrEnum): + """Folder names in browse view for podcasts.""" + + PODCASTS = "Podcasts" + + +ABSBROWSEITEMSTOPATH: dict[str, str] = { + AbsBrowseItemsBook.AUTHORS: AbsBrowsePaths.AUTHORS, + AbsBrowseItemsBook.NARRATORS: AbsBrowsePaths.NARRATORS, + AbsBrowseItemsBook.SERIES: AbsBrowsePaths.SERIES, + AbsBrowseItemsBook.COLLECTIONS: AbsBrowsePaths.COLLECTIONS, + AbsBrowseItemsBook.AUDIOBOOKS: AbsBrowsePaths.AUDIOBOOKS, +} diff --git a/music_assistant/providers/audiobookshelf/helpers.py b/music_assistant/providers/audiobookshelf/helpers.py new file mode 100644 index 00000000..3390b570 --- /dev/null +++ b/music_assistant/providers/audiobookshelf/helpers.py @@ -0,0 +1,24 @@ +"""Helpers for Audiobookshelf provider.""" + +from dataclasses import dataclass, field + +from mashumaro.mixins.dict import DataClassDictMixin + + +@dataclass(kw_only=True) +class LibraryHelper(DataClassDictMixin): + """Lib name + media items' uuids.""" + + name: str + item_ids: set[str] = field(default_factory=set) + + +@dataclass(kw_only=True) +class LibrariesHelper(DataClassDictMixin): + """Helper class to store ABSLibrary name, id and the uuids of its media items. + + Dictionary is lib_id:AbsLibraryWithItemIDs. + """ + + audiobooks: dict[str, LibraryHelper] = field(default_factory=dict) + podcasts: dict[str, LibraryHelper] = field(default_factory=dict) -- 2.34.1