odesli.js 1.3.2 → 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/.github/workflows/ci.yml +3 -0
- package/lib/index.js +52 -138
- package/package.json +1 -1
package/.github/workflows/ci.yml
CHANGED
package/lib/index.js
CHANGED
|
@@ -592,8 +592,8 @@ module.exports = class Odesli {
|
|
|
592
592
|
}
|
|
593
593
|
|
|
594
594
|
// if they happen to input the full id (PLATFORM_SONG::UNIQUEID), just get the UNIQUEID
|
|
595
|
-
|
|
596
|
-
const path = `links?platform=${platform}&type=${type}&id=${id}&userCountry=${country}`
|
|
595
|
+
const idParts = id.split('::')
|
|
596
|
+
const path = `links?platform=${platform}&type=${type}&id=${idParts.length > 1 ? idParts[1] : id}&userCountry=${country}`
|
|
597
597
|
|
|
598
598
|
// Temporarily override timeout and cache settings if specified
|
|
599
599
|
const originalTimeout = this.timeout
|
|
@@ -674,15 +674,19 @@ module.exports = class Odesli {
|
|
|
674
674
|
* @throws {Error} When country code format is invalid
|
|
675
675
|
* @throws {Error} When API returns an error
|
|
676
676
|
*/
|
|
677
|
-
async getById
|
|
677
|
+
async getById(id, options = {}) {
|
|
678
678
|
if (!id) throw new Error('No `id` was provided to odesli.getById()')
|
|
679
|
-
|
|
679
|
+
|
|
680
|
+
const idParts = id.split('::');
|
|
681
|
+
if (idParts.length !== 2 || !idParts[0] || !idParts[1] || !idParts[0].includes('_')) {
|
|
682
|
+
throw new Error('Provided Entity ID Does not match format. `<PLATFORM>_<SONG|ALBUM>::<UNIQUEID>`')
|
|
683
|
+
}
|
|
680
684
|
|
|
681
685
|
// Handle backward compatibility: if second parameter is a string, treat it as country
|
|
682
686
|
let country = 'US'
|
|
683
687
|
let skipCache = false
|
|
684
688
|
let timeout = this.timeout
|
|
685
|
-
|
|
689
|
+
|
|
686
690
|
if (typeof options === 'string') {
|
|
687
691
|
country = options
|
|
688
692
|
} else {
|
|
@@ -690,31 +694,33 @@ module.exports = class Odesli {
|
|
|
690
694
|
skipCache = options.skipCache || false
|
|
691
695
|
timeout = options.timeout || this.timeout
|
|
692
696
|
}
|
|
693
|
-
|
|
697
|
+
|
|
694
698
|
// Validate country code
|
|
695
699
|
if (!this._validateCountry(country)) {
|
|
696
700
|
throw new Error('Invalid country code format. Must be ISO 3166-1 Alpha-2 (e.g., "US", "GB")')
|
|
697
701
|
}
|
|
698
702
|
|
|
699
703
|
// Convert string into seperate params
|
|
700
|
-
const
|
|
701
|
-
const
|
|
702
|
-
const
|
|
704
|
+
const parts = id.split('::')
|
|
705
|
+
const unique = parts[1] || ''
|
|
706
|
+
const platformAndType = (parts[0] || '').split('_')
|
|
707
|
+
const type = platformAndType.pop() || ''
|
|
708
|
+
const platform = platformAndType.join('_')
|
|
709
|
+
|
|
710
|
+
const path = `links?platform=${platform.toLowerCase()}&type=${type.toLowerCase()}&id=${unique}&userCountry=${country}`
|
|
703
711
|
|
|
704
|
-
const path = `links?platform=${platform}&type=${type}&id=${unique}&userCountry=${country}`
|
|
705
|
-
|
|
706
712
|
// Temporarily override timeout and cache settings if specified
|
|
707
713
|
const originalTimeout = this.timeout
|
|
708
714
|
const originalCacheEnabled = this.cacheEnabled
|
|
709
|
-
|
|
715
|
+
|
|
710
716
|
if (timeout !== this.timeout) {
|
|
711
717
|
this.timeout = timeout
|
|
712
718
|
}
|
|
713
|
-
|
|
719
|
+
|
|
714
720
|
if (skipCache) {
|
|
715
721
|
this.cacheEnabled = false
|
|
716
722
|
}
|
|
717
|
-
|
|
723
|
+
|
|
718
724
|
try {
|
|
719
725
|
const song = await this._request(path)
|
|
720
726
|
|
|
@@ -750,111 +756,12 @@ module.exports = class Odesli {
|
|
|
750
756
|
}
|
|
751
757
|
}
|
|
752
758
|
|
|
753
|
-
/**
|
|
754
|
-
* Fetch multiple URLs in batches with enhanced error handling
|
|
755
|
-
*
|
|
756
|
-
* @param {Array<string>} urls - Array of URLs to fetch
|
|
757
|
-
* @param {Object} options - Batch options
|
|
758
|
-
* @param {number} [options.concurrency=5] - Maximum concurrent requests
|
|
759
|
-
* @param {string} [options.country='US'] - Country code for all requests
|
|
760
|
-
* @param {boolean} [options.skipCache=false] - Skip cache for all requests
|
|
761
|
-
* @param {number} [options.timeout] - Timeout for all requests
|
|
762
|
-
* @returns {Promise<Array<Object>>} Array of results with enhanced error information
|
|
763
|
-
*
|
|
764
|
-
* @example
|
|
765
|
-
* ```javascript
|
|
766
|
-
* const results = await odesli.fetchBatch([
|
|
767
|
-
* 'https://open.spotify.com/track/123',
|
|
768
|
-
* 'https://music.apple.com/us/album/test/456?i=789'
|
|
769
|
-
* ], { concurrency: 3, country: 'GB' });
|
|
770
|
-
*
|
|
771
|
-
* results.forEach((result, index) => {
|
|
772
|
-
* if (result.success) {
|
|
773
|
-
* console.log(`Song ${index + 1}: ${result.title}`);
|
|
774
|
-
* } else {
|
|
775
|
-
* console.log(`Song ${index + 1}: Error - ${result.error}`);
|
|
776
|
-
* console.log(` Platform: ${result.platform}`);
|
|
777
|
-
* console.log(` ID: ${result.extractedId || 'Not found'}`);
|
|
778
|
-
* }
|
|
779
|
-
* });
|
|
780
|
-
* ```
|
|
781
|
-
*/
|
|
782
|
-
async fetchBatch (urls, options = {}) {
|
|
783
|
-
if (!Array.isArray(urls)) {
|
|
784
|
-
throw new Error('URLs must be an array')
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
if (urls.length === 0) {
|
|
788
|
-
return []
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
const { concurrency = 5, ...requestOptions } = options
|
|
792
|
-
|
|
793
|
-
const results = []
|
|
794
|
-
const chunks = []
|
|
795
|
-
|
|
796
|
-
// Split URLs into chunks for concurrency control
|
|
797
|
-
for (let i = 0; i < urls.length; i += concurrency) {
|
|
798
|
-
chunks.push(urls.slice(i, i + concurrency))
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
for (const chunk of chunks) {
|
|
802
|
-
const chunkPromises = chunk.map(async (url, _index) => {
|
|
803
|
-
try {
|
|
804
|
-
const result = await this.fetch(url, requestOptions)
|
|
805
|
-
// Add success property to successful responses
|
|
806
|
-
return { ...result, success: true }
|
|
807
|
-
} catch (error) {
|
|
808
|
-
// Enhanced error object with more context
|
|
809
|
-
const errorInfo = {
|
|
810
|
-
success: false,
|
|
811
|
-
url,
|
|
812
|
-
error: error.message,
|
|
813
|
-
platform: this.detectPlatform(url),
|
|
814
|
-
extractedId: this.extractId(url),
|
|
815
|
-
timestamp: new Date().toISOString(),
|
|
816
|
-
statusCode: this._extractStatusCode(error.message),
|
|
817
|
-
retryable: this._isRetryableError(error.message)
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
// Add additional context based on error type
|
|
821
|
-
if (error.message.includes('400')) {
|
|
822
|
-
errorInfo.errorType = 'BAD_REQUEST'
|
|
823
|
-
errorInfo.suggestion = 'Check if the URL is valid and accessible'
|
|
824
|
-
} else if (error.message.includes('401')) {
|
|
825
|
-
errorInfo.errorType = 'UNAUTHORIZED'
|
|
826
|
-
errorInfo.suggestion = 'API key may be required or invalid'
|
|
827
|
-
} else if (error.message.includes('429')) {
|
|
828
|
-
errorInfo.errorType = 'RATE_LIMITED'
|
|
829
|
-
errorInfo.suggestion = 'Rate limit exceeded, try again later'
|
|
830
|
-
} else if (error.message.includes('404')) {
|
|
831
|
-
errorInfo.errorType = 'NOT_FOUND'
|
|
832
|
-
errorInfo.suggestion = 'Content may not be available in the specified region'
|
|
833
|
-
} else if (error.message.includes('timeout')) {
|
|
834
|
-
errorInfo.errorType = 'TIMEOUT'
|
|
835
|
-
errorInfo.suggestion = 'Request timed out, try again'
|
|
836
|
-
} else {
|
|
837
|
-
errorInfo.errorType = 'UNKNOWN'
|
|
838
|
-
errorInfo.suggestion = 'An unexpected error occurred'
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
return errorInfo
|
|
842
|
-
}
|
|
843
|
-
})
|
|
844
|
-
|
|
845
|
-
const chunkResults = await Promise.all(chunkPromises)
|
|
846
|
-
results.push(...chunkResults)
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
return results
|
|
850
|
-
}
|
|
851
|
-
|
|
852
759
|
/**
|
|
853
760
|
* Extract status code from error message
|
|
854
761
|
* @private
|
|
855
762
|
*/
|
|
856
763
|
_extractStatusCode(errorMessage) {
|
|
857
|
-
const match = errorMessage.match(/(
|
|
764
|
+
const match = errorMessage.match(/(\\d{3}):/)
|
|
858
765
|
return match ? parseInt(match[1]) : null
|
|
859
766
|
}
|
|
860
767
|
|
|
@@ -883,20 +790,26 @@ module.exports = class Odesli {
|
|
|
883
790
|
detectPlatform (url) {
|
|
884
791
|
if (!url) return null
|
|
885
792
|
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
793
|
+
try {
|
|
794
|
+
const urlObj = new URL(url)
|
|
795
|
+
const hostname = urlObj.hostname.toLowerCase()
|
|
796
|
+
const pathname = urlObj.pathname.toLowerCase()
|
|
797
|
+
|
|
798
|
+
if (['music.youtube.com'].includes(hostname) || (['youtube.com', 'www.youtube.com'].includes(hostname) && pathname.startsWith('/music'))) return 'youtubeMusic'
|
|
799
|
+
if (['youtube.com', 'www.youtube.com', 'youtu.be'].includes(hostname)) return 'youtube'
|
|
800
|
+
if (['music.amazon.com'].includes(hostname) || (['amazon.com', 'www.amazon.com'].includes(hostname) && pathname.startsWith('/music'))) return 'amazonMusic'
|
|
801
|
+
if (['music.yandex.ru'].includes(hostname) || (['yandex.ru'].includes(hostname) && pathname.startsWith('/music'))) return 'yandex'
|
|
802
|
+
if (['open.spotify.com', 'spotify.com'].includes(hostname)) return 'spotify'
|
|
803
|
+
if (['music.apple.com', 'itunes.apple.com'].includes(hostname)) return 'appleMusic'
|
|
804
|
+
if (['tidal.com', 'listen.tidal.com'].includes(hostname)) return 'tidal'
|
|
805
|
+
if (['deezer.com', 'www.deezer.com'].includes(hostname)) return 'deezer'
|
|
806
|
+
if (['soundcloud.com', 'www.soundcloud.com'].includes(hostname)) return 'soundcloud'
|
|
807
|
+
if (['pandora.com', 'www.pandora.com'].includes(hostname)) return 'pandora'
|
|
808
|
+
if (['napster.com', 'us.napster.com'].includes(hostname)) return 'napster'
|
|
809
|
+
if (['spinrilla.com', 'www.spinrilla.com'].includes(hostname)) return 'spinrilla'
|
|
810
|
+
} catch {
|
|
811
|
+
// Malformed URL, just ignore.
|
|
812
|
+
}
|
|
900
813
|
|
|
901
814
|
return null
|
|
902
815
|
}
|
|
@@ -917,14 +830,15 @@ module.exports = class Odesli {
|
|
|
917
830
|
try {
|
|
918
831
|
const urlObj = new URL(url)
|
|
919
832
|
const pathParts = urlObj.pathname.split('/').filter(Boolean)
|
|
833
|
+
const hostname = urlObj.hostname.toLowerCase()
|
|
920
834
|
|
|
921
835
|
// Spotify
|
|
922
|
-
if (
|
|
836
|
+
if (hostname === 'open.spotify.com') {
|
|
923
837
|
return pathParts[pathParts.length - 1]
|
|
924
838
|
}
|
|
925
839
|
|
|
926
840
|
// Apple Music - improved to handle various URL formats
|
|
927
|
-
if (
|
|
841
|
+
if (['music.apple.com', 'itunes.apple.com'].includes(hostname)) {
|
|
928
842
|
// Handle album URLs with track IDs: /album/name/id?i=trackId
|
|
929
843
|
const trackIdMatch = url.match(/[?&]i=(\d+)/)
|
|
930
844
|
if (trackIdMatch) {
|
|
@@ -938,38 +852,38 @@ module.exports = class Odesli {
|
|
|
938
852
|
}
|
|
939
853
|
|
|
940
854
|
// Handle other Apple Music URL formats
|
|
941
|
-
const
|
|
942
|
-
if (
|
|
943
|
-
return
|
|
855
|
+
const lastPart = pathParts[pathParts.length - 1];
|
|
856
|
+
if (/^\d+$/.test(lastPart)) {
|
|
857
|
+
return lastPart;
|
|
944
858
|
}
|
|
945
859
|
|
|
946
860
|
return null
|
|
947
861
|
}
|
|
948
862
|
|
|
949
863
|
// YouTube
|
|
950
|
-
if (
|
|
864
|
+
if (['youtube.com', 'www.youtube.com', 'music.youtube.com', 'youtu.be'].includes(hostname)) {
|
|
951
865
|
const videoMatch = url.match(/(?:v=|\/)([a-zA-Z0-9_-]{11})/)
|
|
952
866
|
return videoMatch ? videoMatch[1] : null
|
|
953
867
|
}
|
|
954
868
|
|
|
955
869
|
// Tidal
|
|
956
|
-
if (
|
|
870
|
+
if (['tidal.com', 'listen.tidal.com'].includes(hostname)) {
|
|
957
871
|
return pathParts[pathParts.length - 1]
|
|
958
872
|
}
|
|
959
873
|
|
|
960
874
|
// Deezer
|
|
961
|
-
if (
|
|
875
|
+
if (['deezer.com', 'www.deezer.com'].includes(hostname)) {
|
|
962
876
|
return pathParts[pathParts.length - 1]
|
|
963
877
|
}
|
|
964
878
|
|
|
965
879
|
// Amazon Music
|
|
966
|
-
if (
|
|
880
|
+
if (['music.amazon.com'].includes(hostname) || (['amazon.com', 'www.amazon.com'].includes(hostname) && urlObj.pathname.toLowerCase().startsWith('/music'))) {
|
|
967
881
|
const idMatch = url.match(/\/albums\/([^/]+)/)
|
|
968
882
|
return idMatch ? idMatch[1] : null
|
|
969
883
|
}
|
|
970
884
|
|
|
971
885
|
// SoundCloud
|
|
972
|
-
if (
|
|
886
|
+
if (['soundcloud.com', 'www.soundcloud.com'].includes(hostname)) {
|
|
973
887
|
return pathParts[pathParts.length - 1]
|
|
974
888
|
}
|
|
975
889
|
|