odesli.js 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -100
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -756,111 +756,12 @@ module.exports = class Odesli {
|
|
|
756
756
|
}
|
|
757
757
|
}
|
|
758
758
|
|
|
759
|
-
/**
|
|
760
|
-
* Fetch multiple URLs in batches with enhanced error handling
|
|
761
|
-
*
|
|
762
|
-
* @param {Array<string>} urls - Array of URLs to fetch
|
|
763
|
-
* @param {Object} options - Batch options
|
|
764
|
-
* @param {number} [options.concurrency=5] - Maximum concurrent requests
|
|
765
|
-
* @param {string} [options.country='US'] - Country code for all requests
|
|
766
|
-
* @param {boolean} [options.skipCache=false] - Skip cache for all requests
|
|
767
|
-
* @param {number} [options.timeout] - Timeout for all requests
|
|
768
|
-
* @returns {Promise<Array<Object>>} Array of results with enhanced error information
|
|
769
|
-
*
|
|
770
|
-
* @example
|
|
771
|
-
* ```javascript
|
|
772
|
-
* const results = await odesli.fetchBatch([
|
|
773
|
-
* 'https://open.spotify.com/track/123',
|
|
774
|
-
* 'https://music.apple.com/us/album/test/456?i=789'
|
|
775
|
-
* ], { concurrency: 3, country: 'GB' });
|
|
776
|
-
*
|
|
777
|
-
* results.forEach((result, index) => {
|
|
778
|
-
* if (result.success) {
|
|
779
|
-
* console.log(`Song ${index + 1}: ${result.title}`);
|
|
780
|
-
* } else {
|
|
781
|
-
* console.log(`Song ${index + 1}: Error - ${result.error}`);
|
|
782
|
-
* console.log(` Platform: ${result.platform}`);
|
|
783
|
-
* console.log(` ID: ${result.extractedId || 'Not found'}`);
|
|
784
|
-
* }
|
|
785
|
-
* });
|
|
786
|
-
* ```
|
|
787
|
-
*/
|
|
788
|
-
async fetchBatch (urls, options = {}) {
|
|
789
|
-
if (!Array.isArray(urls)) {
|
|
790
|
-
throw new Error('URLs must be an array')
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
if (urls.length === 0) {
|
|
794
|
-
return []
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
const { concurrency = 5, ...requestOptions } = options
|
|
798
|
-
|
|
799
|
-
const results = []
|
|
800
|
-
const chunks = []
|
|
801
|
-
|
|
802
|
-
// Split URLs into chunks for concurrency control
|
|
803
|
-
for (let i = 0; i < urls.length; i += concurrency) {
|
|
804
|
-
chunks.push(urls.slice(i, i + concurrency))
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
for (const chunk of chunks) {
|
|
808
|
-
const chunkPromises = chunk.map(async (url, _index) => {
|
|
809
|
-
try {
|
|
810
|
-
const result = await this.fetch(url, requestOptions)
|
|
811
|
-
// Add success property to successful responses
|
|
812
|
-
return { ...result, success: true }
|
|
813
|
-
} catch (error) {
|
|
814
|
-
// Enhanced error object with more context
|
|
815
|
-
const errorInfo = {
|
|
816
|
-
success: false,
|
|
817
|
-
url,
|
|
818
|
-
error: error.message,
|
|
819
|
-
platform: this.detectPlatform(url),
|
|
820
|
-
extractedId: this.extractId(url),
|
|
821
|
-
timestamp: new Date().toISOString(),
|
|
822
|
-
statusCode: this._extractStatusCode(error.message),
|
|
823
|
-
retryable: this._isRetryableError(error.message)
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
// Add additional context based on error type
|
|
827
|
-
if (error.message.includes('400')) {
|
|
828
|
-
errorInfo.errorType = 'BAD_REQUEST'
|
|
829
|
-
errorInfo.suggestion = 'Check if the URL is valid and accessible'
|
|
830
|
-
} else if (error.message.includes('401')) {
|
|
831
|
-
errorInfo.errorType = 'UNAUTHORIZED'
|
|
832
|
-
errorInfo.suggestion = 'API key may be required or invalid'
|
|
833
|
-
} else if (error.message.includes('429')) {
|
|
834
|
-
errorInfo.errorType = 'RATE_LIMITED'
|
|
835
|
-
errorInfo.suggestion = 'Rate limit exceeded, try again later'
|
|
836
|
-
} else if (error.message.includes('404')) {
|
|
837
|
-
errorInfo.errorType = 'NOT_FOUND'
|
|
838
|
-
errorInfo.suggestion = 'Content may not be available in the specified region'
|
|
839
|
-
} else if (error.message.includes('timeout')) {
|
|
840
|
-
errorInfo.errorType = 'TIMEOUT'
|
|
841
|
-
errorInfo.suggestion = 'Request timed out, try again'
|
|
842
|
-
} else {
|
|
843
|
-
errorInfo.errorType = 'UNKNOWN'
|
|
844
|
-
errorInfo.suggestion = 'An unexpected error occurred'
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
return errorInfo
|
|
848
|
-
}
|
|
849
|
-
})
|
|
850
|
-
|
|
851
|
-
const chunkResults = await Promise.all(chunkPromises)
|
|
852
|
-
results.push(...chunkResults)
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
return results
|
|
856
|
-
}
|
|
857
|
-
|
|
858
759
|
/**
|
|
859
760
|
* Extract status code from error message
|
|
860
761
|
* @private
|
|
861
762
|
*/
|
|
862
763
|
_extractStatusCode(errorMessage) {
|
|
863
|
-
const match = errorMessage.match(/(
|
|
764
|
+
const match = errorMessage.match(/(\\d{3}):/)
|
|
864
765
|
return match ? parseInt(match[1]) : null
|
|
865
766
|
}
|
|
866
767
|
|