Created
January 24, 2011 19:24
-
-
Save rds/793770 to your computer and use it in GitHub Desktop.
Spotify Metadata API helpers for jQuery
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
(function($) { | |
$.grepWithLimit = function( elems, callback, limit, inv ) { | |
var ret = [], retVal; | |
inv = !!inv; | |
// Go through the array, only saving the items | |
// that pass the validator function | |
for ( var i = 0, length = elems.length; i < length; i++ ) { | |
retVal = !!callback( elems[ i ], i ); | |
if ( inv !== retVal ) { | |
ret.push( elems[ i ] ); | |
} | |
// Break when the limit has been reached | |
if ( limit >= ret.length) { | |
break; | |
} | |
} | |
return ret; | |
}, | |
})(jQuery); | |
(function($) { | |
var spotifyFunctions = { | |
spotifyLookup: function(uri, callback, options) { | |
options = $.extend(options, {}); | |
var data = { uri: uri }; | |
if (options.extras) data['extras'] = options.extras | |
return $.get('http://ws.spotify.com/lookup/1/', data, callback, options.dataType || 'json'); | |
}, | |
spotifySearch: function(query, callback, options) { | |
if (options == null) options = $.extend(options, {}) | |
if ($.isArray(query)) { | |
return $.each(query, function(query, i) { | |
return $.spotifySearch(query, callback, options) | |
}) | |
} else { | |
var data = { q: query, page: options.page || 1 }; | |
return $.get('http://ws.spotify.com/search/1/' + (options.method || 'track'), data, callback, options.dataType || 'json') | |
} | |
}, | |
filterTracksByCountryCode: function(tracks, twoLetterCountryCode, limit) { | |
if (limit) { | |
limit = 100; | |
} | |
if (twoLetterCountryCode == 'UK') { | |
twoLetterCountryCode = 'GB'; | |
} | |
return $.grepWithLimit(tracks, function(track, i) { | |
return track.album.availability.territories.search(twoLetterCountryCode) >= 0; | |
}, limit); | |
}, | |
spotifyArtistSearch: function(query, data, callback) { | |
return jQuery.spotifySearch(query, callback, $.extend(data, { method: 'artist' })); | |
}, | |
spotifyAlbumSearch: function(query, data, callback) { | |
return jQuery.spotifySearch(query, callback, $.extend(data, { method: 'album' })); | |
} | |
} | |
$.extend(spotifyFunctions); | |
$.spotifyTrackSearch = $.spotifySearch; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment