From c7c80183988a45319b8d96335ebd9c2ca115c25a Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Mon, 25 Mar 2024 10:44:41 +0800 Subject: [PATCH] fix: #4 Signed-off-by: yihong0618 --- setup.py | 2 +- suno/suno.py | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index b195061..8ccc404 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="suno_songs", - version="0.1.2", + version="0.1.5", author="yihong0618", author_email="zouzou0208@gmail.com", description="High quality image generation by ideogram.ai. Reverse engineered API.", diff --git a/suno/suno.py b/suno/suno.py index 1ef2115..9bce002 100644 --- a/suno/suno.py +++ b/suno/suno.py @@ -24,14 +24,7 @@ browser_version = "edge101" HEADERS = { - "Origin": base_url, - "Referer": base_url + "/", - "DNT": "1", "Accept-Encoding": "gzip, deflate, br", - "Content-Type": "application/json", - "Pragma": "no-cache", - "Cache-Control": "no-cache", - "TE": "trailers", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) \ Gecko/20100101 Firefox/117.0", } @@ -47,6 +40,7 @@ def __init__(self, cookie: str) -> None: HEADERS["Authorization"] = f"Bearer {auth_token}" self.session.headers = HEADERS self.sid = None + self.retry_time = 0 def _get_auth_token(self): response = self.session.get(get_session_url, impersonate=browser_version) @@ -65,6 +59,7 @@ def _renew(self): response = self.session.post( exchange_token_url.format(sid=self.sid), impersonate=browser_version ) + print(response.json()) self.session.headers["Authorization"] = f"Bearer {response.json().get('jwt')}" @staticmethod @@ -92,6 +87,9 @@ def _fetch_songs_metadata(self, ids): except: if response.json().get("detail", "") == "Unauthorized": print("Token expired, renewing...") + self.retry_time += 1 + if self.retry_time > 3: + raise Exception("Token expired") self._renew() time.sleep(5) return @@ -124,7 +122,7 @@ def get_songs(self, prompt: str) -> list: request_ids = [i["id"] for i in songs_meta_info] start_wait = time.time() print("Waiting for results...") - sleep_time = 6 + sleep_time = 10 while True: if int(time.time() - start_wait) > 600: raise Exception("Request timeout") @@ -133,7 +131,7 @@ def get_songs(self, prompt: str) -> list: # spider rule if sleep_time > 2: time.sleep(sleep_time) - sleep_time -= 1 + sleep_time -= 2 else: time.sleep(2) if not song_url: