Last active
July 3, 2022 05:00
-
-
Save ahallora/75f65ca399a69b7a2b3d to your computer and use it in GitHub Desktop.
Validate Spotify Playlist HTTP and URI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Script to validate Spotify playlist HTTP and URI's | |
* Valid format: http://open.spotify.com/user/__username__/playlist/__playlistID__ | |
* spotify:user:__username__:playlist:__playlistID__ | |
* | |
* License: MIT | |
*/ | |
function validateSpotifyPlaylistHTTPandURI(input) { | |
isValid = false; | |
/* | |
* VALIDATE SPOTIFY HTTP | |
*********************** | |
*/ | |
if(input.indexOf('/')>-1) { | |
// not a valid spotify URI - check if valid Spotify HTTP and convert | |
parts = input.split('/'); | |
spotifyIndex = -1; | |
for(b=0;b<parts.length;b++) { | |
// check if the URL is to spotify | |
if(parts[b].indexOf('spotify.com') >- 1) { | |
spotifyIndex = b; | |
break; | |
} | |
} | |
if(spotifyIndex > -1) { | |
if(parts.length > spotifyIndex + 4) { | |
// the input is long enough to hold the additional parameters | |
isValid = true; | |
// make sure the structure of the url is correct | |
// it is not possible to validate the username or playlist ID | |
if( parts[spotifyIndex + 1] != 'user') isValid = false; | |
if( parts[spotifyIndex + 3] != 'playlist') isValid = false; | |
} | |
} | |
} | |
/* | |
* VALIDATE SPOTIFY URI | |
********************** | |
*/ | |
if(!isValid && input.indexOf(':')>-1) { | |
parts = input.split(':'); | |
spotifyIndex = -1; | |
for(b=0;b<parts.length;b++) { | |
// check if the URL is to spotify | |
if(parts[b].indexOf('spotify') >- 1) { | |
spotifyIndex = b; | |
break; | |
} | |
} | |
if(spotifyIndex > -1) { | |
if(parts.length > spotifyIndex + 4) { | |
// the input is long enough to hold the additional parameters | |
isValid = true; | |
// make sure the structure of the url is correct | |
// it is not possible to validate the username or playlist ID | |
if( parts[spotifyIndex + 1] != 'user') isValid = false; | |
if( parts[spotifyIndex + 3] != 'playlist') isValid = false; | |
} | |
} | |
} | |
return isValid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M?si=cd98478d007c4ac9
it's a valid Spotify playlist but your script says it's not.