Skip to content

Commit

Permalink
feat: Update song library retrieval and authentication token renewal
Browse files Browse the repository at this point in the history
- Add code to get the song library
- Add a new method `_renew_auth_token` to renew the authentication token

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
  • Loading branch information
yihong0618 committed Mar 29, 2024
1 parent dff76f4 commit 388bb1f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ print(i.get_limit_left())
i.save_songs("大江东去,浪淘尽,千古风流人物。故垒西边,人道是、三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。", is_custom=True, title="custom", tags="轻松的R&B, BPM60, 小调, 电吉他、贝斯、键盘和轻鼓, 男性歌手")
```

Get song library

```python
from suno import SongsGen
i = SongsGen('cookie') # Replace 'cookie'
print(i.get_song_library())
```

## Thanks

- All my 爱发电 sponsors https://afdian.net/a/yihong0618?tab=sponsor
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.3.2",
version="0.4.0",
author="yihong0618",
author_email="zouzou0208@gmail.com",
description="High quality songs generation by suno.ai. Reverse engineered API.",
Expand Down
38 changes: 36 additions & 2 deletions suno/suno.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def _get_auth_token(self):
data = response.json()
return data.get("jwt")

def _renew_auth_token(self):
auth_token = self._get_auth_token()
HEADERS["Authorization"] = f"Bearer {auth_token}"
self.session.headers = HEADERS

@staticmethod
def parse_cookie_string(cookie_string):
cookie = SimpleCookie()
Expand All @@ -97,6 +102,28 @@ def parse_cookie_string(cookie_string):
cookies_dict[key] = morsel.value
return Cookies(cookies_dict)

def get_song_library(self):
self._renew_auth_token()
page_number = 1
result = []
while 1:
print(f"Getting page {page_number} data.")
url = f"https://studio-api.suno.ai/api/feed/?page={page_number}"
response = self.session.get(url, impersonate=browser_version)
data = response.json()
if page_number == 3:
break
if len(data) < 20:
result.extend(data)
break
# spider rule
time.sleep(2)
if page_number % 3 == 0:
self._renew_auth_token()
page_number += 1
result.extend(data)
return result

def get_limit_left(self) -> int:
self.session.headers["user-agent"] = ua.random
r = self.session.get(
Expand Down Expand Up @@ -166,6 +193,7 @@ def get_songs(
prompt: str,
tags: Union[str, None] = None,
title: str = "",
make_instrumental: bool = False,
is_custom: bool = False,
) -> dict:
url = f"{base_url}/api/generate/v2/"
Expand All @@ -174,7 +202,7 @@ def get_songs(
"gpt_description_prompt": prompt,
"mv": "chirp-v3-0",
"prompt": "",
"make_instrumental": False,
"make_instrumental": make_instrumental,
}
if is_custom:
payload["prompt"] = prompt
Expand Down Expand Up @@ -225,12 +253,17 @@ def save_songs(
output_dir: str = "./output",
tags: Union[str, None] = None,
title: Union[str, None] = None,
make_instrumental: bool = False,
is_custom: bool = False,
) -> None:
mp3_index = 0
try:
self.get_songs(
prompt, tags=tags, title=title, is_custom=is_custom
prompt,
tags=tags,
title=title,
is_custom=is_custom,
make_instrumental=make_instrumental,
) # make the info dict
song_name = self.song_info_dict["song_name"]
lyric = self.song_info_dict["lyric"]
Expand Down Expand Up @@ -313,6 +346,7 @@ def main():
output_dir=args.output_dir,
title=args.title,
tags=args.tags,
make_instrumental=False,
is_custom=args.is_custom,
)

Expand Down

0 comments on commit 388bb1f

Please sign in to comment.