From f690bb68d795a779a74f62338a4fc78e7690ccb7 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 30 Jul 2024 12:04:57 +0200 Subject: [PATCH] Allow use of a personal client id for Spotify (#1536) --- music_assistant/common/models/config_entries.py | 2 +- .../server/providers/spotify/__init__.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/music_assistant/common/models/config_entries.py b/music_assistant/common/models/config_entries.py index e6a62ba4..cd86921c 100644 --- a/music_assistant/common/models/config_entries.py +++ b/music_assistant/common/models/config_entries.py @@ -187,7 +187,7 @@ class Config(DataClassDictMixin): def get_value(self, key: str) -> ConfigValueType: """Return config value for given key.""" config_value = self.values[key] - if config_value.type == ConfigEntryType.SECURE_STRING: + if config_value.type == ConfigEntryType.SECURE_STRING and config_value.value: assert isinstance(config_value.value, str) assert DECRYPT_CALLBACK is not None return DECRYPT_CALLBACK(config_value.value) diff --git a/music_assistant/server/providers/spotify/__init__.py b/music_assistant/server/providers/spotify/__init__.py index a2fcbf8f..be7e6ebe 100644 --- a/music_assistant/server/providers/spotify/__init__.py +++ b/music_assistant/server/providers/spotify/__init__.py @@ -61,6 +61,7 @@ if TYPE_CHECKING: from music_assistant.server import MusicAssistant from music_assistant.server.models import ProviderInstanceType +CONF_CLIENT_ID = "client_id" CACHE_DIR = gettempdir() LIKED_SONGS_FAKE_PLAYLIST_ID_PREFIX = "liked_songs" @@ -118,6 +119,15 @@ async def get_config_entries( label="Password", required=True, ), + ConfigEntry( + key=CONF_CLIENT_ID, + type=ConfigEntryType.SECURE_STRING, + label="Client ID", + description="By default, a generic client ID is used which is heavy rate limited. " + "It is advised that you create your own Spotify Developer account and use " + "that client ID here to speedup performance.", + required=False, + ), ) @@ -135,8 +145,11 @@ class SpotifyProvider(MusicProvider): """Handle async initialization of the provider.""" self._cache_dir = CACHE_DIR self._ap_workaround = False - # try to get a token, raise if that fails self._cache_dir = os.path.join(CACHE_DIR, self.instance_id) + if self.config.get_value(CONF_CLIENT_ID): + # loosen the throttler a bit when a custom client id is used + self.throttler.rate_limit = 45 + self.throttler.period = 30 # try login which will raise if it fails await self.login() @@ -726,7 +739,7 @@ class SpotifyProvider(MusicProvider): "-O", "-t", "--client-id", - app_var(2), + self.config.get_value(CONF_CLIENT_ID) or app_var(2), "--scope", scope, "-c", -- 2.34.1