forked from SunoAI-API/Suno-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
63 lines (47 loc) · 1.76 KB
/
utils.py
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
import json
import os
import time
import aiohttp
from dotenv import load_dotenv
load_dotenv()
BASE_URL = os.getenv("BASE_URL")
COMMON_HEADERS = {
"Content-Type": "text/plain;charset=UTF-8",
"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://app.suno.ai/",
"Origin": "https://app.suno.ai",
}
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)
print(data, method, headers, url)
async with aiohttp.ClientSession() as session:
try:
async with session.request(
method=method, url=url, data=data, headers=headers
) as resp:
return await resp.json()
except Exception as e:
return f"An error occurred: {e}"
async def get_feed(ids, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/feed/?ids={ids}"
response = await fetch(api_url, headers, method="GET")
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 generate_lyrics(prompt, token):
headers = {"Authorization": f"Bearer {token}"}
api_url = f"{BASE_URL}/api/generate/lyrics/"
data = {"prompt": prompt}
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")