YT Music: Fix token expiration on reboot (#1493)
authorMarvin Schenkel <marvinschenkel@gmail.com>
Fri, 12 Jul 2024 10:23:51 +0000 (12:23 +0200)
committerGitHub <noreply@github.com>
Fri, 12 Jul 2024 10:23:51 +0000 (12:23 +0200)
Write yt dlp cache on startup.

music_assistant/server/providers/ytmusic/__init__.py

index 48fbd3ccc013f9a9489dacbe12f5164773dcc903..af450cf9b26d8c860e5c351a60bb9125138feb11 100644 (file)
@@ -108,6 +108,9 @@ SUPPORTED_FEATURES = (
     ProviderFeature.SIMILAR_TRACKS,
 )
 
+YT_DLP_CACHE_SECTION = "youtube-oauth2"
+YT_DLP_CACHE_KEY = "oauth_token"
+
 # TODO: fix disabled tests
 # ruff: noqa: PLW2901, RET504
 
@@ -202,6 +205,8 @@ class YoutubeMusicProvider(MusicProvider):
                 break
         else:
             self.language = "en"
+        # Make sure to write the ytdlp cache on startup
+        await self._update_ytdlp_oauth_token_cache()
         if not await self._user_has_ytm_premium():
             raise LoginFailed("User does not have Youtube Music Premium")
 
@@ -812,7 +817,7 @@ class YoutubeMusicProvider(MusicProvider):
                     "token_type": self.config.get_value(CONF_TOKEN_TYPE),
                     "refresh_token": self.config.get_value(CONF_REFRESH_TOKEN),
                 }
-                ydl.cache.store("youtube-oauth2", "token_data", token_data)
+                ydl.cache.store(YT_DLP_CACHE_SECTION, YT_DLP_CACHE_KEY, token_data)
                 self.logger.debug("Updated ytdlp oauth token cache with new OAuth token.")
 
         await asyncio.to_thread(_update_oauth_cache)