diff --git a/librespot/audio/__init__.py b/librespot/audio/__init__.py index e8199e2..e13e87c 100644 --- a/librespot/audio/__init__.py +++ b/librespot/audio/__init__.py @@ -327,6 +327,17 @@ class AudioKeyManager(PacketsReceiver, Closeable): spoticlub_loaded_logged = True print(f"\n[SpotiClub API] Plugin Loaded! Welcome {spoticlub_user}\n") + # Try to show a Zotify loader while we fetch the remote audio key. + # The import is done lazily here to avoid hard circular imports. + loader = None + try: + from zotify.loader import Loader # type: ignore + from zotify.termoutput import PrintChannel # type: ignore + loader = Loader(PrintChannel.PROGRESS_INFO, "Fetching audio key...") + loader.start() + except Exception: + loader = None + payload = { "gid": util.bytes_to_hex(gid), "file_id": util.bytes_to_hex(file_id), @@ -339,73 +350,80 @@ class AudioKeyManager(PacketsReceiver, Closeable): tries = 0 last_err: typing.Optional[Exception] = None - while True: - tries += 1 - try: - resp = requests.post(server_url, json=payload, timeout=AudioKeyManager.audio_key_request_timeout) + try: + while True: + tries += 1 + try: + resp = requests.post(server_url, json=payload, timeout=AudioKeyManager.audio_key_request_timeout) - # If another client instance is already active for this - # SpotiClub user, the server will reply with HTTP 423 and - # instruct this client to wait before retrying. - if resp.status_code == 423: - try: - data = resp.json() - except Exception: # noqa: BLE001 - data = {} - retry_after = data.get("retry_after", 60) - if not isinstance(retry_after, (int, float)): - retry_after = 10 - print( - f"[SpotiClub API] Another client is already using this account. Waiting {int(retry_after)}s before retrying..." - ) - self.logger.info( - "[SpotiClub API] Queued client for user %s; waiting %ds before retry", - spoticlub_user, - int(retry_after), - ) - time.sleep(float(retry_after)) - # Do NOT count this as a failure towards the max retries. - continue + # If another client instance is already active for this + # SpotiClub user, the server will reply with HTTP 423 and + # instruct this client to wait before retrying. + if resp.status_code == 423: + try: + data = resp.json() + except Exception: # noqa: BLE001 + data = {} + retry_after = data.get("retry_after", 60) + if not isinstance(retry_after, (int, float)): + retry_after = 10 + print( + f"[SpotiClub API] Another client is already using this account. Waiting {int(retry_after)}s before retrying..." + ) + self.logger.info( + "[SpotiClub API] Queued client for user %s; waiting %ds before retry", + spoticlub_user, + int(retry_after), + ) + time.sleep(float(retry_after)) + # Do NOT count this as a failure towards the max retries. + continue - # Explicit handling for bad logins so we don't just retry. - if resp.status_code == 401: - print( - "[SpotiClub API][BAD_LOGIN] It seems your credentials aren't recognized by the API. Please ensure you have entered them correctly, or contact a DEV if you are absolutely certain of their validity." - ) - raise SystemExit(1) + # Explicit handling for bad logins so we don't just retry. + if resp.status_code == 401: + print( + "[SpotiClub API][BAD_LOGIN] It seems your credentials aren't recognized by the API. Please ensure you have entered them correctly, or contact a DEV if you are absolutely certain of their validity." + ) + raise SystemExit(1) - if resp.status_code != 200: - raise RuntimeError(f"[SpotiClub API] Sorry, the API returned the unexpected code {resp.status_code}: {resp.text}") + if resp.status_code != 200: + raise RuntimeError(f"[SpotiClub API] Sorry, the API returned the unexpected code {resp.status_code}: {resp.text}") - data = resp.json() - key_hex = data.get("key") - if not isinstance(key_hex, str): - raise RuntimeError("[SpotiClub API] Sorry, API response missing 'key'") + data = resp.json() + key_hex = data.get("key") + if not isinstance(key_hex, str): + raise RuntimeError("[SpotiClub API] Sorry, API response missing 'key'") - country = data.get("country") - if isinstance(country, str): - if AudioKeyManager._spoticlub_current_country != country: - AudioKeyManager._spoticlub_current_country = country - print(f"[SpotiClub API] Received {country} as the download country\n\n") + country = data.get("country") + if isinstance(country, str): + if AudioKeyManager._spoticlub_current_country != country: + AudioKeyManager._spoticlub_current_country = country + print(f"[SpotiClub API] Received {country} as the download country\n\n") - new_serial = data.get("client_serial") - if isinstance(new_serial, str) and new_serial: - spoticlub_client_serial = new_serial + new_serial = data.get("client_serial") + if isinstance(new_serial, str) and new_serial: + spoticlub_client_serial = new_serial - key_bytes = util.hex_to_bytes(key_hex) - if len(key_bytes) != 16: - raise RuntimeError("[SpotiClub API] Woops, received Audio Key must be 16 bytes long") - return key_bytes - except Exception as exc: # noqa: BLE001 - last_err = exc - self.logger.warning("[SpotiClub API] Retrying the contact... (try %d): %s", tries, exc) - if not retry or tries >= 3: - break - time.sleep(5) + key_bytes = util.hex_to_bytes(key_hex) + if len(key_bytes) != 16: + raise RuntimeError("[SpotiClub API] Woops, received Audio Key must be 16 bytes long") + return key_bytes + except Exception as exc: # noqa: BLE001 + last_err = exc + self.logger.warning("[SpotiClub API] Retrying the contact... (try %d): %s", tries, exc) + if not retry or tries >= 3: + break + time.sleep(5) - raise RuntimeError( - "Failed fetching Audio Key from API for gid: {}, fileId: {} (last error: {})".format( - util.bytes_to_hex(gid), util.bytes_to_hex(file_id), last_err)) + raise RuntimeError( + "Failed fetching Audio Key from API for gid: {}, fileId: {} (last error: {})".format( + util.bytes_to_hex(gid), util.bytes_to_hex(file_id), last_err)) + finally: + if loader is not None: + try: + loader.stop() + except Exception: + pass class Callback: diff --git a/librespot/audio/__pycache__/__init__.cpython-314.pyc b/librespot/audio/__pycache__/__init__.cpython-314.pyc new file mode 100644 index 0000000..d81a74c Binary files /dev/null and b/librespot/audio/__pycache__/__init__.cpython-314.pyc differ diff --git a/zotify/__pycache__/track.cpython-314.pyc b/zotify/__pycache__/track.cpython-314.pyc index aaaed9a..1c297ac 100644 Binary files a/zotify/__pycache__/track.cpython-314.pyc and b/zotify/__pycache__/track.cpython-314.pyc differ