mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-18 02:54:19 +02:00
- 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.
44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
import axiosRetry from '../../utils/retryAxios.js';
|
|
import handleError from '../../errors/generalApiErrorHandler.js';
|
|
|
|
/**
|
|
* Retrieves the metadata for a recording from MusicBrainz.
|
|
*
|
|
* @param {string} recordingId - The MusicBrainz recording ID (can be obtained from acoustid).
|
|
* @returns {Promise<Object>} - A promise resolving to the track metadata.
|
|
*/
|
|
export default async function getMetadata(recordingId) {
|
|
const baseUrl = 'https://musicbrainz.org';
|
|
const query = `/ws/2/recording/${recordingId}?fmt=json&inc=artists+releases+release-groups+isrcs+url-rels+discids+media+artist-credits+aliases+tags+ratings+genres`;
|
|
|
|
const { VERSION, PROJECTNAME, EMAIL } = process.env;
|
|
const requestOptions = {
|
|
headers: {
|
|
'User-Agent': `${PROJECTNAME}/${VERSION} ( ${EMAIL} )`,
|
|
Accept: 'application/json',
|
|
},
|
|
};
|
|
|
|
return axiosRetry.get(baseUrl + query, requestOptions)
|
|
.then((response) => {
|
|
const { data } = response;
|
|
|
|
if (!data) {
|
|
console.error(`Recording ID ${recordingId} could not retrieve metadata from MusicBrainz`);
|
|
return null;
|
|
}
|
|
|
|
console.log('Metadata retrieval successful for:', recordingId);
|
|
return data;
|
|
})
|
|
.catch((error) => {
|
|
// If it's a 404 error, log the error and proceed
|
|
if (error.response && error.response.status === 404) {
|
|
console.error(`No metadata found for recording ID: ${recordingId}`);
|
|
return null;
|
|
}
|
|
const errorMessage = handleError(error, recordingId);
|
|
throw new Error(errorMessage);
|
|
});
|
|
}
|