first commit

This commit is contained in:
“xHuPo” 2024-08-29 11:43:48 +08:00
parent 8e58e24d2e
commit 973c47ff25
16 changed files with 866 additions and 0 deletions

90
utils.py Normal file
View file

@ -0,0 +1,90 @@
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/?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']
}
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.16.1"
return await fetch(api_url, headers, method="GET")