diff --git a/js/api.js b/js/api.js index 44b71c1..324182d 100644 --- a/js/api.js +++ b/js/api.js @@ -48,7 +48,19 @@ export class LosslessAPI { const response = await fetch(url, { signal: options.signal }); if (response.status === 429) { - throw new Error(RATE_LIMIT_ERROR_MESSAGE); + const retryAfter = response.headers.get('Retry-After'); + let waitTime = 2000 * attempt; // Default exponential backoff + + if (retryAfter) { + const seconds = parseInt(retryAfter, 10); + if (!isNaN(seconds)) { + waitTime = seconds * 1000; + } + } + + console.warn(`Rate limit hit. Waiting ${waitTime}ms before retry ${attempt}/${maxRetries}...`); + await delay(waitTime); + continue; } if (response.ok) { diff --git a/js/app.js b/js/app.js index 0476dd8..69ae0da 100644 --- a/js/app.js +++ b/js/app.js @@ -1058,6 +1058,9 @@ async function parseCSV(csvText, api, onProgress) { // Search for the track in hifi tidal api's catalog if (trackTitle && artistNames) { + // Add a small delay to prevent rate limiting + await new Promise(resolve => setTimeout(resolve, 300)); + try { const searchQuery = `${trackTitle} ${artistNames}`; const searchResults = await api.searchTracks(searchQuery);