odesli.js 1.3.1 → 1.3.3
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 +51 -38
- 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
|
|
|
@@ -883,20 +889,26 @@ module.exports = class Odesli {
|
|
|
883
889
|
detectPlatform (url) {
|
|
884
890
|
if (!url) return null
|
|
885
891
|
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
892
|
+
try {
|
|
893
|
+
const urlObj = new URL(url)
|
|
894
|
+
const hostname = urlObj.hostname.toLowerCase()
|
|
895
|
+
const pathname = urlObj.pathname.toLowerCase()
|
|
896
|
+
|
|
897
|
+
if (['music.youtube.com'].includes(hostname) || (['youtube.com', 'www.youtube.com'].includes(hostname) && pathname.startsWith('/music'))) return 'youtubeMusic'
|
|
898
|
+
if (['youtube.com', 'www.youtube.com', 'youtu.be'].includes(hostname)) return 'youtube'
|
|
899
|
+
if (['music.amazon.com'].includes(hostname) || (['amazon.com', 'www.amazon.com'].includes(hostname) && pathname.startsWith('/music'))) return 'amazonMusic'
|
|
900
|
+
if (['music.yandex.ru'].includes(hostname) || (['yandex.ru'].includes(hostname) && pathname.startsWith('/music'))) return 'yandex'
|
|
901
|
+
if (['open.spotify.com', 'spotify.com'].includes(hostname)) return 'spotify'
|
|
902
|
+
if (['music.apple.com', 'itunes.apple.com'].includes(hostname)) return 'appleMusic'
|
|
903
|
+
if (['tidal.com', 'listen.tidal.com'].includes(hostname)) return 'tidal'
|
|
904
|
+
if (['deezer.com', 'www.deezer.com'].includes(hostname)) return 'deezer'
|
|
905
|
+
if (['soundcloud.com', 'www.soundcloud.com'].includes(hostname)) return 'soundcloud'
|
|
906
|
+
if (['pandora.com', 'www.pandora.com'].includes(hostname)) return 'pandora'
|
|
907
|
+
if (['napster.com', 'us.napster.com'].includes(hostname)) return 'napster'
|
|
908
|
+
if (['spinrilla.com', 'www.spinrilla.com'].includes(hostname)) return 'spinrilla'
|
|
909
|
+
} catch {
|
|
910
|
+
// Malformed URL, just ignore.
|
|
911
|
+
}
|
|
900
912
|
|
|
901
913
|
return null
|
|
902
914
|
}
|
|
@@ -917,14 +929,15 @@ module.exports = class Odesli {
|
|
|
917
929
|
try {
|
|
918
930
|
const urlObj = new URL(url)
|
|
919
931
|
const pathParts = urlObj.pathname.split('/').filter(Boolean)
|
|
932
|
+
const hostname = urlObj.hostname.toLowerCase()
|
|
920
933
|
|
|
921
934
|
// Spotify
|
|
922
|
-
if (
|
|
935
|
+
if (hostname === 'open.spotify.com') {
|
|
923
936
|
return pathParts[pathParts.length - 1]
|
|
924
937
|
}
|
|
925
938
|
|
|
926
939
|
// Apple Music - improved to handle various URL formats
|
|
927
|
-
if (
|
|
940
|
+
if (['music.apple.com', 'itunes.apple.com'].includes(hostname)) {
|
|
928
941
|
// Handle album URLs with track IDs: /album/name/id?i=trackId
|
|
929
942
|
const trackIdMatch = url.match(/[?&]i=(\d+)/)
|
|
930
943
|
if (trackIdMatch) {
|
|
@@ -938,38 +951,38 @@ module.exports = class Odesli {
|
|
|
938
951
|
}
|
|
939
952
|
|
|
940
953
|
// Handle other Apple Music URL formats
|
|
941
|
-
const
|
|
942
|
-
if (
|
|
943
|
-
return
|
|
954
|
+
const lastPart = pathParts[pathParts.length - 1];
|
|
955
|
+
if (/^\d+$/.test(lastPart)) {
|
|
956
|
+
return lastPart;
|
|
944
957
|
}
|
|
945
958
|
|
|
946
959
|
return null
|
|
947
960
|
}
|
|
948
961
|
|
|
949
962
|
// YouTube
|
|
950
|
-
if (
|
|
963
|
+
if (['youtube.com', 'www.youtube.com', 'music.youtube.com', 'youtu.be'].includes(hostname)) {
|
|
951
964
|
const videoMatch = url.match(/(?:v=|\/)([a-zA-Z0-9_-]{11})/)
|
|
952
965
|
return videoMatch ? videoMatch[1] : null
|
|
953
966
|
}
|
|
954
967
|
|
|
955
968
|
// Tidal
|
|
956
|
-
if (
|
|
969
|
+
if (['tidal.com', 'listen.tidal.com'].includes(hostname)) {
|
|
957
970
|
return pathParts[pathParts.length - 1]
|
|
958
971
|
}
|
|
959
972
|
|
|
960
973
|
// Deezer
|
|
961
|
-
if (
|
|
974
|
+
if (['deezer.com', 'www.deezer.com'].includes(hostname)) {
|
|
962
975
|
return pathParts[pathParts.length - 1]
|
|
963
976
|
}
|
|
964
977
|
|
|
965
978
|
// Amazon Music
|
|
966
|
-
if (
|
|
979
|
+
if (['music.amazon.com'].includes(hostname) || (['amazon.com', 'www.amazon.com'].includes(hostname) && urlObj.pathname.toLowerCase().startsWith('/music'))) {
|
|
967
980
|
const idMatch = url.match(/\/albums\/([^/]+)/)
|
|
968
981
|
return idMatch ? idMatch[1] : null
|
|
969
982
|
}
|
|
970
983
|
|
|
971
984
|
// SoundCloud
|
|
972
|
-
if (
|
|
985
|
+
if (['soundcloud.com', 'www.soundcloud.com'].includes(hostname)) {
|
|
973
986
|
return pathParts[pathParts.length - 1]
|
|
974
987
|
}
|
|
975
988
|
|