Fix: Deezer tracks that have been replaced with a newer version fail to play (#1892)
authorawhiemstra <ahiemstra@heimr.nl>
Tue, 21 Jan 2025 11:35:27 +0000 (12:35 +0100)
committerGitHub <noreply@github.com>
Tue, 21 Jan 2025 11:35:27 +0000 (12:35 +0100)
Some tracks have been replaced by a newer version. When this happens,
the old track information stays available but it will fail to play
because the stream has become invalid. Fortunately, the playback API
provides the track replacement in a "FALLBACK" entry. So when we
encounter a track that has a "FALLBACK" entry, use the information from
that entry rather than the main track entry for the stream.

Fixes music-assistant/support#3466

music_assistant/providers/deezer/__init__.py
music_assistant/providers/deezer/gw_client.py

index 994c14b8fb57135cd8b314d1f23c3e23a88c0ca0..a2b77b19eaab32570d1d48259d12ed446c5ca488 100644 (file)
@@ -441,7 +441,10 @@ class DeezerProvider(MusicProvider):
             ),
             stream_type=StreamType.CUSTOM,
             duration=int(song_data["DURATION"]),
-            data={"url": url, "format": url_details["format"]},
+            # Due to track replacement, the track ID of the stream may be different from the ID
+            # that is stored. We need the proper track ID to decrypt the stream, so store it
+            # separately so we can use it later on.
+            data={"url": url, "format": url_details["format"], "track_id": song_data["SNG_ID"]},
             size=int(song_data[f"FILESIZE_{url_details['format']}"]),
         )
 
@@ -449,7 +452,7 @@ class DeezerProvider(MusicProvider):
         self, streamdetails: StreamDetails, seek_position: int = 0
     ) -> AsyncGenerator[bytes, None]:
         """Return the audio stream for the provider item."""
-        blowfish_key = self.get_blowfish_key(streamdetails.item_id)
+        blowfish_key = self.get_blowfish_key(streamdetails.data["track_id"])
         chunk_index = 0
         timeout = ClientTimeout(total=0, connect=30, sock_read=600)
         headers = {}
index e0e83f753c31f0ebcf6a45b0dd3cff19f7b8343e..f5dda1eba097901f69253f76fc84b846959faa0c 100644 (file)
@@ -128,8 +128,18 @@ class GWClient:
     async def get_deezer_track_urls(self, track_id):
         """Get the URL for a given track id."""
         dz_license = await self._get_license()
-        song_data = await self.get_song_data(track_id)
-        track_token = song_data["results"]["TRACK_TOKEN"]
+
+        song_results = await self.get_song_data(track_id)
+
+        song_data = song_results["results"]
+        # If the song has been replaced by a newer version, the old track will
+        # not play anymore. The data for the newer song is contained in a
+        # "FALLBACK" entry in the song data. So if that is available, use that
+        # instead so we get the right track token.
+        if "FALLBACK" in song_data:
+            song_data = song_data["FALLBACK"]
+
+        track_token = song_data["TRACK_TOKEN"]
         url_data = {
             "license_token": dz_license,
             "media": [
@@ -151,7 +161,7 @@ class GWClient:
             msg = "Received an error from API"
             raise DeezerGWError(msg, error)
 
-        return result_json["data"][0]["media"][0], song_data["results"]
+        return result_json["data"][0]["media"][0], song_data
 
     async def log_listen(
         self, next_track: str | None = None, last_track: StreamDetails | None = None