Skip to content

Conversation

@blackicedbear
Copy link

This PR resolves issue #68, where downloaded songs became corrupted or muted after 10 seconds of playback.

  • Updated TOTP implementation to use the correct secret and version.
  • Adjusted TOTP generation logic to support both client and server timestamps.
  • Enabled use_totp authentication by default to ensure valid decryption keys.
  • Bumped project version to 1.5.9.

With these changes, songs now play correctly without muting after 10 seconds.

Closes #68

@yashikada
Copy link

Could you resolve the conflicts? With latest release 1.5.9 you code is incompatible.

@yashikada
Copy link

yashikada commented Oct 4, 2025

I fixed your PR, but doesn't work, I think fhis PR can be closed.
This is the code which I used:

diff --git a/votify/spotify_api.py b/votify/spotify_api.py
index cb637eb..65028ef 100644
--- a/votify/spotify_api.py
+++ b/votify/spotify_api.py
@@ -48,6 +48,7 @@ class SpotifyApi:
         self,
         sp_dc: str | None = None,
         use_device_flow: bool = False,
+        use_totp: bool = True,
     ) -> None:
         self.sp_dc = sp_dc
         self.use_device_flow = use_device_flow
@@ -117,15 +118,17 @@ class SpotifyApi:
 
     def _setup_authorization_with_totp(self) -> None:
         server_time = self._get_server_time()
-        totp = self.totp.generate(timestamp=server_time)
+        # totp = self.totp.generate(timestamp=server_time)
+        totp = self.totp.generate()
+        totpServer = self.totp.generate(timestamp=server_time)
         response = self.session.get(
             self.SESSION_TOKEN_URL,
             params={
                 "reason": "init",
                 "productType": "web-player",
                 "totp": totp,
+                "totpServer": totpServer,
                 "totpVer": str(self.totp.version),
-                "ts": str(server_time),
             },
         )
         check_response(response)
diff --git a/votify/totp.py b/votify/totp.py
index 9e6ea32..83e6170 100644
--- a/votify/totp.py
+++ b/votify/totp.py
@@ -4,6 +4,8 @@ import hashlib
 import hmac
 import math
 import requests
+import time
+from typing import Optional
 
 
 # thanks to https://github.com/glomatico/votify/pull/42#issuecomment-2727036757
@@ -33,7 +35,10 @@ class TOTP:
         latest_version = max(int(v) for v in secrets.keys())
         return latest_version, secrets[str(latest_version)]
 
-    def generate(self, timestamp: int) -> str:
+    def generate(self, timestamp: Optional[int] = None) -> str:
+        if timestamp is None:
+            timestamp = int(time.time() * 1000)
+
         counter = math.floor(timestamp / 1000 / self.PERIOD)
         counter_bytes = counter.to_bytes(8, byteorder="big")
 

Is better improve this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Songs become corrupted/muted after 10 seconds

2 participants