mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-19 23:54:27 +02:00
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:
34
src/api/recognition/auddApi.js
Normal file
34
src/api/recognition/auddApi.js
Normal 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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user