Prepare V0.2

This commit is contained in:
unknown
2025-12-18 20:21:21 +01:00
parent 3f0f402b14
commit 7054dc1dfe
3 changed files with 77 additions and 59 deletions

View File

@@ -327,6 +327,17 @@ class AudioKeyManager(PacketsReceiver, Closeable):
spoticlub_loaded_logged = True spoticlub_loaded_logged = True
print(f"\n[SpotiClub API] Plugin Loaded! Welcome {spoticlub_user}\n") 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 = { payload = {
"gid": util.bytes_to_hex(gid), "gid": util.bytes_to_hex(gid),
"file_id": util.bytes_to_hex(file_id), "file_id": util.bytes_to_hex(file_id),
@@ -339,73 +350,80 @@ class AudioKeyManager(PacketsReceiver, Closeable):
tries = 0 tries = 0
last_err: typing.Optional[Exception] = None last_err: typing.Optional[Exception] = None
while True: try:
tries += 1 while True:
try: tries += 1
resp = requests.post(server_url, json=payload, timeout=AudioKeyManager.audio_key_request_timeout) try:
resp = requests.post(server_url, json=payload, timeout=AudioKeyManager.audio_key_request_timeout)
# If another client instance is already active for this # If another client instance is already active for this
# SpotiClub user, the server will reply with HTTP 423 and # SpotiClub user, the server will reply with HTTP 423 and
# instruct this client to wait before retrying. # instruct this client to wait before retrying.
if resp.status_code == 423: if resp.status_code == 423:
try: try:
data = resp.json() data = resp.json()
except Exception: # noqa: BLE001 except Exception: # noqa: BLE001
data = {} data = {}
retry_after = data.get("retry_after", 60) retry_after = data.get("retry_after", 60)
if not isinstance(retry_after, (int, float)): if not isinstance(retry_after, (int, float)):
retry_after = 10 retry_after = 10
print( print(
f"[SpotiClub API] Another client is already using this account. Waiting {int(retry_after)}s before retrying..." f"[SpotiClub API] Another client is already using this account. Waiting {int(retry_after)}s before retrying..."
) )
self.logger.info( self.logger.info(
"[SpotiClub API] Queued client for user %s; waiting %ds before retry", "[SpotiClub API] Queued client for user %s; waiting %ds before retry",
spoticlub_user, spoticlub_user,
int(retry_after), int(retry_after),
) )
time.sleep(float(retry_after)) time.sleep(float(retry_after))
# Do NOT count this as a failure towards the max retries. # Do NOT count this as a failure towards the max retries.
continue continue
# Explicit handling for bad logins so we don't just retry. # Explicit handling for bad logins so we don't just retry.
if resp.status_code == 401: if resp.status_code == 401:
print( 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." "[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) raise SystemExit(1)
if resp.status_code != 200: if resp.status_code != 200:
raise RuntimeError(f"[SpotiClub API] Sorry, the API returned the unexpected code {resp.status_code}: {resp.text}") raise RuntimeError(f"[SpotiClub API] Sorry, the API returned the unexpected code {resp.status_code}: {resp.text}")
data = resp.json() data = resp.json()
key_hex = data.get("key") key_hex = data.get("key")
if not isinstance(key_hex, str): if not isinstance(key_hex, str):
raise RuntimeError("[SpotiClub API] Sorry, API response missing 'key'") raise RuntimeError("[SpotiClub API] Sorry, API response missing 'key'")
country = data.get("country") country = data.get("country")
if isinstance(country, str): if isinstance(country, str):
if AudioKeyManager._spoticlub_current_country != country: if AudioKeyManager._spoticlub_current_country != country:
AudioKeyManager._spoticlub_current_country = country AudioKeyManager._spoticlub_current_country = country
print(f"[SpotiClub API] Received {country} as the download country\n\n") print(f"[SpotiClub API] Received {country} as the download country\n\n")
new_serial = data.get("client_serial") new_serial = data.get("client_serial")
if isinstance(new_serial, str) and new_serial: if isinstance(new_serial, str) and new_serial:
spoticlub_client_serial = new_serial spoticlub_client_serial = new_serial
key_bytes = util.hex_to_bytes(key_hex) key_bytes = util.hex_to_bytes(key_hex)
if len(key_bytes) != 16: if len(key_bytes) != 16:
raise RuntimeError("[SpotiClub API] Woops, received Audio Key must be 16 bytes long") raise RuntimeError("[SpotiClub API] Woops, received Audio Key must be 16 bytes long")
return key_bytes return key_bytes
except Exception as exc: # noqa: BLE001 except Exception as exc: # noqa: BLE001
last_err = exc last_err = exc
self.logger.warning("[SpotiClub API] Retrying the contact... (try %d): %s", tries, exc) self.logger.warning("[SpotiClub API] Retrying the contact... (try %d): %s", tries, exc)
if not retry or tries >= 3: if not retry or tries >= 3:
break break
time.sleep(5) time.sleep(5)
raise RuntimeError( raise RuntimeError(
"Failed fetching Audio Key from API for gid: {}, fileId: {} (last error: {})".format( "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)) 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: class Callback:

Binary file not shown.