Skip to content

Commit

Permalink
fix: better error handle
Browse files Browse the repository at this point in the history
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
  • Loading branch information
yihong0618 committed Mar 25, 2024
1 parent 6836913 commit fa3b364
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

concurrency:
group: ${{ github.event.number || github.run_id }}
cancel-in-progress: true

jobs:
testing:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: install python 3.9
uses: actions/setup-python@v5
with:
python-version: "3.9"
cache: "pip" # caching pip dependencies
- name: Check formatting (black)
run: |
pip install black
black . --check
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.2.0",
version="0.2.1",
author="yihong0618",
author_email="zouzou0208@gmail.com",
description="High quality image generation by ideogram.ai. Reverse engineered API.",
Expand Down
68 changes: 49 additions & 19 deletions suno/suno.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def __init__(self, cookie: str) -> None:
self.session.headers = HEADERS
self.sid = None
self.retry_time = 0
# make the song_info_dict global since we can get the lyrics and song name first
self.song_info_dict = {}
# now data
self.now_data = {}

def _get_auth_token(self):
response = self.session.get(get_session_url, impersonate=browser_version)
Expand Down Expand Up @@ -93,30 +97,51 @@ def _fetch_songs_metadata(self, ids):
url = f"https://studio-api.suno.ai/api/feed/?ids={id1}%2C{id2}"
response = self.session.get(url, impersonate=browser_version)
try:
data = response.json()[0]
data = response.json()
self.now_data = data
except:
if response.json().get("detail", "") == "Unauthorized":
print("Token expired, renewing...")
self.retry_time += 1
if self.retry_time > 3:
raise Exception("Token expired will renew and sleep 5 seconds")
song_name, lyric = self._parse_lyrics(self.now_data[0])
self.song_info_dict["song_name"] = song_name
self.song_info_dict["lyric"] = lyric
self.song_info_dict["song_url"] = (
f"https://audiopipe.suno.ai/?item_id={id1}"
)
print("will sleep 45 and try to download")
time.sleep(45)
# Done here
return True
self._renew()
time.sleep(5)
data = response.json()
song_info_dict = {
"song_name": "",
"lyric": "",
"song_url": "",
}
for d in data:
# only get one url for now
# and early return
if audio_url := d.get("audio_url"):
song_info_dict["song_url"] = audio_url
song_name, lyric = self._parse_lyrics(data[0])
song_info_dict["song_name"] = song_name
song_info_dict["lyric"] = lyric
return song_info_dict
try:

for d in data:
# only get one url for now
# and early return
if audio_url := d.get("audio_url"):
song_name, lyric = self._parse_lyrics(d)
self.song_info_dict["song_name"] = song_name
self.song_info_dict["lyric"] = lyric
self.song_info_dict["song_url"] = audio_url
return True
return False
except Exception as e:
print(e)
# since we only get the music_id is ok
# so we can make the id here and sleep some time
print("Will sleep 45s and get the music url")
time.sleep(45)
song_name, lyric = self._parse_lyrics(self.now_data[0])
self.song_info_dict["song_name"] = song_name
self.song_info_dict["lyric"] = lyric
self.song_info_dict["song_url"] = (
f"https://audiopipe.suno.ai/?item_id={id1}"
)
# Done here
return True

def get_songs(self, prompt: str) -> dict:
url = f"{base_url}/api/generate/v2/"
Expand Down Expand Up @@ -156,7 +181,9 @@ def get_songs(self, prompt: str) -> dict:
if not song_info:
print(".", end="", flush=True)
else:
return song_info
break
# keep the song info dict as old api
return self.song_info_dict

def save_songs(
self,
Expand All @@ -165,7 +192,10 @@ def save_songs(
) -> None:
mp3_index = 0
try:
song_name, lyric, link = self.get_songs(prompt).values()
self.get_songs(prompt) # make the info dict
song_name = self.song_info_dict["song_name"]
lyric = self.song_info_dict["lyric"]
link = self.song_info_dict["song_url"]
except Exception as e:
print(e)
raise
Expand Down

0 comments on commit fa3b364

Please sign in to comment.