Skip to content

Commit 518a71f

Browse files
committed
fix: fall back to highest available TOTP version when configured TOTP_VER is missing
1 parent b62e6e1 commit 518a71f

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

spotify_monitor.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
"""
33
Author: Michal Szymanski <misiektoja-github@rm-rf.ninja>
4-
v2.9.1
4+
v2.9.2
55
66
Tool implementing real-time tracking of Spotify friends music activity:
77
https://github.com/misiektoja/spotify_monitor/
@@ -17,7 +17,7 @@
1717
spotipy (required since v2.7 due to new Spotify restrictions introduced on 22 Dec 2025)
1818
"""
1919

20-
VERSION = "2.9.1"
20+
VERSION = "2.9.2"
2121

2222
# ---------------------------
2323
# CONFIGURATION SECTION START
@@ -1579,16 +1579,24 @@ def fetch_server_time(session: req.Session, ua: str) -> int:
15791579
return int(parsedate_to_datetime(date_hdr).timestamp())
15801580

15811581

1582+
# Resolves the effective TOTP version, falling back to the highest available when the configured TOTP_VER is missing from SECRET_CIPHER_DICT
1583+
def resolve_totp_ver() -> int:
1584+
if not SECRET_CIPHER_DICT:
1585+
raise SecretsUnavailableError("resolve_totp_ver(): SECRET_CIPHER_DICT is empty")
1586+
if TOTP_VER and str(TOTP_VER) in SECRET_CIPHER_DICT:
1587+
return TOTP_VER
1588+
available = sorted(map(int, SECRET_CIPHER_DICT))
1589+
fallback = available[-1]
1590+
if TOTP_VER:
1591+
print(f"Warning: configured TOTP_VER ({TOTP_VER}) is missing from SECRET_CIPHER_DICT (available: {available}); falling back to auto-selected version {fallback}")
1592+
return fallback
1593+
1594+
15821595
# Creates a TOTP object using a secret derived from transformed cipher bytes
15831596
def generate_totp():
15841597
import pyotp
15851598

1586-
if not SECRET_CIPHER_DICT:
1587-
raise SecretsUnavailableError("generate_totp(): SECRET_CIPHER_DICT is empty")
1588-
1589-
ver = TOTP_VER or max(map(int, SECRET_CIPHER_DICT))
1590-
if str(ver) not in SECRET_CIPHER_DICT:
1591-
raise SecretsUnavailableError(f"generate_totp(): Defined TOTP_VER ({ver}) is missing in SECRET_CIPHER_DICT")
1599+
ver = resolve_totp_ver()
15921600

15931601
secret_cipher_bytes = SECRET_CIPHER_DICT[str(ver)]
15941602

@@ -1681,7 +1689,7 @@ def refresh_access_token_from_sp_dc(sp_dc: str) -> dict:
16811689
client_time = int(time_ns() / 1000 / 1000)
16821690
otp_value = totp_obj.at(server_time)
16831691

1684-
totp_ver = TOTP_VER or max(map(int, SECRET_CIPHER_DICT))
1692+
totp_ver = resolve_totp_ver()
16851693

16861694
params = {
16871695
"reason": "transport",

0 commit comments

Comments
 (0)