mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-28 03:39:50 +02:00
feat(api): implement audio recognition with Audd API
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import axios from 'axios';
|
||||
import fs from 'fs';
|
||||
|
||||
export default function audioController(filePath, AUDD_API_TOKEN) {
|
||||
const audioData = fs.readFileSync(filePath);
|
||||
const base64Audio = Buffer.from(audioData).toString('base64');
|
||||
const body = new URLSearchParams({
|
||||
api_token: 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)}`);
|
||||
return data;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error recognizing audio:', error.toString());
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user