package com.dareville.common.utils.spotify { /** * SpotifyUtil * * @author krisrange */ public class SpotifyUtil { public static const URL_FORMAT_HTTP : String = "http"; public static const URL_FORMAT_SPOTIFY : String = "spotify"; /** * Validates the provided playlist URL and can return it as either a * HTTP or Spotify URL format. Method will return null if the value is * not a correctly formatted spotify or http spotify URL. * * Example: * spotify:user:user_name:playlist:3WbWswJtOPIdKBQmZs9pGr * http://open.spotify.com/user/user_name/playlist/3WbWswJtOPIdKBQmZs9pGr * * @param url Playlist url string * @param return_type The format the playlist should be returned as * * @return Boolean */ public static function validatePlaylist( url : String, return_type : String = URL_FORMAT_SPOTIFY ) : String { // First make sure that we we have a url if( !url ) { return null; } // Regular expression we will use to test the url passed in var spotifyURI : RegExp = new RegExp( /spotify:user:([^:]+):playlist:([\w]+)/s ); var httpURI : RegExp = new RegExp( /http:\/\/open.spotify.com\/user\/([^\/]+)\/playlist\/([\w]+)/s ); var httpPrefix : String = "http://open.spotify.com/user/"; var spotifyMatch : Array = url.match( spotifyURI ); var httpMatch : Array = url.match( httpURI ); var match : Array; if( spotifyMatch ) match = spotifyMatch; if( httpMatch ) match = httpMatch; // No match means with either means the value is invalid if( !match ) return null; // We now have a valid URL. Now we need to check the format, if our // format does not match, we still might have to return null. // Otherwise, return the correct format switch( return_type ) { case URL_FORMAT_SPOTIFY : url = "spotify:user:" + match[1] + ":playlist:" + match[2]; break; case URL_FORMAT_HTTP : url = "http://open.spotify.com/user/" + match[1] + "/playlist/" + match[2] + "/"; break; default: url = null; } return url; } /** * Validates the provided track URL and can return it as either a * HTTP or Spotify URL format. Method will return null if the value is * not a correctly formatted spotify or http spotify URL. * * Example: * spotify:track:02FIWpmMzU0ivgRe3J470T * http://open.spotify.com/track/02FIWpmMzU0ivgRe3J470T * * @param url Track url string * @param return_type The format the track should be returned as * * @return Boolean */ public static function validateTrack( url : String, return_type : String = URL_FORMAT_SPOTIFY ) : String { // First make sure that we we have a url if( !url ) { return null; } // Regular expression we will use to test the url passed in var spotifyURI : RegExp = new RegExp( /spotify:track:([\w]+)/s ); var httpURI : RegExp = new RegExp( /http:\/\/open.spotify.com\/track\/([\w]+)/s ); var httpPrefix : String = "http://open.spotify.com/track/"; var spotifyMatch : Array = url.match( spotifyURI ); var httpMatch : Array = url.match( httpURI ); var match : Array; if( spotifyMatch ) match = spotifyMatch; if( httpMatch ) match = httpMatch; // No match means with either means the value is invalid if( !match ) return null; // We now have a valid URL. Now we need to check the format, if our // format does not match, we still might have to return null. // Otherwise, return the correct format switch( return_type ) { case URL_FORMAT_SPOTIFY : url = "spotify:track:" + match[1]; break; case URL_FORMAT_HTTP : url = "http://open.spotify.com/track/" + match[1] + "/"; break; default: url = null; } return url; } } }