Skip to content

Instantly share code, notes, and snippets.

@plamere
Created December 6, 2011 13:45
Show Gist options
  • Save plamere/1438262 to your computer and use it in GitHub Desktop.
Save plamere/1438262 to your computer and use it in GitHub Desktop.
Demonstration of how to call the Echo Nest API from a Spotify App
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playlister</title>
<link rel="stylesheet" href="sp://import/css/adam.css">
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<h2 id='title'> Here's a cool playlist based upon what is now playing!</h2>
<div id="info"> </div>
<button id="new" onclick="makePlaylistFromNowPlaying()"> Generate Playlist </button>
<div id='all-results'>
<ul id="results"> </ul>
</div>
</body>
<script type="text/javascript">
sp = getSpotifyApi(1);
jQuery.ajaxSettings.traditional = true;
function makePlaylistFromNowPlaying() {
var playerTrackInfo = sp.trackPlayer.getNowPlayingTrack();
console.log(playerTrackInfo);
if (playerTrackInfo == null) {
info("Start playing something and I'll make a playlist of good songs based on that song");
} else {
var track = playerTrackInfo.track;
var artist = track.artists[0].name;
fetchPlaylist(artist, 25);
}
}
function fetchPlaylist(artist, size) {
info('Getting playlist for ' + artist);
var url = 'http://developer.echonest.com/api/v4/playlist/basic?api_key=N6E4NIOVYMTHNDM8J&callback=?';
$.getJSON(url, { 'artist': artist, 'format':'jsonp',
'results': size, 'type':'artist-radio'}, function(data) {
if (checkResponse(data)) {
$("#results").empty();
info("");
var curTracks = []
for (var i = 0; i < data.response.songs.length; i++) {
var song = data.response.songs[i];
var song_name = song.title + " by " + song.artist_name;
var li = $("<li>").text(song_name);
$("#results").append(li);
}
} else {
info("trouble getting results");
}
});
}
function info(s) {
$("#info").text(s);
}
function error(s) {
info(s);
}
function checkResponse(data) {
if (data.response) {
if (data.response.status.code != 0) {
error("Whoops... Unexpected error from server. " + data.response.status.message);
log(JSON.stringify(data.response));
} else {
return true;
}
} else {
error("Unexpected response from server");
}
return false;
}
$(document).ready(function() {
makePlaylistFromNowPlaying();
});
</script>
</html>
{
"BundleType": "Application",
"AppIcon": {
"18x18": "icon.png"
},
"AppName": {
"en": "SpotifyEchoNestPlaylistDemo"
},
"SupportedLanguages": [
"en"
],
"RequiredPermissions": [
"http://*.echonest.com"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment