forked from gochendong/suno-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
89 lines (67 loc) · 2.65 KB
/
utils.py
File metadata and controls
89 lines (67 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import os
import aiohttp
from loguru import logger
BASE_URL = os.getenv("BASE_URL")
COMMON_HEADERS = {
# "Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"Referer": "https://suno.com/",
"Origin": "https://suno.com",
}
async def fetch(url, headers=None, data=None, method="POST"):
if headers is None:
headers = {}
headers.update(COMMON_HEADERS)
if data is not None:
data = json.dumps(data)
async with aiohttp.ClientSession() as session:
try:
async with session.request(
method=method, url=url, data=data, headers=headers
) as resp:
# Read the response content
text = await resp.text()
# Check for HTTP errors
if resp.status >= 400:
raise Exception(f"HTTP Error {resp.status}: {text}")
# If the response is JSON, parse it
return json.loads(text)
except Exception as e:
# Handle other exceptions
raise Exception(str(e))
async def get_feed(ids, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/clip/{ids}"
response = await fetch(api_url, headers, method="GET")
return [response]
async def get_feeds(ids, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/feed/v2?ids={ids}"
response = await fetch(api_url, headers, method="GET")
clips = response.get("clips")
if clips:
return clips
return response
async def generate_music(data, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/generate/v2/"
response = await fetch(api_url, headers, data)
return response
async def concat_music(data, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/generate/concat/v2/"
response = await fetch(api_url, headers, data)
return response
async def generate_lyrics(prompt, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/generate/lyrics/"
data = {"prompt": prompt, "lyrics_model": "default"}
return await fetch(api_url, headers, data)
async def get_lyrics(lid, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/generate/lyrics/{lid}"
return await fetch(api_url, headers, method="GET")
# You can use this function to send notifications
def notify(message: str):
logger.info(message)