YTMusic: Rewrite YTM auth to ytdlp access token plugin (#1497)
authorMarvin Schenkel <marvinschenkel@gmail.com>
Sun, 14 Jul 2024 17:36:07 +0000 (19:36 +0200)
committerGitHub <noreply@github.com>
Sun, 14 Jul 2024 17:36:07 +0000 (19:36 +0200)
* Rewrite YTM auth to ytdlp access token plugin.

* Rewrite YTM auth to ytdlp access token plugin.

music_assistant/server/providers/ytmusic/__init__.py
music_assistant/server/providers/ytmusic/manifest.json
requirements_all.txt

index b5da3c5e552644ace59fc79d4175330eec42d29d..cb317fd977e31ac66ec0a63ea04ce59a444739ef 100644 (file)
@@ -205,8 +205,6 @@ 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")
 
@@ -573,7 +571,6 @@ class YoutubeMusicProvider(MusicProvider):
             self.config.update({CONF_EXPIRY_TIME: time() + token["expires_in"]})
             self.config.update({CONF_TOKEN_TYPE: token["token_type"]})
             self._initialize_headers()
-            await self._update_ytdlp_oauth_token_cache()
 
     def _initialize_headers(self) -> dict[str, str]:
         """Return headers to include in the requests."""
@@ -778,12 +775,14 @@ class YoutubeMusicProvider(MusicProvider):
 
         def _extract_best_stream_url_format() -> dict[str, Any]:
             url = f"{YTM_DOMAIN}/watch?v={item_id}"
+            auth = (
+                f"{self.config.get_value(CONF_TOKEN_TYPE)} {self.config.get_value(CONF_AUTH_TOKEN)}"
+            )
             ydl_opts = {
                 "quiet": self.logger.level > logging.DEBUG,
-                # This enables the oauth2 plugin so we can grab the best
+                # This enables the access token plugin so we can grab the best
                 # available quality audio stream
-                "username": "oauth2",
-                "password": "",
+                "username": auth,
                 # This enforces a player client and skips unnecessary scraping to increase speed
                 "extractor_args": {
                     "youtube": {"skip": ["translated_subs", "dash"], "player_client": ["ios"]}
@@ -800,27 +799,6 @@ class YoutubeMusicProvider(MusicProvider):
 
         return await asyncio.to_thread(_extract_best_stream_url_format)
 
-    async def _update_ytdlp_oauth_token_cache(self) -> None:
-        """Update the ytdlp token so we can grab the best available quality audio stream."""
-
-        def _update_oauth_cache() -> None:
-            ydl_opts = {
-                "quiet": self.logger.level > logging.DEBUG,
-                "username": "oauth2",
-                "password": "",
-            }
-            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
-                token_data = {
-                    "access_token": self.config.get_value(CONF_AUTH_TOKEN),
-                    "expires": self.config.get_value(CONF_EXPIRY_TIME),
-                    "token_type": self.config.get_value(CONF_TOKEN_TYPE),
-                    "refresh_token": self.config.get_value(CONF_REFRESH_TOKEN),
-                }
-                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)
-
     def _get_item_mapping(self, media_type: MediaType, key: str, name: str) -> ItemMapping:
         return ItemMapping(
             media_type=media_type,
index 381e0d5b031091389f4da05c24739d3231ab2b97..0dcc9554cb7f0da285291047a5db160c4a489a15 100644 (file)
@@ -4,7 +4,7 @@
   "name": "YouTube Music",
   "description": "Support for the YouTube Music streaming provider in Music Assistant.",
   "codeowners": ["@MarvinSchenkel"],
-  "requirements": ["ytmusicapi==1.7.3", "git+https://github.com/coletdjnz/yt-dlp-youtube-oauth2.git", "yt-dlp==2024.7.7"],
+  "requirements": ["ytmusicapi==1.7.3", "git+https://github.com/MarvinSchenkel/yt-dlp-youtube-accesstoken.git", "yt-dlp==2024.7.7"],
   "documentation": "https://music-assistant.io/music-providers/youtube-music/",
   "multi_instance": true
 }
index b3bb949cf171765876af6307aea84adf202c8262..076bc4d7b604b2613f73c06ca495a6470e3090df 100644 (file)
@@ -18,7 +18,7 @@ deezer-python-async==0.3.0
 defusedxml==0.7.1
 eyeD3==0.9.7
 faust-cchardet>=2.1.18
-git+https://github.com/coletdjnz/yt-dlp-youtube-oauth2.git
+git+https://github.com/MarvinSchenkel/yt-dlp-youtube-accesstoken.git
 hass-client==1.2.0
 ifaddr==0.2.0
 mashumaro==3.13.1