Skip to content

Instantly share code, notes, and snippets.

@ahallora
Last active July 3, 2022 05:00
Show Gist options
  • Save ahallora/75f65ca399a69b7a2b3d to your computer and use it in GitHub Desktop.
Save ahallora/75f65ca399a69b7a2b3d to your computer and use it in GitHub Desktop.
Validate Spotify Playlist HTTP and URI
/*
* 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;
}
@blakazulu
Copy link

https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M?si=cd98478d007c4ac9
it's a valid Spotify playlist but your script says it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment