feat: optimize code

This commit is contained in:
GitPusher99 2024-03-28 16:21:15 +08:00
parent 7bcf16207a
commit 179669e399
2 changed files with 24 additions and 23 deletions

View File

@ -3,6 +3,7 @@ import UserAgent from 'user-agents';
import pino from 'pino';
import { wrapper } from "axios-cookiejar-support";
import { CookieJar } from "tough-cookie";
import { sleep } from "@/lib/utils";
const logger = pino();
@ -24,24 +25,6 @@ export interface AudioInfo {
duration?: string;
}
/**
*
* @param x
* @param y
*/
const sleep = (x: number, y?: number): Promise<void> => {
let timeout = x * 1000;
if (y !== undefined && y !== x) {
const min = Math.min(x, y);
const max = Math.max(x, y);
timeout = Math.floor(Math.random() * (max - min + 1) + min) * 1000;
}
// console.log(`Sleeping for ${timeout / 1000} seconds`);
logger.info(`Sleeping for ${timeout / 1000} seconds`);
return new Promise(resolve => setTimeout(resolve, timeout));
}
class SunoApi {
private static BASE_URL: string = 'https://studio-api.suno.ai';
private static CLERK_BASE_URL: string = 'https://clerk.suno.ai';
@ -247,9 +230,7 @@ class SunoApi {
// Reassemble the processed lyrics lines into a single string, separated by newlines between each line.
// Additional formatting logic can be added here, such as adding specific markers or handling special lines.
const formattedLyrics = lines.join('\n');
return formattedLyrics;
return lines.join('\n');
}
/**
@ -300,8 +281,7 @@ class SunoApi {
const newSunoApi = async (cookie: string) => {
const sunoApi = new SunoApi(cookie);
await sunoApi.init();
return sunoApi;
return await sunoApi.init();
}
export const sunoApi = newSunoApi(process.env.SUNO_COOKIE || '');

21
src/lib/utils.ts Normal file
View File

@ -0,0 +1,21 @@
import pino from "pino";
const logger = pino();
/**
*
* @param x
* @param y
*/
export const sleep = (x: number, y?: number): Promise<void> => {
let timeout = x * 1000;
if (y !== undefined && y !== x) {
const min = Math.min(x, y);
const max = Math.max(x, y);
timeout = Math.floor(Math.random() * (max - min + 1) + min) * 1000;
}
// console.log(`Sleeping for ${timeout / 1000} seconds`);
logger.info(`Sleeping for ${timeout / 1000} seconds`);
return new Promise(resolve => setTimeout(resolve, timeout));
}