Skip to content

Commit

Permalink
fix: #4
Browse files Browse the repository at this point in the history
Signed-off-by: yihong0618 <[email protected]>
  • Loading branch information
yihong0618 committed Mar 25, 2024
1 parent b122b12 commit c7c8018
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="suno_songs",
version="0.1.2",
version="0.1.5",
author="yihong0618",
author_email="[email protected]",
description="High quality image generation by ideogram.ai. Reverse engineered API.",
Expand Down
16 changes: 7 additions & 9 deletions suno/suno.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit c7c8018

Please sign in to comment.