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.
@@ -6,6 +6,9 @@ on:
6
6
  pull_request:
7
7
  branches: [master]
8
8
 
9
+ permissions:
10
+ contents: read
11
+
9
12
  jobs:
10
13
  test:
11
14
  runs-on: ubuntu-latest
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
- id = id.replace(/[^::]+::/g, '')
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 (id, options = {}) {
677
+ async getById(id, options = {}) {
678
678
  if (!id) throw new Error('No `id` was provided to odesli.getById()')
679
- if (!id.match(/\w+_\w+::\w+/g)) throw new Error('Provided Entity ID Does not match format. `<PLATFORM>_<SONG|ALBUM>::<UNIQUEID>`')
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 platform = id.replace(/_\w+::\w+/g, '').toLowerCase()
701
- const type = id.replace(/\w+_/g, '').replace(/::\w+/g, '').toLowerCase()
702
- const unique = id.replace(/\w+::/g, '')
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
- const urlLower = url.toLowerCase()
887
-
888
- if (urlLower.includes('spotify.com')) return 'spotify'
889
- if (urlLower.includes('music.apple.com') || urlLower.includes('itunes.apple.com')) return 'appleMusic'
890
- if (urlLower.includes('youtube.com') || urlLower.includes('youtu.be')) return 'youtube'
891
- if (urlLower.includes('youtube.com/music') || urlLower.includes('music.youtube.com')) return 'youtubeMusic'
892
- if (urlLower.includes('tidal.com')) return 'tidal'
893
- if (urlLower.includes('deezer.com')) return 'deezer'
894
- if (urlLower.includes('amazon.com/music') || urlLower.includes('music.amazon.com')) return 'amazonMusic'
895
- if (urlLower.includes('soundcloud.com')) return 'soundcloud'
896
- if (urlLower.includes('pandora.com')) return 'pandora'
897
- if (urlLower.includes('napster.com')) return 'napster'
898
- if (urlLower.includes('yandex.ru/music') || urlLower.includes('music.yandex.ru')) return 'yandex'
899
- if (urlLower.includes('spinrilla.com')) return 'spinrilla'
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 (url.includes('open.spotify.com')) {
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 (url.includes('music.apple.com') || url.includes('itunes.apple.com')) {
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 pathIdMatch = url.match(/\/(\d+)(?:\?|$)/)
942
- if (pathIdMatch) {
943
- return pathIdMatch[1]
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 (url.includes('youtube.com') || url.includes('youtu.be')) {
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 (url.includes('tidal.com')) {
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 (url.includes('deezer.com')) {
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 (url.includes('amazon.com/music') || url.includes('music.amazon.com')) {
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 (url.includes('soundcloud.com')) {
985
+ if (['soundcloud.com', 'www.soundcloud.com'].includes(hostname)) {
973
986
  return pathParts[pathParts.length - 1]
974
987
  }
975
988
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "odesli.js",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Node.js Client to query odesli.co (song.link/album.link) API",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",