52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import sys, os, time
|
|
from yandex_music import Client
|
|
from yandex_music.exceptions import NetworkError
|
|
|
|
c = Client()
|
|
code = c.request_device_code()
|
|
|
|
print("="*50, flush=True)
|
|
print(f"URL: {code.verification_url}", flush=True)
|
|
print(f"CODE: {code.user_code}", flush=True)
|
|
print("="*50, flush=True)
|
|
|
|
with open("/tmp/ya_device_status.txt", "w") as f:
|
|
f.write(f"URL: {code.verification_url}\nCODE: {code.user_code}\n")
|
|
|
|
deadline = time.monotonic() + code.expires_in
|
|
interval = max(code.interval or 5, 6)
|
|
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
token_obj = c.poll_device_token(code.device_code)
|
|
if token_obj is not None:
|
|
token = token_obj.access_token
|
|
print(f"SUCCESS_TOKEN: {token}", flush=True)
|
|
with open("/tmp/ya_token.txt", "w") as f:
|
|
f.write(token)
|
|
|
|
# Read and update credentials.txt
|
|
cred_path = "/home/ai-projects/memory/credentials.txt"
|
|
if os.path.exists(cred_path):
|
|
with open(cred_path, "r") as cf:
|
|
content = cf.read()
|
|
if "yandex_music_token:" not in content:
|
|
content += f"\n# Yandex Music Token\nyandex_music_token: {token}\n"
|
|
with open(cred_path, "w") as cf:
|
|
cf.write(content)
|
|
print("CREDENTIALS_UPDATED", flush=True)
|
|
sys.exit(0)
|
|
except NetworkError as e:
|
|
if "429" in str(e):
|
|
print("Got 429 rate limit, sleeping 10s...", flush=True)
|
|
time.sleep(10)
|
|
continue
|
|
print(f"Network error: {e}", flush=True)
|
|
except Exception as e:
|
|
print(f"Error polling: {e}", flush=True)
|
|
|
|
time.sleep(interval)
|
|
|
|
print("TIMED_OUT", flush=True)
|
|
sys.exit(1)
|