Files
MusicMetaFinder/src/api/recognition/auddApi.js
xtrullor73 d8683cb8ad 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.
2024-05-13 22:11:08 -07:00

35 lines
1.1 KiB
JavaScript

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);
});
}