91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
import json
|
|
import aiohttp
|
|
|
|
from logger import logger
|
|
|
|
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://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:
|
|
content_type = resp.headers.get("Content-Type","")
|
|
logger.info(f"Request to {url} with headers {resp.headers} method {method} and data {data}")
|
|
if "application/json" in content_type:
|
|
return await resp.json()
|
|
else:
|
|
text_response = await resp.text()
|
|
logger.error(f"Unexpected response format: {text_response}")
|
|
return f"Unexpected response format: {text_response}"
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error occurred: {e}")
|
|
raise
|
|
|
|
async def get_feed(url, ids, token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/feed/v2?ids={ids}"
|
|
response = await fetch(api_url, headers, method="GET")
|
|
return response
|
|
|
|
|
|
async def generate_music(url,data, token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/generate/v2/"
|
|
response = await fetch(api_url, headers, data)
|
|
return response
|
|
|
|
|
|
async def generate_lyrics(url,prompt, token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/generate/lyrics/"
|
|
data = {"prompt": prompt}
|
|
return await fetch(api_url, headers, data)
|
|
|
|
|
|
async def get_lyrics(url,lid, token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/generate/lyrics/{lid}"
|
|
return await fetch(api_url, headers, method="GET")
|
|
|
|
|
|
async def get_credits(url,token):
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/billing/info/"
|
|
respose = await fetch(api_url, headers, method="GET")
|
|
return {
|
|
"credits_left": respose['total_credits_left'],
|
|
"period": respose['period'],
|
|
"monthly_limit": respose['monthly_limit'],
|
|
"monthly_usage": respose['monthly_usage'],
|
|
"subscription_type": respose['subscription_type']
|
|
}
|
|
|
|
async def recharge(url,data, token):
|
|
if not token or not isinstance(data,dict):
|
|
logger.error("Invalid token or data")
|
|
raise
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
api_url = f"{url}/api/billing/purchase-credits/"
|
|
return await fetch(api_url, headers, data)
|
|
|
|
async def get_expire(cookie):
|
|
if not cookie:
|
|
logger.error("Invalid cookie at get expire")
|
|
raise
|
|
headers = {"Cookie": cookie}
|
|
api_url = "https://clerk.suno.com/v1/client?_clerk_js_version=5.26.1"
|
|
return await fetch(api_url, headers, method="GET")
|