feat: optimize code
This commit is contained in:
		
							parent
							
								
									7bcf16207a
								
							
						
					
					
						commit
						179669e399
					
				@ -3,6 +3,7 @@ import UserAgent from 'user-agents';
 | 
				
			|||||||
import pino from 'pino';
 | 
					import pino from 'pino';
 | 
				
			||||||
import { wrapper } from "axios-cookiejar-support";
 | 
					import { wrapper } from "axios-cookiejar-support";
 | 
				
			||||||
import { CookieJar } from "tough-cookie";
 | 
					import { CookieJar } from "tough-cookie";
 | 
				
			||||||
 | 
					import { sleep } from "@/lib/utils";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const logger = pino();
 | 
					const logger = pino();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -24,24 +25,6 @@ export interface AudioInfo {
 | 
				
			|||||||
  duration?: string;
 | 
					  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 {
 | 
					class SunoApi {
 | 
				
			||||||
  private static BASE_URL: string = 'https://studio-api.suno.ai';
 | 
					  private static BASE_URL: string = 'https://studio-api.suno.ai';
 | 
				
			||||||
  private static CLERK_BASE_URL: string = 'https://clerk.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.
 | 
					    // 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.
 | 
					    // Additional formatting logic can be added here, such as adding specific markers or handling special lines.
 | 
				
			||||||
    const formattedLyrics = lines.join('\n');
 | 
					    return lines.join('\n');
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return formattedLyrics;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -300,8 +281,7 @@ class SunoApi {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const newSunoApi = async (cookie: string) => {
 | 
					const newSunoApi = async (cookie: string) => {
 | 
				
			||||||
  const sunoApi = new SunoApi(cookie);
 | 
					  const sunoApi = new SunoApi(cookie);
 | 
				
			||||||
  await sunoApi.init();
 | 
					  return await sunoApi.init();
 | 
				
			||||||
  return sunoApi;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const sunoApi = newSunoApi(process.env.SUNO_COOKIE || '');
 | 
					export const sunoApi = newSunoApi(process.env.SUNO_COOKIE || '');
 | 
				
			||||||
							
								
								
									
										21
									
								
								src/lib/utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/lib/utils.ts
									
									
									
									
									
										Normal 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));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user