feat: Refactor project structure for modularity and expandability

- Implemented the service and adapter layers instead of controllers to simplify the integration of new audio recognition and metadata fetching APIs.
- Unified code styling and practices throughout the project to ensure consistency.
This commit is contained in:
xtrullor73
2024-05-13 22:11:08 -07:00
parent bcdc66ca18
commit d8683cb8ad
33 changed files with 398 additions and 243 deletions

View File

@@ -0,0 +1,34 @@
import fs from 'fs';
import axios from 'axios';
import axiosRetry from '../../utils/retryAxios.js';
import handleError from '../../errors/generalApiErrorHandler.js';
export default async function auddAudioRecognition(filePath) {
const audioData = fs.readFileSync(filePath);
const base64Audio = Buffer.from(audioData).toString('base64');
const body = new URLSearchParams({
api_token: process.env.AUDD_API_TOKEN,
audio: base64Audio,
return: 'spotify',
});
const requestOptions = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
};
return axios.post('https://api.audd.io/', new URLSearchParams(body), requestOptions)
.then((response) => {
const { data } = response;
if (!data) throw new Error('No data received from Audd API');
if (data.error) throw new Error(`Audd API Error: ${JSON.stringify(data.error)}`);
console.log('Recognition successful for:', filePath);
return data;
})
.catch((error) => {
console.error('Error recognizing audio');
const errorMessage = handleError(error, filePath);
throw new Error(errorMessage);
});
}