|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import time |
| 5 | +from http.cookies import SimpleCookie |
| 6 | +from threading import Thread |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +from utils import COMMON_HEADERS |
| 11 | + |
| 12 | + |
| 13 | +class SunoCookie: |
| 14 | + def __init__(self): |
| 15 | + self.cookie = SimpleCookie() |
| 16 | + self.session_id = None |
| 17 | + self.token = None |
| 18 | + |
| 19 | + def load_cookie(self, cookie_str): |
| 20 | + self.cookie.load(cookie_str) |
| 21 | + |
| 22 | + def get_cookie(self): |
| 23 | + return ";".join([f"{i}={self.cookie.get(i).value}" for i in self.cookie.keys()]) |
| 24 | + |
| 25 | + def set_session_id(self, session_id): |
| 26 | + self.session_id = session_id |
| 27 | + |
| 28 | + def get_session_id(self): |
| 29 | + return self.session_id |
| 30 | + |
| 31 | + def get_token(self): |
| 32 | + return self.token |
| 33 | + |
| 34 | + def set_token(self, token: str): |
| 35 | + self.token = token |
| 36 | + |
| 37 | + |
| 38 | +suno_auth = SunoCookie() |
| 39 | +suno_auth.set_session_id(os.getenv("SESSION_ID")) |
| 40 | +suno_auth.load_cookie(os.getenv("COOKIE")) |
| 41 | + |
| 42 | + |
| 43 | +def update_token(suno_cookie: SunoCookie): |
| 44 | + headers = {"cookie": suno_cookie.get_cookie()} |
| 45 | + headers.update(COMMON_HEADERS) |
| 46 | + session_id = suno_cookie.get_session_id() |
| 47 | + |
| 48 | + resp = requests.post( |
| 49 | + url=f"https://clerk.suno.com/v1/client/sessions/{session_id}/tokens?_clerk_js_version=4.72.0-snapshot.vc141245", |
| 50 | + headers=headers, |
| 51 | + ) |
| 52 | + |
| 53 | + resp_headers = dict(resp.headers) |
| 54 | + set_cookie = resp_headers.get("Set-Cookie") |
| 55 | + suno_cookie.load_cookie(set_cookie) |
| 56 | + token = resp.json().get("jwt") |
| 57 | + suno_cookie.set_token(token) |
| 58 | + # print(set_cookie) |
| 59 | + # print(f"*** token -> {token} ***") |
| 60 | + |
| 61 | + |
| 62 | +def keep_alive(suno_cookie: SunoCookie): |
| 63 | + while True: |
| 64 | + try: |
| 65 | + update_token(suno_cookie) |
| 66 | + except Exception as e: |
| 67 | + print(e) |
| 68 | + finally: |
| 69 | + time.sleep(5) |
| 70 | + |
| 71 | + |
| 72 | +def start_keep_alive(suno_cookie: SunoCookie): |
| 73 | + t = Thread(target=keep_alive, args=(suno_cookie,)) |
| 74 | + t.start() |
| 75 | + |
| 76 | + |
| 77 | +start_keep_alive(suno_auth) |
0 commit comments